0

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:)

baran
  • 1
  • 4
  • Maybe this is just a typo but in your question you state you need to send `0x04 0x02` but in your code you send `0x06 0x02`. have you tried using a generic BLE scnaner app such as [nRF Connect](https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-mobile) to write the values to the device and see if anything happens? – Michael Kotzjan Oct 10 '22 at 06:39
  • Thanks Michael, I have already tried many values but unfortunately without success. the write to device is done successfully when I tried to read the same characteristic but no change in device (no on / off ). I use the code in os app based on electron js and Vue2 – baran Oct 10 '22 at 07:07
  • Without documentation, this is a painstaking process of try-and-error with different values. If the device (a lock I assume) comes with an official app you could try to sniff the correct values, but I would imagine a lock to use some kind of encryption. Please try using nRF Connect to experiment further – Michael Kotzjan Oct 10 '22 at 07:14

1 Answers1

0

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

baran
  • 1
  • 4
  • Can you explain a little more about how nRF Connect helped you? Comments are ephemeral, so anything in a comment that helped you should be quoted in your answer. – RamenChef Oct 19 '22 at 02:25