You are reading a single comment by @Gordon and its replies.
Click here to read the full conversation.
-
Hi, thanks, now it works :)
Here is the working code :)
I fixed the uuid part and also the command was wrong :)return new Promise<string>((resolve, reject) => { return Ble.startDeviceScan(null, null, async (error, device) => { if (error) { console.log(error.message); if (typeof error.message === 'string') { //show error } reject(false); return; } console.log(device?.name); if (device?.name?.trim().startsWith('Puck.js')) { try { if (device) { Ble.stopDeviceScan(); const discoverAndWrite = async () => { await device.discoverAllServicesAndCharacteristics(); const result2 = await device.services(); result2.forEach(async (service) => { const charList = await service.characteristics(); charList.forEach(async (characteristic) => { if (characteristic.uuid === '6e400002-b5a3-f393-e0a9-e50e24dcca9e') { const data = Buffer.from('LED.toggle();\n').toString('base64'); characteristic .writeWithResponse(data) .then((val) => console.log('value', val)) .catch((reason) => console.log('reason', reason)); } if (characteristic.uuid === '6e400003-b5a3-f393-e0a9-e50e24dcca9e') { characteristic.monitor((error, newCharacteristic) => { console.log('monitor', error, newCharacteristic); }); } }); }); }; const connected = await device.isConnected(); if (!connected) { await device.connect(); } await discoverAndWrite(); resolve(device.id); } } catch (err: any) { console.log('errMessage', err); if (typeof err.message === 'string') { // show error } reject(err.message); } } }); });
I think the issue is you're trying to read and write from characteristic
6e400002-b5a3-f393-e0a9-e50e24dcca9e
If you check other code that uses BLE at something like : https://www.espruino.com/Interfacing#node-js-javascript
It writes to
"6e400002-b5a3-f393-e0a9-e50e24dcca9e"
(as you're doing) but then reads from (notifies/monitors)"6e400003-b5a3-f393-e0a9-e50e24dcca9e"
So you need:
When you get this working, please could you paste up the final code? It'd be graet to have a full example of using
react-native-ble-plx