lundi 8 avril 2019

characteristic startNotifications throws error: GATT operation failed for unknown reason

Using web Bluetooth when trying to start notifications it throws error

Used this example https://googlechrome.github.io/samples/web-bluetooth/notifications-async-await.html

also found something similar about my problem https://bugs.chromium.org/p/chromium/issues/detail?id=664863 however it doesnt help since i think i do everything correct

Here is my code example:

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');
    }

My goal is to read stored data on device. Like device was measuring temp or whatever and store it to its memory. i need to query all data device has. As far as i understand i should start notifications, then need to write to CCCD value informing it what i want to get, and on 'characteristicvaluechanged' event i will receive all i need.

However when getting to characteristicCacheA.startNotifications() it fails with error: GATT operation failed for unknown reason. What could be possibly be the problem?




Aucun commentaire:

Enregistrer un commentaire