2017-01-15 1 views
0

PhoneGap/Cordova App에서 "randdusing/cordova-plugin-bluetoothle"플러그인을 사용하여 Bluetooth 응용 프로그램을 만듭니다. 나는 플러그인의 GitHub page에 주어진 간단한 예제를 사용하고있다. 하지만 나열된 장치를 받고 있지만, 메시지 아래 Scanning for devices (will continue to scan until you select a device)...PhoneGap/Cordova의 Bluetooth LE Suported 응용 프로그램

을 받고 있지 않다 것은 내가이 코르도바에서 처음으로 블루투스 LE 프로젝트입니다

document.addEventListener("deviceready", onDeviceReady, false); 

    function onDeviceReady() { 
     bluetoothle.initialize({ 
      request: true, 
      statusReceiver: false 
     }, initializeSuccess, handleError); 
    } 

    function initializeSuccess(result) { 

     if (result.status === "enabled") { 

      log("Bluetooth is enabled."); 
      log(result); 
     } else { 

      document.getElementById("start-scan").disabled = true; 

      log("Bluetooth is not enabled:", "status"); 
      log(result, "status"); 
     } 
    } 

    function handleError(error) { 

     var msg; 

     if (error.error && error.message) { 

      var errorItems = []; 

      if (error.service) { 

       errorItems.push("service: " + (uuids[error.service] || error.service)); 
      } 

      if (error.characteristic) { 

       errorItems.push("characteristic: " + (uuids[error.characteristic] || error.characteristic)); 
      } 

      msg = "Error on " + error.error + ": " + error.message + (errorItems.length && (" (" + errorItems.join(", ") + ")")); 
     } else { 

      msg = error; 
     } 

     log(msg, "error"); 

     if (error.error === "read" && error.service && error.characteristic) { 

      reportValue(error.service, error.characteristic, "Error: " + error.message); 
     } 
    } 
    var foundDevices = []; 

    function startScan() { 

     log("Starting scan for devices...", "status"); 



     document.getElementById("devices").innerHTML = ""; 
     document.getElementById("services").innerHTML = ""; 
     document.getElementById("output").innerHTML = ""; 

     if (window.cordova.platformId === "windows") { 

      bluetoothle.retrieveConnected(retrieveConnectedSuccess, handleError, {}); 
     } else { 

      bluetoothle.startScan(startScanSuccess, handleError, { 
       services: [] 
      }); 
     } 
    } 

    function startScanSuccess(result) { 
     log("startScanSuccess(" + result.status + ")"); 

     if (result.status === "scanStarted") { 
      log("Scanning for devices (will continue to scan until you select a device)...", "status"); 
     } else if (result.status === "scanResult") { 

      if (!foundDevices.some(function (device) { 

        return device.address === result.address; 

       })) { 

       log('FOUND DEVICE:'); 
       log(result); 
       foundDevices.push(result); 
       addDevice(result.name, result.address); 
      } 
     } 
    } 

    function retrieveConnectedSuccess(result) { 

     log("retrieveConnectedSuccess()"); 
     log(result); 

     result.forEach(function (device) { 

     addDevice(device.name, device.address); 

     }); 
    } 

    function addDevice(name, address) { 

     var button = document.createElement("button"); 
     button.style.width = "100%"; 
     button.style.padding = "10px"; 
     button.style.fontSize = "16px"; 
     button.textContent = name + ": " + address; 

     button.addEventListener("click", function() { 

      document.getElementById("services").innerHTML = ""; 
      connect(address); 
     }); 

     document.getElementById("devices").appendChild(button); 
    } 

    function log(msg, level) { 

     level = level || "log"; 

     if (typeof msg === "object") { 

      msg = JSON.stringify(msg, null, " "); 
     } 

     console.log(msg); 

     if (level === "status" || level === "error") { 

      var msgDiv = document.createElement("div"); 
      msgDiv.textContent = msg; 

      if (level === "error") { 

       msgDiv.style.color = "red"; 
      } 

      msgDiv.style.padding = "5px 0"; 
      msgDiv.style.borderBottom = "rgb(192,192,192) solid 1px"; 
      document.getElementById("output").appendChild(msgDiv); 
     } 
    } 

을 사용하고있는 코드입니다. 제발 나를 도우며 좋은 문서를 가지고 이것보다 다른 플러그인을 제안 해주십시오.

감사합니다.

답변

0

Android 6 이상에서 앱을 테스트하는 경우 기기의 블루투스 기능을 사용하려면 먼저 권한을 요청해야합니다. 그렇지 않으면 앱이 자동으로 실패하고 발견 된 기기가 표시되지 않습니다. 당신은뿐만 아니라 다른 권한을 요청해야한다,이

bluetoothle.requestPermission().then(success, fail) 

의 블루투스 LE 플러그인의 기본 방법을 사용할 수 있습니다 또는 당신은 cordova-plugin-android-permissions 추가 할 수 있습니다.

관련 문제