1
該代碼有效,但建立連接後,它僅保持連接幾秒鐘。爲什麼幾秒鐘後藍牙串行Ionic 3會損壞插座?
這是我的代碼:
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.buscar_bluetooth().then((successMessage: Array<Object>) => {
console.log("Dispositivos " + 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;
});
});
}
buscar_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 se encontraron dispositivos');
}
}).catch((error) => {
console.log("Error " + JSON.stringify(error));
reject('Bluetooth no disponible en esta plataforma');
});
}, failure => {
reject('Bluetooth no disponible');
});
});
}
refresh_bluetooth(refresher: Refresher){
if (refresher){
this.buscar_bluetooth().then((successMessage: Array<Object>) => {
this.li_devices = [];
console.log("Dispositivos " + 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();
});
}
}
revisar_conexion(seleccion){
this.bluetoothSerial.isConnected().then(isConnected => {
const toast = this.toastCtrl.create({
message: "Ya esta conectado",
duration: 3000
});
toast.present();
}, notConected => {
this.conectar(seleccion["address"]);
});
}
conectar(direccion_mac: string){
console.log("Conectando con " + direccion_mac);
this.bluetoothSerial.connect(direccion_mac).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("Desconectado " + JSON.stringify(fail));
});
}
mensaje: string = "";
enviarMensajes(){
this.bluetoothSerial.isConnected().then(isConnected => {
this.bluetoothSerial.write(this.mensaje);
const toast = this.toastCtrl.create({
message: "Mensaje enviado",
duration: 3000
});
toast.present();
}, notConected => {
const toast = this.toastCtrl.create({
message: "No estas conectado",
duration: 3000
});
toast.present();
});
}
}
這是輸出:
06-16 12:42:18.170 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62200)] "Dispositivos [{"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)
06-16 12:46:30.630 17996-17996/io.ionic.starter W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
06-16 12:46:44.250 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62268)] "Conectando con 98:D3:33:80:AD:E5", source: file:///android_asset/www/build/main.js (62268)
06-16 12:46:44.260 17996-18342/io.ionic.starter I/BluetoothSerial: MESSAGE_STATE_CHANGE: 2
06-16 12:46:44.260 17996-18342/io.ionic.starter I/BluetoothSerial: BluetoothSerialService.STATE_CONNECTING
06-16 12:46:44.260 17996-25926/io.ionic.starter I/BluetoothSerialService: BEGIN mConnectThread SocketType:Secure
06-16 12:46:44.260 17996-25926/io.ionic.starter I/BluetoothSerialService: Connecting to socket...
06-16 12:46:44.260 17996-25926/io.ionic.starter W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
06-16 12:46:45.710 17996-25926/io.ionic.starter I/BluetoothSerialService: Connected
06-16 12:46:45.710 17996-25950/io.ionic.starter I/BluetoothSerialService: BEGIN mConnectedThread
06-16 12:46:45.710 17996-18342/io.ionic.starter I/BluetoothSerial: JLOZOYABT
06-16 12:46:45.710 17996-18342/io.ionic.starter I/BluetoothSerial: MESSAGE_STATE_CHANGE: 3
06-16 12:46:45.710 17996-18342/io.ionic.starter I/BluetoothSerial: BluetoothSerialService.STATE_CONNECTED
06-16 12:46:45.720 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62270)] "Connect "OK"", source: file:///android_asset/www/build/main.js (62270)
06-16 12:46:45.730 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62272)] "Available 0", source: file:///android_asset/www/build/main.js (62272)
06-16 12:46:45.730 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62274)] "Read """, source: file:///android_asset/www/build/main.js (62274)
06-16 12:47:11.000 17996-25950/io.ionic.starter E/BluetoothSerialService: disconnected
java.io.IOException: bt socket closed, read return: -1
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:437)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
at java.io.InputStream.read(InputStream.java:162)
at com.megster.cordova.BluetoothSerialService$ConnectedThread.run(BluetoothSerialService.java:451)
06-16 12:47:11.000 17996-17996/io.ionic.starter I/chromium: [INFO:CONSOLE(62278)] "Desconectado "Device connection was lost"", source: file:///android_asset/www/build/main.js (62278)
06-16 12:47:11.010 17996-18342/io.ionic.starter I/BluetoothSerial: MESSAGE_STATE_CHANGE: 0
06-16 12:47:11.010 17996-18342/io.ionic.starter I/BluetoothSerial: BluetoothSerialService.STATE_NONE
06-16 12:47:11.010 17996-18342/io.ionic.starter I/BluetoothSerial: MESSAGE_STATE_CHANGE: 0
06-16 12:47:11.010 17996-18342/io.ionic.starter I/BluetoothSerial: BluetoothSerialService.STATE_NONE
06-16 12:47:11.010 17996-18342/io.ionic.starter I/BluetoothSerial: MESSAGE_STATE_CHANGE: 0
06-16 12:47:11.010 17996-18342/io.ionic.starter I/BluetoothSerial: BluetoothSerialService.STATE_NONE
我不知道是什麼問題,我還沒有發現有關此問題的任何信息。
任何幫助,歡迎。
刪除我的意見清理!也會刪除這個。 – Zimano