Reputation: 85
I am working on a Bluetooth project in react native, Where I am trying to read data from another devices. Reading data using using the following piece of code
BluetoothSerial.readFromDevice().then((data) => {
console.log(data)
}
which is working perfectly. But i want to use an event listener to collect the data send by some other devices at any random time. I am using a listener found at https://github.com/rusel1989/react-native-bluetooth-serial/issues/16 answered by @andrescheca. the code look like the following
BluetoothSerial.withDelimiter('\r').then(() => {
Promise.all([
BluetoothSerial.isEnabled(),
BluetoothSerial.list()
])
.then((values) => {
const [ isEnabled, devices ] = values
this.setState({ isEnabled, devices })
})
BluetoothSerial.on('bluetoothEnabled', () => console.log('Bluetooth enabled'))
BluetoothSerial.on('bluetoothDisabled', () => console.log('Bluetooth disabled'))
BluetoothSerial.on('read', (data) => {
console.log(`DATA FROM BLUETOOTH: ${data.data}`);
Toast.show(data.data);
})
BluetoothSerial.on('error', (err) => console.log(`Error: ${err.message}`))
BluetoothSerial.on('connectionLost', () => {
if (this.state.device) {
console.log(`Connection to device ${this.state.device.name} has been lost`)
}
this.setState({ connected: false })
})
});
All the listeners i.e bluetoothEnabled, bluetoothDisabled are firing when i am enabling and disabling Bluetooth. But the read Listener is not firing on receiving data. I am assuming that it should fire as soon as another device send some data. I would really appreciate if someone could help me with this issue.
Upvotes: 1
Views: 744
Reputation: 85
I solved this issue just adding \r at the end of data that i was trying to send to my device from a Linux server. Thanks @kendavidson for your help.
Upvotes: 1