-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to read data? #112
Comments
You can use the method BluetoothSerial.readFromDevice() to read data sent by device. Use a setInterval and execute or you can add a event listener DEVICE_READ, consult the file RTCBluetoothSerialModule.java on node_modules |
can you show me a runnable case?Thank you so much |
Sure, for exemple: import BluetoothSerial from 'react-native-bluetooth-serial'; receiveDataFromDevice = async () => { and in your .js file execute a setInterval repeatedly. notes: The library BluetoothSerial contains some methods in your interface React, but the method readFromDevice is in the RCT .java file on node_modules / react-native-bluetooth-serial ... |
Thank you so much!But where should I place this code...? setInterval call what?Can you show me more code ,I was stuck in this problem for a long time |
Hi, @JulioCVaz I am trying to read data from a microcontroller that is sending data via a Bluetooth module (HC05). I see that it has been said that we can use the BluetoothSerial.readFromDevice(). I was wondering how I could actually write implementable code in my .js file using the event listener? Also, I wanted to ask if we can use all the functions that are available in the file RTCBluetoothSerialModule.java located in node_modules? |
@ramacha7 , Hello friend! you can see in this file, methods that have a "@ReactMethod" you can implements in the your .js code. And in this file you can see that method to use a ListenerEvent https://github.com/rusel1989/react-native-bluetooth-serial/blob/master/index.js Example for implementation follow this file: good luck :) |
@JulioCVaz Thank you so much for the direction. I also wanted to ask if I can directly use the readFromDevice() function, because I need to use '\r' as my delimiter and I don't think it uses that as the delimiter. |
@ramacha7 Yeah, you can! Maybe you can need adjust the data received using \r |
@JulioCVaz I tried setting up an event and using the readFromDevice() function. However I am not able to read the data being sent. This is my code so far.
This is the result I get after printing readdata (this is the state variable holding the current data that is read) : |
@ramacha7 You should use async await cuz this method readFromDevice return a promise. example: const handleRead = async () => { |
@JulioCVaz . I tried the code you suggested and I was still not able to receive the data. the event is being triggered however, I cant see the data. This is the output: [Thu Feb 25 2021 14:51:09.913] LOG Permission is OK |
@ramacha7 you pair the device with your cellphone? you only can read the data only if the devices is paired. |
@JulioCVaz Oh are you talking about the function pairdevice()? Are you referring to this function? I pair the device with my cellphone in the settings of my android. But I don't write any code to do it. Also how can I access or use this function. I am only asking because it is declared as a private function
|
@ramacha7 I think that how you pair into android settings should work. But I would try pair using pairDevice() check if has another behavior. |
@JulioCVaz I was wondering if you had any code where you implemented connecting and receiving messages from a device to the app. It would be really helpful. I will try using the pairdevice() for now. |
@JulioCVaz hello, am able to pair the device but not able receive any data from it using BluetoothSerial.readFromDevice(). result -- entered read function |
Hi @taj567 , how are u? Maybe that way it will work readFromDevice = async (id) => {
const res = await BluetoothSerial.readFromDevice(id)
console.log(res)
} Another point: #112 (comment) I haven't worked with this library for a while, but, I remember that to |
Hi @JulioCVaz, am fine & Hope you to? able to retrieve---- -----------"bytes": [2, 1, 0, 11, 9, 80, 82, 32, 66, 84, 32, 70, 67, 54, 70, 7, 255, 0, 0, 0, 0, 0, 0, 17, 7, 243, 89, 244, 161, 144, 225, 252, 174, 244, 73, 9, 248, 127, 134, 83, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ---FYI after decoding the data above we are able to see only the device name. Any idea |
@taj567 I'm fine :] Congratz to connected with ble-manager and read data 👍 I think that about your question, you can try convert this array buffer to a string or valid value. import { Buffer } from 'buffer'
const data = DATA_FROM_BLE_MANAGER
const bufferToString = Buffer.from(data.ArrayBuffer).toString('base64') I see that this library is necessary to convert the received data buffer https://github.com/innoveit/react-native-ble-manager#readperipheralid-serviceuuid-characteristicuuid BleManager.read(
"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
)
.then((readData) => {
// Success code
console.log("Read: " + readData);
const buffer = Buffer.Buffer.from(readData); //https://github.com/feross/buffer#convert-arraybuffer-to-buffer
const sensorData = buffer.readUInt8(1, true);
})
.catch((error) => {
// Failure code
console.log(error);
}); or follow this thread in the stackoverflow to create a buffer converter: https://stackoverflow.com/a/37902334/10608968 |
@JulioCVaz Hi, |
@JulioCVaz by using this --BleManager.read(
}) ---getting read failed error--- |
@taj567 Alright, I think that to read the data from your device, maybe u can use another library more robust like https://github.com/dotintent/react-native-ble-plx I search the specification about your machine https://www.cpapdirect.com/cpap-machines/philips-respironics-dreamstation-auto-cpap-with-heated-humidifier but I don't find if is a machine with BLE(Bluetooth low energy) or a Normal Bluetooth connect. This is diferent to read of data. |
@JulioCVaz Yes, it is an updated one!. |
@JulioCVaz we are able to pair the device and connect but we need to get the extra information related to the device. |
@taj567 awesome! Try to use the |
Hi, @JulioCVaz How are you? Can I retrieve data from the digital hemoglobin meter poc-30 point of care device via Bluetooth using react-native-bluetooth-serial, or do I need to use Android code? |
Use of read?
How to get Bluetooth information?
The text was updated successfully, but these errors were encountered: