[Help] Cannot read from device, but can write to it
See original GitHub issueMobile Device Environment Provide a list of operating systems on which this issue is relevant.
- Device: Android
- OS: Android 11
Application Environment Provide information about your development environment:
- React Native version: v0.63.4
- RN Bluetooth Classic version: 1.60.0-rc.16
Describe the bug I have a Raspberry Pi 3, on which a bluetooth server (Bluedot btcomm) is running. I can send and receive data perfectly from the Android Serial Bluetooth Terminal application. But with my React Native app, I can only send data. I cannot read data from the device. My code is shown below:
let status = await RNBluetoothClassic.requestBluetoothEnabled();
if (status) {
try {
let unpaired = await RNBluetoothClassic.startDiscovery();
let rpi = unpaired.find((o) => o.name === "raspberrypi");
if (rpi) {
let conn = await RNBluetoothClassic.getBondedDevices();
let connStatus = conn.find((o) => o.name === "raspberrypi");
if (!connStatus) {
console.log("Pairing....");
await RNBluetoothClassic.pairDevice(rpi.id);
}
try {
device = await RNBluetoothClassic.connectToDevice(rpi.id, {});
await device.connect({});
await device.write("Foo", "ascii"); // 'Foo' is received in raspberry pi
console.log(await device.available()); // Logs '0'
await device.onDataReceived((res) => console.log(res)); // Does not log anything
let readData = await device.read();
console.log(readData); // Logs 'null'
} catch (e) {
console.log(e);
}
} else {
console.log("Bluetooth error!");
}
} catch (e) {
console.log(e);
}
}
Please let me know if I am doing anything incorrectly.
Thank you.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Unable to write to serial device but able to read from it
I am using select to wait for the IO resource to become available ... But even waiting 20 seconds, the device is still...
Read more >I can do the WRITE but i can´t do the READ from a SERIAL port
I need to read/write from a pressure control (from GDS instruments) that has a SERIAL port (RS-232). I'm using LABVIEW 7.1, and Windows...
Read more >How To Fix 'can't read from the source file or disk' Errors - Alphr
It is an easy one to fix though. Right-click the file you are trying to copy and select Properties.
Read more >Unable to access (read and write) USB flash drive in Windows
Unable to access (read and write) USB flash drive in Windows 10. I have tried multiple solutions to troubleshoot or even just to...
Read more >How to Fix SD Card Won't Read /SD Card Cannot Read error
It can be a USB port issue. Some laptops have more than one USB port – front and back. Try to access the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@hemanth-kumarv @kenjdavidson I have implemented this library in my app to establish bluetooth connection with another device.(which is an android phone in my case) However I am unable to connect to another device. In the above code snippet shared by @hemanth-kumarv , device = await RNBluetoothClassic.connectToDevice(rpi.id, {}); await device.connect({});
connectToDevice accepts 2 params which are address and options.
This is the definition of this method in the react-native-bluetooth-classic lib.
What do we mean by the comment section above.? What should I pass as options in connectToDevice method.
Can you share a proper example of how can we connect to a device?
Not sure what to tell ya man, the error is right there
The socket on the other side (
ServerSocket
) is:as I mentioned before, in order to make a connection, you need something waiting on the other side to accept this connection.
accept
doesn’t work, but how doesn’t it work? you have provided no errors.You also need to have an understanding of the language used:
You’re not connected (ie. the error above) so you’d never get the device back from
isDeviceConnected()
. I can tell you that theaccept
method works just fine, it’s being used in the sample/development application and I’ve used it to communicate between two Android devices.