Skip to content

Commit 40a24e6

Browse files
committed
[dev]
1 parent 4b8c99d commit 40a24e6

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

android/src/main/java/com/pauldemarco/flutterblue/FlutterBluePlugin.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,14 @@ public void onMethodCall(MethodCall call, Result result) {
226226
String deviceId = (String)call.arguments;
227227
BluetoothGatt gattServer = mGattServers.remove(deviceId);
228228
if(gattServer != null) {
229-
gattServer.disconnect();
230-
gattServer.close();
231-
gattServer = null;
229+
try {
230+
gattServer.disconnect();
231+
gattServer.close();
232+
gattServer = null;
233+
}catch (Exception e){
234+
log(LogLevel.ERROR, e.getMessage());
235+
}
236+
232237
}
233238
result.success(null);
234239
break;
@@ -559,14 +564,17 @@ public void onReceive(Context context, Intent intent) {
559564
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
560565
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
561566
BluetoothAdapter.ERROR);
567+
562568
switch (state) {
563569
case BluetoothAdapter.STATE_OFF:
570+
log(LogLevel.DEBUG, "[手机蓝牙状态] status: 关闭" );
564571
sink.success(Protos.BluetoothState.newBuilder().setState(Protos.BluetoothState.State.OFF).build().toByteArray());
565572
break;
566573
case BluetoothAdapter.STATE_TURNING_OFF:
567574
sink.success(Protos.BluetoothState.newBuilder().setState(Protos.BluetoothState.State.TURNING_OFF).build().toByteArray());
568575
break;
569576
case BluetoothAdapter.STATE_ON:
577+
log(LogLevel.DEBUG, "[手机蓝牙状态] status: 打开" );
570578
sink.success(Protos.BluetoothState.newBuilder().setState(Protos.BluetoothState.State.ON).build().toByteArray());
571579
break;
572580
case BluetoothAdapter.STATE_TURNING_ON:
@@ -705,9 +713,6 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
705713
if(newState == BluetoothProfile.STATE_DISCONNECTED) {
706714
if(!mGattServers.containsKey(gatt.getDevice().getAddress())) {
707715
gatt.close();
708-
}else{
709-
mGattServers.remove(gatt.getDevice().getAddress());
710-
gatt.close();
711716
}
712717
}
713718
invokeMethodUIThread("DeviceState", ProtoMaker.from(gatt.getDevice(), newState).toByteArray());

0 commit comments

Comments
 (0)