PhoneGap/Cordova 中支持蓝牙 LE 的应用程序

Posted

技术标签:

【中文标题】PhoneGap/Cordova 中支持蓝牙 LE 的应用程序【英文标题】:Bluetooth LE Suported app in PhoneGap/Cordova 【发布时间】:2017-01-15 16:14:30 【问题描述】:

我在 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】:

如果您在 android 6+ 上测试您的应用,您需要先申请许可才能使用设备的蓝牙功能。否则,您的应用程序将静默失败,您将看不到任何发现的设备。您可以为此使用蓝牙 LE 插件的内置方法

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

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

【讨论】:

以上是关于PhoneGap/Cordova 中支持蓝牙 LE 的应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap/Cordova 国际化支持

PhoneGap/Cordova 3 标准客户端数据库

phonegap+cordova+ionic调用原生API

PhoneGap/Cordova:如何使用 cookie (iOS)?

Phonegap/cordova IOS横向视频

如何使用cordova命令行创建好一个工程