Bluetooth LE支持PhoneGap / Cordova中的应用

时间:2017-01-15 16:14:31

标签: android ios cordova bluetooth

我在PhoneGap / Cordova App中使用“randdusing / cordova-plugin-bluetoothle”插件来创建蓝牙应用程序。我正在使用插件GitHub page上给出的一个简单示例。但我没有列出任何设备但收到消息Scanning for devices (will continue to scan until you select a device)...

以下是我正在使用的代码

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);
      }
  }

这是我在Cordova的第一个蓝牙LE项目。请帮助我,并提供比这更好的任何其他插件和良好的文档。

感谢。

1 个答案:

答案 0 :(得分:0)

如果您在Android 6+上测试您的应用,则需要首先请求权限才能使用设备的蓝牙功能。否则,您的应用程序将无声地失败,您将看不到任何设备被发现。您可以使用蓝牙LE插件的内置方法

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

如果您还需要申请其他权限,也可以添加cordova-plugin-android-permissions