vendredi 21 septembre 2018

Web Bluetooth API, BluetoothRemoteGATTServer.connect method doesn't connect desktop with mobile

I'm trying to connect desktop web app with mobile phone or arduino bluetooth module HC-05 through bluetoth.

After requesting available devices I get instance of BluetoothDevice

    BluetoothDevice {id: "w3G0aamPHHaBwLatIUFJcQ==", name: "Galaxy J5 (2016)", gatt: BluetoothRemoteGATTServer, ongattserverdisconnected: null}
gatt: BluetoothRemoteGATTServer {connected: false, device: BluetoothDevice}
id: "w3G0aamPHHaBwLatIUFJcQ=="
name: "Galaxy J5 (2016)"
ongattserverdisconnected: null
__proto__: BluetoothDevice

Next step is to connect with the instance of BluetoothRemoteGATTServer using it's connect() function. But it doesn't works, also as it doesn't catch any error in catch block.

Here is code example with try to get battery level value.

navigator.bluetooth
  .requestDevice({
    acceptAllDevices: true,
    optionalServices: ['battery_service']
  })
  .then((device) => {
    return device.gatt.connect();
  })
  .then((server) => {
    return server.getPrimaryService('battery_service');
  })
  .then((service) => {
    return service.getCharacteristic('battery_level');
  })
  .then((characteristic) => {
    return characteristic.readValue();
  })
  .then((value) => {
    console.log(`Battery percentage is ${value.getUint8(0)}`);
  })
  .catch((error) => {
    console.log(`error ${error}`);
  });

Any help will be highly appreciated.




Aucun commentaire:

Enregistrer un commentaire