My app is set up to write to and read from a bluetooth device. Before a write function is called, I use the isConnected() function to check the status of the connection before writing. The problem is this function returns a valid connection even if there is none (I turn off the BT device to check this) Moreover the write function when called also writes to the bt device (without a connection) and calls the success handler of the write method. It takes about 4-5 seconds for the bluetooth connection status to be updated by the observable from the connect() method. and if a write method is called in this period I get this unreliable behaviour.
How can I handle this? Or is there something wrong with my set up?
Here is my code -
writeData(data) {
this.bluetoothSerial.isConnected().then(() => {
this.bluetoothSerial.write(data).then((success) => {
console.log('written: ' + data + ' success ' + success);
alert(data + " Written to device!");
},
(error) => {
alert('error writing: ' + error);
});
}, (error) => {
console.log('Operation not completed as device not connected');
alert('Device is not connected');
});
}