Although the code appears to be functioning correctly, it loses connection shortly after establishing it.
This snippet contains the relevant code:
import { Component } from '@angular/core';
import { Platform, NavController, ToastController, Refresher } from 'ionic-angular';
import { BluetoothSerial } from '@ionic-native/bluetooth-serial';
import { Observable } from 'rxjs';
@Component({
selector: 'page-opciones',
templateUrl: 'opciones.html'
})
export class OpcionesPage {
li_devices: Array<Object> = [];
loading: any;
mostrarSpiner = true;
constructor(
public platform: Platform,
public toastCtrl: ToastController,
public navCtrl: NavController,
public bluetoothSerial: BluetoothSerial) {
platform.ready().then(() => {
this.searchBluetooth().then((successMessage: Array<Object>) => {
console.log("Devices " + JSON.stringify(successMessage));
this.li_devices = successMessage;
this.mostrarSpiner = false;
},
failureMessage => {
const toast = this.toastCtrl.create({
message: JSON.stringify(failureMessage),
duration: 3000
});
toast.present();
this.mostrarSpiner = false;
});
});
}
search_bluetooth(){
return new Promise((resolve, reject) => {
this.bluetoothSerial.isEnabled().then(success =>{
this.bluetoothSerial.discoverUnpaired().then(data => {
if (data.length > 0){
resolve(data);
} else {
reject('No devices found');
}
}).catch((error) => {
console.log("Error " + JSON.stringify(error));
reject('Bluetooth not available on this platform');
});
}, failure => {
reject('Bluetooth not available');
});
});
}
refresh_bluetooth(refresher: Refresher){
if (refresher){
this.searchBluetooth().then((successMessage: Array<Object>) => {
this.li_devices = [];
console.log("Devices " + JSON.stringify(successMessage));
this.li_devices = successMessage;
refresher.complete();
},
failureMessage => {
const toast = this.toastCtrl.create({
message: JSON.stringify(failureMessage),
duration: 3000
});
toast.present();
refresher.complete();
});
}
}
check_connection(selection){
this.bluetoothSerial.isConnected().then(isConnected => {
const toast = this.toastCtrl.create({
message: "Already connected",
duration: 3000
});
toast.present();
}, notConected => {
this.connect(selection["address"]);
});
}
connect(mac_address: string){
console.log("Connecting to " + mac_address);
this.bluetoothSerial.connect(mac_address).subscribe((data: Observable<string>) => {
console.log("Connect " + JSON.stringify(data));
this.bluetoothSerial.available().then(data =>{
console.log("Available " + JSON.stringify(data));
this.bluetoothSerial.read().then(data =>{
console.log("Read " + JSON.stringify(data));
});
});
}, fail => {
console.log("Disconnected " + JSON.stringify(fail));
});
}
message: string = "";
sendMessages(){
this.bluetoothSerial.isConnected().then(isConnected => {
this.bluetoothSerial.write(this.message);
const toast = this.toastCtrl.create({
message: "Message sent",
duration: 3000
});
toast.present();
}, notConected => {
const toast = this.toastCtrl.create({
message: "You are not connected",
duration: 3000
});
toast.present();
});
}
}
Below is the output for reference:
06-16 12:42:18.170 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62200)] "Devices [{"name":"JLOZOYABT","address":"98:D3:33:80:AD:E5","id":"98:D3:33:80:AD:E5","class":7936},{"address":"65:68:12:29:A9:2B","id":"65:68:12:29:A9:2B","class":7936}]", source: file:///android_asset/www/build/main.js (62200)
...
[Various log messages regarding connectivity and disconnection]
...
The issue at hand remains unclear, as no relevant information has been discovered thus far.
Any assistance or insights would be greatly appreciated.