Connecting to an iOS device using the chrome.bluet

2019-02-15 19:16发布

问题:

I have been trying to connect an iPhone 5 to OSX Chrome (Version 38.0.2096.0 dev) over BLE using chrome.bluetoothLowEnergy API.

I'm using BLE Utility on my iphone to simulate battery service. I can discover the service and connect to it using other phones or OSX utility, but I'm having problems doing so with chrome. I can see my device listed, but no services discovered and when I try to connect to the device, connection fails with the following message:

Operation failed

I would greatly appreciate any help.

Here is my code:

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:

The BLE implementation for the Mac is not complete yet as far as I know. A good place to follow the development of the API is on the Chrome issue tracker. This is a link to the issue related to the Mac one specifically. To see all the bluetooth changes, just search for bluetooth under open issues.