I'm using React Native and the BLE package react-native-ble-plx on my Ubuntu machine.
According to the docs, I've done the following:
1. Scanned for devices
2. Connected to device
3. device.discoverAllServicesAndCharacteristics
4. writeCharacteristicWithResponseForDevice
My problem lies in reading the response I get from sending my command.
RX characteristic is writable, but TX is only notifiable and not readable. Therefore I need to monitor the characteristic with monitorCharacteristicForDevice, however I can't get this to function.
I never recieve neither 'monitor success
', 'Error at receiving data from device
' nor 'Monitor failure
' in the code below.
How do I read the return?
monitorDevice = () => {
this.setState({monitoring: true})
console.log(this.state.servicesDiscovered)
if(this.state.servicesDiscovered){
{this.manager.monitorCharacteristicForDevice(this.state.connectedTo,
SERVICE_UUID,
TX_CHARACTERISTIC,
(error, characteristic) => {
if (error) {
console.error("Error at receiving data from device", error);
return
}
else {
this.setState({monitoring: true})
console.log('monitor success')
console.log('monitor success' + characteristic.value);
this.state.messagesRecieved.push(characteristic.value)
}
})
}
}
else{
console.log("Monitor failure")
}
}
checkBattery = () => {
if(this.state.monitoring){
this.manager.writeCharacteristicWithResponseForDevice(this.state.connectedTo,
SERVICE_UUID,
RX_CHARACTERISTIC,
"YmF0dGVyeQ==")
.then(characteristic => {
console.log("Successfully sent: " + characteristic.value)
return
})
.catch(err => {
console.log(err)
})
}
else{
alert("Not monitoring")
}
}