0

needing a guide for how to layout functionality for a React Native app that's pairing with an ESP32 that will eventually feed back weight readings using read Characteristic, and be able to toggle a DI via write to a characteristic.

i can currently scan and connect to the ESP32 and show the values from the ESP32 (random changing values for now) and also toggle the LED via changing a hardcoded value. But i want to be able to do this via a button in the app.

 const scanDevices = () => {
        //set isLoading to true to show activity Indicator
        setIsLoading(true);

        //scan for devices, (UUIDs, ScanOptions(error, device))
        manager.startDeviceScan(null, null, (error, device) => {
            if (error) {
                console.log("Error in scanning", error.message)
                return;
            }
            if (device) {   
                //if a device is scanned, add the name & id details into the scannedDevice object via reducer             
                dispatch({type: 'DEVICE_ADD', payload: {name: device.name, id: device.id}});
            }
        });
        
        //end scan after 3 seconds, stop the activity indicator swirly thing
        setTimeout(() => {
            console.log("Scan timeout after 5 seconds");
            manager.stopDeviceScan();
            setIsLoading(false);
          }, 5000);
    };
  const deviceConnect = (device) => {

            console.log("Connecting to:", device.name, device.id);
            setIsConnected(true);
            setConnectedDevice(device);
        
            manager.connectToDevice(device.id)
                .then((device) => {
                    console.log("Discovering all services & chars");
                    return device.discoverAllServicesAndCharacteristics()
                }).then((device) => {     
                   // console.log("Write Value inside deviceConnect:", writeValue)
                    console.log("Device:", device.name, "has been connected."); 
                    return deviceNotifications(device, writeValue);
                }).catch((error) => {
                    console.log("device connect error:", device.name, error)
                    //JSON.stringify(error)
                });
            
    };
   const deviceNotifications = async (device, writeValue) => {

        const service = "af493e2a-f002-11eb-9a03-0242ac130003";
        const characteristicTX = "af49423a-f002-11eb-9a03-0242ac130003";
        const characteristicRX = "af49414a-f002-11eb-9a03-0242ac130003"; 

        if (device) {
            try {
                device.monitorCharacteristicForService(service, characteristicTX, (error, characteristic) => {
                    if (error) {
                        console.log(error);
                    } else {
                        setCharacteristicValue(() => {                    
                        return [{id: uuid.v4(), value: (base64.decode(characteristic.value))}];
                    })}
                });
            device.writeCharacteristicWithResponseForService(service, characteristicRX, base64.encode(writeValue));
            console.log("Writing to RX:", writeValue);   
            }
            catch (err) {
                console.log("deviceNotification catch error:", err);
            } 
        };
}

I'm getting pretty confused trying to sort through the [ble-plx documentation][1] ([github wiki][2])

Currently the only way i can get the LED to turn on/off, is i have the LED toggle section inside the deviceNotifications async function and have to manually change the value that's being encoded and written in the code itself, rather than from the App UI using an useState value.

I tried using the useState toggle off a button (which toggled the value and logged out OK), and then re-calling the deviceConnect function, but the commented out console.log in the .then promise section didn't work past the first one, returning which turned the LED on (writing 'A' to the characteristic).

thanks for any help in advance, i know a lot of these ble-plx questions go unanswered.

//this is at a top level inside the main function
 const [writeValue, setWriteValue] = useState('A');
    const toggleLED = () => {
        if (writeValue == 'B') {
            setWriteValue('A');
            console.log("Toggling write value:", writeValue);
        } else {
            setWriteValue('B')
            console.log("Toggling write value", writeValue)
        };
    };



  [1]: https://dotintent.github.io/react-native-ble-plx/
  [2]: https://github.com/dotintent/react-native-ble-plx/wiki
  [3]: https://www.polidea.com/blog/ReactNative_and_Bluetooth_to_An_Other_level/
MaceyMace
  • 3
  • 4

0 Answers0