How to read BLE Peripheral Characteristics in react native using react-native-ble-plx
I'm developing an application which scans for BLE devices(Peripherals), then connect to them, read their services and characteristics. For now, I was able to connect to an iPhone which acting as a Peripheral device with certain characteristics and also was able to connect to Fitbit Versa Watch to read the heart sensor data. Although I was able to discover their services, I wasn't able to extract more information from the services like ServiceUUId and their characteristics.
Below is my code is written in react native.
scanAndConnect() {
console.log("Scanning Started");
this.manager.startDeviceScan(null, null, (error, device) => {
if (error) {
// Handle error (scanning will be stopped automatically)
console.log("Error in scanning devices:", error);
return
}
// Check if it is a device you are looking for based on advertisement data
// or other criteria.
console.log("Detected Device Details:", device.id, device.name);
// ||device.localName === 'BLEPeripheralApp')
if (device.name === 'Versa Lite'){ //
// Stop scanning as it's not necessary if you are scanning for one device.
console.log("Device Found, Stopping the Scan.");
console.log("Connecting to:",device.name)
this.manager.stopDeviceScan();
device.connect()
.then((device) => {
// this.info("Discovering services and characteristics")
console.log("Connected...Discovering services and characteristics");
return device.discoverAllServicesAndCharacteristics()
})
.then((device) => {
console.log('Services and characteristics discovered');
//return this.testChar(device)
const services = device.services()
console.log(services);
return device.readCharacteristicForService(services)
// device.readCharacteristicForService("abbaff00-e56a-484c-b832-8b17cf6cbfe8")
// this.info("Setting notifications")
//return this.setupNotifications(device)
})
.then(() => {
const characteristicsData = device.readCharacteristicForService();
console.log(characteristicsData);
//this.info("Listening...")
}, (error) => {
console.warn(error.message);
// this.error(error.message)
})
}
});
}
How do I extract the serviceUUId from the Service method and also read the characteristics of that service? For my iPhone peripheral, I have two mutable characteristics that I should be able to read and write. How do I read them from service to characteristics to the real value?
Any help/suggestions are much appreciated.
Thanks.
Do you face any issues?
@dariuszseweryn Thank you for responding. I was able to get the services and characteristics now. Have modified the above code and seems working fine. But i have a question how do i read the data of characteristics of each service. I have Fitbit Versa Lite Watch trying to connect to it and read the heart rate. I was able to fetch the services and its characteristics, but not getting how do i read the characteristics value. We have read, write, writeWithOutResponse, writeWithResponse i'm confused with this. if i want to read the heart rate of a watch how exactly do i need to proceed once the services and characteristics has been discovered. Kindly Help. Thank you.
I've got the same issue. I've read the GATT Services (https://www.bluetooth.com/specifications/gatt/services/). But I don't know how to get the specified service value like Heart Rate, Running Speed and Cadence, etc.
@lnvtphu It all depends on the device which you are trying read the characteristics, some of the devices like (Samsung Gear, Fibit Versa Lite)doesn't expose their heart rate services and characteristics. I was able to read the characteristics, but couldn't find the one related to Heart Rate as they aren't exposing their services. But Make sure you have 0*180D in any of the services UUID.
Below code should help you getting the characteristics for the services.
async readData(device) {
const services = await device.services();
console.log("Services:",services);
const characteristics = await services[1].characteristics();
console.log("Characteristics:",characteristics);
characteristics[0].monitor((err, update) => {
if (err) {
console.log(`characteristic error: ${err}`);
console.log(JSON.stringify(err));
} else {
console.log("Is Characteristics Readable:",update.isReadable);
console.log("Heart Rate Data:",base64.decode(update.value));
// const readCharacteristic = await device.readCharacteristicForService(userDataServiceUUID,
heightCharacteristicUUID); // assuming the device is already connected
// var data = new Uint16Array(base64.decode(update.value));
const heartRateData = Buffer.from(update.value, 'base64').readUInt16LE(0);
console.log("Heart Beats:",heartRateData);
}
});
}
@Madhu02 many thanks. At the moment, I try to write the Native Module to integrate the Band SDK provide by customer.
@Madhu02 Hello Brother..
if you have idea about to connect bluetooth printer and print the slip.
i have issue with bluetooth printer i cannot write data.please help me to get print slip
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.