Problems with read subscription
See original GitHub issueBefore starting this I want to shortly mention two things. Thanks for creating and maintaining this library and I’m pretty sure the fault is on my side. I just don’t know what I’m doing wrong 🙈
Mobile Device Environment
- Device: Android
- OS: API 29
Application Environment Provide information about your development environment:
- React Native version: 0.63.4
- RN Bluetooth Classic version: 1.60.0-rc.17
Describe the bug I have an app that can connect to python client on a laptop and send messages but does only receives the messages from the python client once the connection is closed. Additionally I get a weird error when I try to remove the read subscription. I’m not sure if both problems are related.
Below you can find the two important files. When I run readSubscription.remove()
I receive the following error:
Cannot read from DEVICE_READ, not currently connected
bluetooth.js
const onDeviceDisconnect = async (
device,
readSubscription,
dispatch,
changeDeviceConnectionStatus,
) => {
readSubscription.remove()
changeDeviceConnectionStatus('disconnected')
dispatch(setDevice({ device: null }))
console.log('disconnected device: ' + device.name)
}
export const sendMessageToDevice = async (device, msg) => {
const msgStr = createBluetoothMessage(msg)
try {
await RNBluetoothClassic.writeToDevice(device.address, msgStr)
} catch (error) {
console.error('error reply message' + msgStr)
}
}
export const connectBluetoothDevice = async (
device,
dispatch,
changeDeviceConnectionStatus,
onDataReceived,
) => {
try {
changeDeviceConnectionStatus('connecting')
let connection = await device.isConnected()
// check if already connected
if (!connection) {
connection = await device.connect({
DELIMITER: Config.BLUETOOTH_DELIMITER,
})
}
changeDeviceConnectionStatus('connected')
let readSubscription = device.onDataReceived(data => onDataReceived(data))
RNBluetoothClassic.onDeviceDisconnected(device =>
onDeviceDisconnect(
device,
readSubscription,
dispatch,
changeDeviceConnectionStatus,
),
)
} catch (error) {
console.error(error)
changeDeviceConnectionStatus('connection failed')
dispatch(setDevice({ device: null }))
}
}
Screen.js
const ExampleContainer = () => {
const device = useSelector(store => store.bluetooth.device)
const [connectionStatus, setConnectionStatus] = useState('not connected')
const dispatch = useDispatch()
async function onDataReceived(data) {
await data
console.log(data)
}
async function onSendCommmand(command) {
await sendMessageToDevice(device, { type: 'command', command: command })
}
useEffect(() => {
async function connectDevice(device) {
await connectBluetoothDevice(
device,
dispatch,
setConnectionStatus,
onDataReceived,
)
}
if (device !== null) {
connectDevice(device)
}
}, [device, dispatch])
return (...)
}
}
export default ExampleContainer
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (6 by maintainers)
So the problem was related to specific kind of message we send.
Right on man, I’m glad you’ve got it up and going!