capacitor-community / bluetooth-le

Capacitor plugin for Bluetooth Low Energy
MIT License
281 stars 85 forks source link

Notification callback is not being raise in any device and the instructions sent to the characteristics are not being written #677

Open luissanchez0305 opened 2 months ago

luissanchez0305 commented 2 months ago

Describe the bug The plugin is being used to connect a Bluetooth device to the mobile. It works up to setting the notification on the mobile device to check changes in the characteristics. I know this because the Bluetooth device starts blinking differently.

But when I try to write into a characteristic it's not raising the callback set on the notification. Secondly, the device should have a 3rd set of blinking when an instruction is written in a characteristic, which never happens.

For what is worth, I have set all the necessary code in a Android Java app and it works correctly, so the Bluetooth device is working correctly

To Reproduce Steps to reproduce the behavior:

  1. Go to connect device and choose the device Instructions use up to here ´´´ // plugin initialize await BleClient.initialize()

// to connect the device await BleClient.connect(id, (deviceId) => this.onDisconnect(deviceId));

// to search in all the services and characteristics of the device and choose the right one await BleClient.getServices(id);

// to make sure there is no other notification set on the mobile device await BleClient.stopNotifications(deviceId, serviceId, characteristic);

// set the notification, this instruction change the blinking on the BT device, so it's working await BleClient.startNotifications( deviceId, serviceId, characteristicId, (value) => { console.log('current heart rate', value);

            console.log('raw', value);
            console.log('text', dataViewToText(value));
            console.log('hex', dataViewToHexString(value));
            console.log('numbers', dataViewToNumbers(value));
        }
    );

// send the instructions to the device const message = 'P : 2 , 3 , 5 , 6'; const arrayString = message.split(' '); const buffer = new ArrayBuffer(arrayString.length); const view = new DataView(buffer, 0); for (let i = 0; i < arrayString.length; i++) { let v; if ( typeof arrayString[i] === 'string' && arrayString[i].charAt(1) === 'x' ) { const hexString = arrayString[i]; const hex = hexString.replace(/^0x/, ''); v = hex.match(/[\dA-F]{2}/gi); } else if ( typeof arrayString[i] === 'number' ) { v = arrayString[i]; } else { v = new TextEncoder().encode(arrayString[i]); console.log('hex', arrayString[i], v); } view.setUint8(i,v); }

    await BleClient.disconnect(id);
    await BleClient.connect(id, (deviceId) => console.log('Disconnected from', deviceId));
    await BleClient.write(id, serviceUuid, characteristicUuid, view);

// 1st ERROR: the notification callback doesn't run

// 2nd ERROR: the notification callback doesn't run on another instruction and the 2nd blinking change never happens. Which give the idea that the written have never taken place ´´´

Expected behavior

  1. Callback notification raise
  2. BT device changes the set of blinking lights to acknowledge the instructions are being set correctly

Screenshots image image image

Plugin version: