Open zhanguangao opened 7 years ago
Do you have to run this with a setInterval
to get updated data from the device?
How can I read continuously from the bluetooth device?
@prashant45 I'd much rather get an event listener setup but I ended up using a setInterval()
as a quick and dirty way to keep reading the latest data.
@joshuapinter Thanks that worked for now 👍
@joshuapinter did you manage to set up the event listener?
@prashant45 No. Still using setInterval()
.
perhaps this could be of help react-native-event-listeners.
@prashant45 It'll be relatively easy to do but it's just not necessary for what we're doing. The setInterval works fine for now. This isn't my repository but you should be able to get a working example going quite easily. I believe the library even supports it but it's not very well documented.
Yes, the library is not well documented. For me, setInterval()
is not working in a long run. I also need to do a lot of stuff after BluetoothSerial.readFromDevice().then((data) => {...});
and app stops responding probably because it does not complete doing the task in assigned interval of 2 seconds.
Hi, I agree, the library is not very well documented but I was reading the Java code and found that BluetoothSerial.withDelimiter
exists. I used it with the carriage return character \r
and BluetoothSerial.on('read')
now works!:
BluetoothSerial.withDelimiter('\r').then(() => {
Promise.all([
BluetoothSerial.isEnabled(),
BluetoothSerial.list(),
]).then(values => {
const [isEnabled, devices] = values;
...
});
BluetoothSerial.on('read', data => {
console.log(`DATA FROM BLUETOOTH: ${data.data}`);
});
});
That way you don't have to use setInterval()
for reading data.
I can not read hex data in normal mode, but in debugger mode I can do it. change the function in RCTBluetoothSerialModule.java
will fix it
if use withDelimiter
change the onData
function like that:
void onData (String data){
mBuffer.append(data);
String completeData = readUntil(this.delimiter);
if (completeData != null && completeData.length() > 0) {
WritableMap params = Arguments.createMap();
WritableArray writableArray = Arguments.createArray();
byte[] bytes = completeData.getBytes();
for(int i=0;i<bytes.length;i++){
writableArray.pushInt((int)bytes[i]);
}
params.putArray("arr", writableArray);
params.putString("data", completeData);
sendEvent(DEVICE_READ, params);
}
}
and in you js
file, use Buffer.from(data.arr)
get string data .
if use readFromDevice
function, change readFromDevice
like that
public void readFromDevice(Promise promise) {
if (D) Log.d(TAG, "Read");
int length = mBuffer.length();
String data = mBuffer.substring(0, length);
mBuffer.delete(0, length);
WritableArray writableArray = Arguments.createArray();
byte[] bytes = data.getBytes();
for(int i=0;i<bytes.length;i++){
writableArray.pushInt((int)bytes[i]);
}
promise.resolve(writableArray);
}
it will get the Buffer array, not the string data. need to use Buffer.from(data)
Hi, I agree, the library is not very well documented but I was reading the Java code and found that
BluetoothSerial.withDelimiter
exists. I used it with the carriage return character\r
andBluetoothSerial.on('read')
now works!:BluetoothSerial.withDelimiter('\r').then(() => { Promise.all([ BluetoothSerial.isEnabled(), BluetoothSerial.list(), ]).then(values => { const [isEnabled, devices] = values; ... }); BluetoothSerial.on('read', data => { console.log(`DATA FROM BLUETOOTH: ${data.data}`); }); });
That way you don't have to use
setInterval()
for reading data.Hi, I agree, the library is not very well documented but I was reading the Java code and found that
BluetoothSerial.withDelimiter
exists. I used it with the carriage return character\r
andBluetoothSerial.on('read')
now works!:BluetoothSerial.withDelimiter('\r').then(() => { Promise.all([ BluetoothSerial.isEnabled(), BluetoothSerial.list(), ]).then(values => { const [isEnabled, devices] = values; ... }); BluetoothSerial.on('read', data => { console.log(`DATA FROM BLUETOOTH: ${data.data}`); }); });
That way you don't have to use
setInterval()
for reading data.
@andrescheca I am doing the same as you have done but still, the event listener is not firing in my case. Kindly help.
Hey @Rehan-Rehman-Punjwani, it's been a long time since I used this library but I can try to help you. Are you able to connect to the device within your React Native app? Have you tried using an app like Bluetooth Terminal in Android to read and send data to the device?
@andrescheca Thank you for your reply, yes I have.. it is working fine with reading and writing data to the server running on my ubuntu. I am not aware of Bluetooth Terminal btw
What about connecting to the bluetooth device from your React Native app?
@andrescheca Yes, it is working.
Have you tried testing it in debugger mode as @wenchao1020 suggested and check if you are receiving data in the Java's onData method?
Have you tried testing it in debugger mode as @wenchao1020 suggested and check if you are receiving data in the Java's onData method?
@andrescheca thank you for the help. I have just solved the issue. I added '\r' at the end of data that i was sending and it worked.
@Rehan-Rehman-Punjwani That's really great!
Hello there, I have the same problem, I am creating a react native chat application that communicates with a HC05 bluetooth module connected to an STM32, and my main problem is to receive instant messages from the module. I already read all your interactions in this forums and tried using the event listener provided by the library but it doesn't trigger instantly the messages, and when it triggers, it duplicates the messages multiple times ? Any solution please ?
Hello Guys, if you have any idea how to retrieve the data from a Bluetooth paired device(ex: as we connected to a Philips CPAP machine) in react-native
BluetoothSerial am able connect device but not able to retrieve the data
You can read using readFromDevice:
BluetoothSerial.readFromDevice().then((data) => {console.log(data)});