Struggling with react native and puck js

Posted on
  • Hi :)

    Im working on integrating the Puck.js into my react native application using react-native-ble-plx
    Everything is working fine, but when i try to write some data, i just get this message

    [BleError: Characteristic 6e400002-b5a3-f393-e0a9-e50e24dcca9e notify change failed for device 1E986C85-8037-E56E-A18D-0F38826AAB12 and service 6e400001-b5a3-f393-e0a9-e50e24dcca9e]

    Thats my code :)

    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 connected = await device.isConnected();
    
                                if (!connected) {
                                    await device.connect();
                                    await device.discoverAllServicesAndCharacteristics();
                                    const result2 = await device.services();
                                    result2.forEach(async (service) => {
                                        const charLit = await service.characteristics();
                                        charLit.forEach(async (chari) => {
                                            console.log('carhi', chari);
                                            if (chari.uuid === '6e400002-b5a3-f393-e0a9-e50e24dcca9e') {
                                                chari.monitor((error, char) => {
                                                    console.log('monitor,', error, char !== undefined);
                                                });
    
                                                const data = Buffer.from(['LED1.set();/n']).toString('base64');
                                                chari
                                                    .writeWithResponse(data)
                                                    .then((val) => console.log('val', val))
                                                    .catch((reason) => console.log('reson', reason));
                                            }
                                        });
                                    });
                                }
    
                                resolve(device.id);
                            }
                        } catch (err: any) {
                            console.log('errMessage', err);
                            if (typeof err.message === 'string') {
                                // show error
                            }
    
                            reject(err.message);
                        }
                    }
                });
            });
    

    I would be greatful for any help :)

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

    if (chari.uuid === '6e400003-b5a3-f393-e0a9-e50e24dcca9e') {
      chari.monitor((error, char) => {
          console.log('monitor,', error, char !== undefined);
      });
    }
    if (chari.uuid === '6e400002-b5a3-f393-e0a9-e50e24dcca9e') {
      const data = Buffer.from(['LED1.set();/n']).toString('base64');
      chari
          .writeWithResponse(data)
    

    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

  • 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);
                        }
                    }
                });
            });
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Struggling with react native and puck js

Posted by Avatar for user154849 @user154849

Actions