@@ -226,9 +226,14 @@ public void onMethodCall(MethodCall call, Result result) {
226
226
String deviceId = (String )call .arguments ;
227
227
BluetoothGatt gattServer = mGattServers .remove (deviceId );
228
228
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
+
232
237
}
233
238
result .success (null );
234
239
break ;
@@ -559,14 +564,17 @@ public void onReceive(Context context, Intent intent) {
559
564
if (BluetoothAdapter .ACTION_STATE_CHANGED .equals (action )) {
560
565
final int state = intent .getIntExtra (BluetoothAdapter .EXTRA_STATE ,
561
566
BluetoothAdapter .ERROR );
567
+
562
568
switch (state ) {
563
569
case BluetoothAdapter .STATE_OFF :
570
+ log (LogLevel .DEBUG , "[手机蓝牙状态] status: 关闭" );
564
571
sink .success (Protos .BluetoothState .newBuilder ().setState (Protos .BluetoothState .State .OFF ).build ().toByteArray ());
565
572
break ;
566
573
case BluetoothAdapter .STATE_TURNING_OFF :
567
574
sink .success (Protos .BluetoothState .newBuilder ().setState (Protos .BluetoothState .State .TURNING_OFF ).build ().toByteArray ());
568
575
break ;
569
576
case BluetoothAdapter .STATE_ON :
577
+ log (LogLevel .DEBUG , "[手机蓝牙状态] status: 打开" );
570
578
sink .success (Protos .BluetoothState .newBuilder ().setState (Protos .BluetoothState .State .ON ).build ().toByteArray ());
571
579
break ;
572
580
case BluetoothAdapter .STATE_TURNING_ON :
@@ -705,9 +713,6 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
705
713
if (newState == BluetoothProfile .STATE_DISCONNECTED ) {
706
714
if (!mGattServers .containsKey (gatt .getDevice ().getAddress ())) {
707
715
gatt .close ();
708
- }else {
709
- mGattServers .remove (gatt .getDevice ().getAddress ());
710
- gatt .close ();
711
716
}
712
717
}
713
718
invokeMethodUIThread ("DeviceState" , ProtoMaker .from (gatt .getDevice (), newState ).toByteArray ());
0 commit comments