使用chrome.bluetoothLowEnergy API连接到iOS设备

时间:2014-07-22 07:02:39

标签: javascript google-chrome bluetooth bluetooth-lowenergy google-chrome-app

我一直在尝试使用chrome.bluetoothLowEnergy API通过BLE将iPhone 5连接到OSX Chrome(版本38.0.2096.0 dev)。

我在iphone上使用BLE Utility来模拟电池服务。我可以发现该服务并使用其他手机或OSX utility连接到该服务,但我在使用Chrome时遇到了问题。我可以看到我的设备已列出,但未发现任何服务,当我尝试连接到设备时,连接失败并显示以下消息:

  

操作失败

我非常感谢任何帮助。

这是我的代码:

的manifest.json

{
  "name": "BLE Test",
  "description": "Chrome BLE Test",
  "version": "1",
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "bluetooth": {
    "low_energy": true,
    "uuids": ["180f"]
  }
}

的index.html

<!DOCTYPE HTML>
<html>
  <head>
    <title>Chrome BLE Test</title> 
    <script type="text/javascript" src="script.js"></script>
  </head>
  <body>
  </body>
</html>

的script.js

function main() {
  var onGetServicesCallback = function(services) {
    if (chrome.runtime.lastError) {
      console.log(chrome.runtime.lastError.message);
      return;
    }

    console.log('Services:', services.length);

    if (!services) {
      console.log('No services');
      return;
    }

    services.forEach(function(service) {
      console.log(service);
    });
  }

  chrome.bluetooth.getDevices(function(devices) {
    if (chrome.runtime.lastError) {
      console.log(chrome.runtime.lastError.message);
      return;
    }
    console.log('Found devices:', devices.length);

    if (devices) {
      devices.forEach(function(device) {
        console.log('Device name:', device.name);
        chrome.bluetoothLowEnergy.getServices(device.address, onGetServicesCallback);

        chrome.bluetoothLowEnergy.connect(device.address, function() {
          if (chrome.runtime.lastError) {
            console.log('Connection failed:', chrome.runtime.lastError.message);
            return;
          }
          console.log('Connected!');
        });
      });
    }
  });
}

document.addEventListener('DOMContentLoaded', main);

1 个答案:

答案 0 :(得分:5)

据我所知,Mac的BLE实现尚未完成。 Chrome问题跟踪器是跟踪API开发的好地方。 This is a link具体涉及与Mac相关的问题。要查看所有蓝牙变化,只需在未解决的问题下搜索蓝牙。