Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Bluetooth connection failed: read failed, socket might closed or… #93

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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