Bluetooth serial ionic 3 check if valid connection

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');

did you find the solution ??

No, I just rely on the subscribe method throwing an error in 3-4 secs. I also have a timer set up to throw an error if reading response from bluetooth device takes longer than 5 secs. Its not a great solution as sometimes when the phone is too busy with other tasks this error gets thrown (when its not actually a drop in connection) but for our project this works okay so far.

1 Like