So, I'm trying to send data to BLE device. My phone and BLE device are already connected.
My function:
const sendCom = async () => {
const data = utf8Encode('1') // convert data to bytes
const service = 'fff0'
const characteristic = 'fff1'
try {
await BleManager.retrieveServices(peripheral.id)
await BleManager.startNotification(peripheral.id, service, characteristic)
await BleManager.write(peripheral.id, service, characteristic, data)
console.log(`Sent: ${data}`)
} catch (error) {
console.log(`Error writing ${characteristic}: ${error}`)
}
}
peripheral
object:
{"advertising": {"isConnectable": true, "localName": "BLE-Waveshare",
"manufacturerData": {"CDVType": "ArrayBuffer",
"bytes": [Array],
"data": "AgEGAwPw/w4JQkxFLVdhdmVzaGFyZQf/mRSchH35AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="},
"serviceData": {}, "serviceUUIDs": ["fff0"],
"txPowerLevel": -2147483648},
"characteristics": [{"characteristic": "fff1", "descriptors": [Array],
"properties": [Object], "service": "fff0"},
{"characteristic": "fff2", "descriptors": [Array],
"properties": [Object], "service": "fff0"}
, {"characteristic": "fff3", "properties": [Object],
"service": "fff0"}], "id": "XX:XX:XX:XX:XX:XX",
"name": "BLE-Waveshare", "rssi": -41,
"services": [{"uuid": "fff0"}]}
I'm calling this function by onPress
prop in Pressable
component. Error occurs only when I trying to write()
something.
I tried to find answer on the internet, but didn't find.
I also tried this function in different variations for example using Promises with .then()
and .catch()
, like so:
const sendCom = async () => {
const data = utf8Encode('1') // convert data to bytes
const service = 'fff0'
const characteristic = 'fff1'
BleManager.retrieveServices(peripheral.id)
.then(() => {
BleManager.startNotification(peripheral.id, service, characteristic)
})
.then(() => {
BleManager.write(peripheral.id, service, characteristic, data)
console.log(`Sent: ${data}`)
})
.catch((error) => {
console.log(`Error writing ${characteristic}: ${error}`)
})
}
The error I getting:
Error writing fff1: Error writing 0000fff1-0000-1000-8000-00805f9b34fb status=3
It will be nice if you explain what status 3
means.
I tried to use writeWithoutResponse() insted of write() and it worked as I expected. So if you have similar issue you can try same thing.