Skip to content

Commit

Permalink
fix: Bluetooth connection failed: read failed, socket might closed or… (
Browse files Browse the repository at this point in the history
#93)

* fix: Bluetooth connection failed: read failed, socket might closed or timeout, read ret: -1

* add missing param and fix catch error

* fix: java.string error
  • Loading branch information
shelomohkhual authored Sep 16, 2024
1 parent f58d0dc commit 9ffe09c
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,19 @@ public void selectDevice(PrinterDeviceId printerDeviceId, Callback successCallba
if(device.getAddress().equals(blePrinterDeviceId.getInnerMacAddress())){

try{
connectBluetoothDevice(device);
connectBluetoothDevice(device, false);
successCallback.invoke(new BLEPrinterDevice(this.mBluetoothDevice).toRNWritableMap());
return;
}catch (IOException e){
e.printStackTrace();
errorCallback.invoke(e.getMessage());
return;
} catch (IOException e) {
try {
connectBluetoothDevice(device, true);
successCallback.invoke(new BLEPrinterDevice(this.mBluetoothDevice).toRNWritableMap());
return;
} catch (IOException er) {
er.printStackTrace();
errorCallback.invoke(er.getMessage());
return;
}
}
}
}
Expand All @@ -144,11 +150,22 @@ public void selectDevice(PrinterDeviceId printerDeviceId, Callback successCallba
return;
}

private void connectBluetoothDevice(BluetoothDevice device) throws IOException{
private void connectBluetoothDevice(BluetoothDevice device, Boolean retry) throws IOException {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
this.mBluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
this.mBluetoothSocket.connect();
this.mBluetoothDevice = device;//最后一步执行

if (retry) {
try {
this.mBluetoothSocket = (BluetoothSocket) device.getClass()
.getMethod("createRfcommSocket", new Class[] { int.class }).invoke(device, 1);
} catch (Exception e) {
e.printStackTrace();
}
} else {
this.mBluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
this.mBluetoothSocket.connect();
}

this.mBluetoothDevice = device;// 最后一步执行

}

Expand Down

0 comments on commit 9ffe09c

Please sign in to comment.