javascriptbluetooth-lowenergygattbluetooth-gattweb-bluetooth

web-bluetooth get stored data from device GATT


My goal is to get data stored in device.

Like device which is measuring temp or whatever and store it to its memory. i need to query all this data device has via Record Access Control Point (RACP).

  1. First thought, to achieve it, was

    • get characteristic

    • start notifications

    • write code to descriptor

    • get all data via eventListener

result: throws error on starting notifications examples used:

https://googlechrome.github.io/samples/web-bluetooth/notifications-async-await.html https://bugs.chromium.org/p/chromium/issues/detail?id=664863

  1. Next thought was to not starting notification since characteristic is INDICATE, WRITE type. So was thinking about add listener and write to descriptor code from device docs which states:

OP Code: 1 – Report stored records

even with deleted startNotifications line is throwing error so my code example is:

const mainService = 'my correct service uuid';
    const characteristicUUID1 = 'my correct char uuid';
    const characteristicUUID2 = 'my correct char uuid';
    const descriptorUUID = '00002902-0000-1000-8000-00805f9b34fb';
    let deviceCache = null;
    let serverCache = null;
    let serviceCache = null;
    let characteristicCacheA = null;
    let characteristicCacheB = null;
    let descriptorCache = null;

    try {
      deviceCache = await navigator.bluetooth.requestDevice({ filters: [{ name: 'my device' }] });

      console.log('Connecting to GATT Server...');
      serverCache = await deviceCache.gatt.connect();

      console.log('Getting Services...');
      serviceCache = await serverCache.getPrimaryService(mainService);

      console.log('Getting Characteristics A...');
      characteristicCacheA = await serviceCache.getCharacteristic(characteristicUUID1);

      console.log('Start Notifications A...');
      await characteristicCacheA.startNotifications();

      console.log('Getting Characteristics B...');
      characteristicCacheB = await serviceCache.getCharacteristic(characteristicUUID2);

      console.log('Start Notifications B...');
      await characteristicCacheB.startNotifications();

      console.log('Add event listener...');
      characteristicCacheA.addEventListener('characteristicvaluechanged', this.handleNotifications);

      console.log('Getting Descriptor...');
      descriptorCache = await characteristicCacheA.getDescriptor(descriptorUUID);

      console.log('Write value to descr...');
      await descriptorCache.writeValue(new Uint8Array([1]));


    } catch (error) {
      console.log(error.message, 'error');
    }

Error with notifications is(with experimental chrome features it doesn't throw error):

error: GATT operation failed for unknown reason.

Error with descriptor is:

writeValue() called on blocklisted object marked exclude-writes.

Also my device is asking for pin but web is connecting without prompting anything. And so maybe it says that writing to descriptor is blocked.

How to handle pin input - no clue(once i got prompt to enter pin after enabling chrome experimental features not sure if its related).

Is my logic correct? - dont think so.

Any suggestions?

What i have investigated so far?

  1. https://googlechrome.github.io/samples/web-bluetooth/
  2. https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html
  3. https://webbluetoothcg.github.io/web-bluetooth/

Edit: After reading this article - https://medium.com/@devdevcharlie/experimenting-with-web-bluetooth-1f1176047ddd

I think correct logic should be, write to command characteristic commands you need(like get all data). After that find correct characteristic responsible for this data from device docs, and start notifications and add eventListener and receive data.


Solution

  • As for now, chrome is not able or got issues with communicating with protected characteristics on windows 10, on macOS it is all working perfectly. I have posted issue on chromium bug tracker if someone is in to watching it. https://bugs.chromium.org/p/chromium/issues/detail?id=960258#c6