【问题标题】:Why is the socket broken after a few seconds, Bluetooth Serial Ionic 3?为什么几秒钟后插座坏了,蓝牙串行离子3?
【发布时间】:2017-11-19 14:26:41
【问题描述】:

代码有效,但在建立连接后,它只保持连接几秒钟。

这是我的代码:

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

我不知道问题是什么,我没有找到任何关于这个问题的信息。

欢迎任何帮助。

【问题讨论】:

  • 删除了我的 cmets 进行清理!也会删除这个。

标签: android cordova typescript bluetooth ionic3


【解决方案1】:

我已经解决了,是我手机的问题,我只需要清除蓝牙缓存并重新启动手机。

【讨论】:

    猜你喜欢
    • 2011-09-13
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2021-08-07
    • 2017-12-14
    相关资源
    最近更新 更多