2017-01-15 15 views
0

PhoneGap/Cordova Appに「randdusing/cordova-plugin-bluetooth」プラグインを使用してBluetoothアプリケーションを作成しています。私はプラグインのGitHub pageに与えられている簡単な例を使用しています。しかし、私は、リストされている任意のデバイスを取得しますが、メッセージの下にScanning for devices (will continue to scan until you select a device)...PhoneGap/CordovaでBluetooth 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); 
     } 
    } 

を使用していますコードですこれはコルドバの私の最初のBluetooth LEのプロジェクトです。私に助けてください、そして、他のプラグインを良い文書でこれよりも良いと提案してください。

ありがとうございました。

答えて

0

Android 6+でアプリをテストする場合は、まずデバイスのBluetooth機能を使用するために許可を要求する必要があります。さもなければあなたのアプリは黙って失敗し、発見されたデバイスは見られません。あなたは、他の権限を要求する必要がなければならない、あなたはこの

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

用のBluetooth LEプラグインの組み込みメソッドを使用するか、cordova-plugin-android-permissions追加することができます。

関連する問題