React-native-ble-plx: Can't monitor device

Created on 5 Feb 2020  Â·  8Comments  Â·  Source: Polidea/react-native-ble-plx

According to the docs, I've done the following:

  • Scanned for devices
  • Connected to device
  • device.discoverAllServicesAndCharacteristics
  • 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" nor "monitorDevice called" in the code below. If I run the checkBattery() function after connection it returns "Not monitoring".

How do I read the return?

monitorDevice = () => {
    console.log("monitorDevice called")
    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)
            return
        }
    })
}

connectAndFetchServicesAndCharacteristics = (device) => {
    this.manager.connectToDevice(device.id, null)
    .then(device => {
        this.setState({connectedTo:device.id})
        console.log('Connection successful\nScan stopped')
        this.manager.stopDeviceScan();
        this.setState({scan: false})
        return device.discoverAllServicesAndCharacteristics()
    })
    .then(device => {
        this.setState({servicesDiscovered: true})
        console.log("Services discovered")
        this.monitorDevice()
        return
    })
    .catch(err => {
        console.log(err)
    })
}

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")
    }
}
question

All 8 comments

this.monitorDevice is a function therefore you must
this.monitorDevice => this.monitorDevice()

My bad, changed it in my original post. There is still no difference however, other than that I recieve monitorDevice called since I now actually call the function.

Good. So now you can show the native logs to see what is happening if your peripheral does indeed send any notifications

Alright, with verbose loglevel I get "ConnectOperation finished" followed by peripheral info. After I run checkBattery() I get:

D RxBle#ConnectionOperationQueue: QUEUED   CharacteristicWriteOperation(88434918)
D RxBle#ConnectionOperationQueue: STARTED  CharacteristicWriteOperation(88434918)
D RxBle#BluetoothGatt$1: onCharacteristicWrite characteristic=... status=0
V RxBle#BleModule: Write to Characteristic(uuid: ..., id: 8, value: 62617474657279)
D RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(88434918) in 121 ms

Nothing happens after that.

The interesting part is if notification is properly set. So actually that and write sequence

Sorry, I don't understand. Do you think both notification and write is not properly set up in the above code?

I dunno. I want to check what is going on.

btw. you directly set the state this.state.connectedTo = device.id

Progress! Hooking the peripheral up to a serial monitor I can see that the received string is basically gibberish. "battery" is received as "m畫mz�". Some type of encoding problem

Edit: Solved it by using base-64 library instead of sending a string that was already base64 encoded.

Was this page helpful?
0 / 5 - 0 ratings