Search code examples
react-nativebluetoothbluetooth-lowenergyreact-native-ble-plx

How to read/write data with BLE and `react-native-ble-plx`?


I successfully setup react-native-ble-plx to a handle BLE device.

I don't have any prior experience working with bluetooth. From my understanding and after reading the documentation, here is how I am supposed to read some informations from the bluetooth device.

// Connect to the device
const connectedDevice = await device.connect();

// Discover all services and characteristics
const servicesAndCharacteristics = await connectedDevice.discoverAllServicesAndCharacteristics();

// let's say I discovered these UUID
const SERVICE_UUID = '0000180a-0000-1000-8000-00805f9b34fb';
const CHARACTERISTIC_UUID = '00002a23-0000-1000-8000-00805f9b34fb';

// Writing command
const characteristic1 = await servicesAndCharacteristics.writeCharacteristicWithResponseForService(SERVICE_UUID, CHARACTERISTIC_UUID, CMD_GET_INFO);

// Reading the response
const response1 = await characteristic1.read();
console.log(response1.value); // Logs the response

I received this documentation from the firmware developer of the bluetooth device.

After establishing Bluetooth communication between the app and the controller, the app can retrieve device information using CMD_GET_INFO. At this stage, direct read-and-write to the device are not yet possible. The app needs to send the CMD_ENTER_PRO command and wait for the controller to respond with 'Success' before it can proceed to send other commands.

I don't understand this part

the app can retrieve device information using CMD_GET_INFO

I thought communication for BLE uses Service UUID and Characteristic UUID.
On the entire documentation i have zero mention about UUID's.
Only using CMD_GET_INFO, but how?

I tried every combinaison of SERVICE_UUID, and CHARACTERISTIC_UUID discovered with discoverAllServicesAndCharacteristics, used plain string for CMD_GET_INFO or base64.encode.

Every-time I encounter this kind of error:

Failed to connect Characteristic xxxx-xxxxx-xxxx-xxxx read failed for device

Questions:

  • What informations would I need to "CRUD" data on the device? (I have a web background so for me it's reading a classic REST api documentation).
  • Does it make sense if I ask to give examples how how to use SERVICE_UUID and CHARACTERISTIC_UUID.

I don't understand if the errors are coming from my code, or a broken build of the firmware.


Edit Following @Michael answer I downloaded nRF Connect.

It's great to debug. I think the errors are not from my implementation in React Native but from the device it self because I have this error:

BLE timeout


Solution

  • Upon investigating, I discovered that my issue is related to the Bluetooth firmware.

    Daniel explains it quite clearly in this video.
    https://youtu.be/UuHLPsjp6fM?si=iHAB80aVruJHUDQM&t=895

    Services UUID and Characteristics UUID comes from your device manual or your firmware engineer.

    So, i "just" need to ask a better documentation.