javascriptbluetoothbluetooth-lowenergybluetooth-gattcharacteristics

Write value as (command) to a BLE characteristic via Javascript


I am working on a Project, and I am new to BLE, I have got problem with writing value to BLE characteristic (run command on BLE device).

The characteristic take these values:

0x04 0x01 – Lock mode is on

0x04 0x02 – Lock mode is off

My method to read, notify and write from/to characteristic:

  async readWriteNotifyCharacteristics(serviceUuid, characteristicUuid, access, command, handel) {

            try {
          
                const service = await this.server?.getPrimaryService(serviceUuid);
                const characteristic = await service?.getCharacteristic(characteristicUuid);
                switch(access) {
                    case 'R':
                        return characteristic.readValue()

                    case 'N':
                        return characteristic.startNotifications()
                            .then(() => {
                              characteristic.addEventListener('characteristicvaluechanged', handel);
                            });

                    case 'W':
                            characteristic.writeValue(command);
                        break;

                    default:
                        console.log('could not conenct');

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

I try to write to characteristic with :

 async readWriteNotifyCharacteristics() {
        const buffer = new Uint16Array([0x06 , 0x02 ])
        await this.cacheCharacteristics(
            'f1196f20-qea4-00e6-bdh89-65790865c9a98',
            'f1196f22-qea4-00e6-bdh89-65790865c9a98',
            'W',
            buffer
        );


        const command = await this.cacheCharacteristics(
            'f1196f20-qea4-00e6-bdh89-65790865c9a98',
            'f1196f22-qea4-00e6-bdh89-65790865c9a98',
            'R'
        );

        },

Everything works fine connection, read from characteristic but nothing happen on the device when I try to write (command) on it. Any idea what I'm doing wrong? Thanks in advance:)


Solution

  • Thanks to Michael Kotzjan, With his suggestion of using nRF Connect, I was able to test many inputs and outputs and see the exact logic of device