2015-10-20 72 views
3

我有两个连接到的设备。当我离开应用程序时,我正在与设备断开连接。两者都经历了相同的过程,但有时其中一个设备保持连接,直到我强制关闭应用程序。如何确保应用程序与蓝牙LE设备断开连接?

我有设备上的指示灯确认它仍然认为它的连接和应用程序的其他实例无法连接到它,直到我强迫关闭第一个。 在下面的日志中,第一个列出的设备保持连接状态。

//call gatt.disconnect(); 
BluetoothGatt: cancelOpen() - device: F0:3D:A0:04:CA:E7 
BluetoothGatt: onClientConnectionState() - status=0 clientIf=7 device=F0:3D:A0:04:CA:E7 

//wait for connection state change callback then call gatt.close(); 
BluetoothGatt: close() 
BluetoothGatt: unregisterApp() - mClientIf=7 

//call gatt.disconnect(); 
BluetoothGatt: cancelOpen() - device: FF:A9:CA:EF:08:A4 
BluetoothGatt: onClientConnectionState() - status=0 clientIf=5 device=FF:A9:CA:EF:08:A4 

//wait for connection state change callback then call gatt.close(); 
BluetoothGatt: close() 
BluetoothGatt: unregisterApp() - mClientIf=5 

在我打电话给gatt.close()之后,我抓住BluetoothManager并在连接的设备列表中查找我的设备。它在列表中。

BluetoothGattCallback

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { 
    super.onConnectionStateChange(gatt, status, newState); 
    final String address = gatt.getDevice().getAddress(); 
    if (newState == BluetoothProfile.STATE_CONNECTED) { 
     onConnected(gatt, address); 
    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { 
     onDisconnected(gatt, address); 
    } 
} 

private void onDisconnected(BluetoothGatt gatt, String address) { 
    Log.v(TAG, address + " disconnected"); 
    if (devices.containsKey(address)) { 
     devices.get(address).setState(Connection.STATE_DISCONNECTED); 
     connect(gatt.getDevice()); 
    } else { 
     gatt.close(); 
     verifyDisconnect(gatt, address); 
    } 
} 

private void verifyDisconnect(BluetoothGatt gatt, String address) { 
    BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); 
    final List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT); 
    for (final BluetoothDevice device : devices) { 
     if (device.getAddress().equals(address)) { 
      Log.d(TAG, address + " is still connected!!!!"); 
      addCommand(new DisconnectCommand(gatt)); 
      break; 
     } 
    } 
} 

这个类被添加到命令队列。

public class DisconnectCommand extends BluetoothCommand { 

    private final BluetoothGatt gatt; 

    public DisconnectCommand(BluetoothGatt gatt) { 
     this.gatt = gatt; 
     priority = BluetoothCommand.DISCONNECT_PRIORITY; 
    } 

    public boolean execute() { 
     gatt.disconnect(); 
     return true; 
    } 
} 
+0

您看到该列表中的设备,尽管关闭GATT分贝的原因,是Android的蓝牙堆栈,缓存了它。看到[这](http://stackoverflow.com/questions/22596951/how-to-programmatically-force-bluetooth-low-energy-service-discovery-on-android),可能会给你一个线索。 – t0mm13b

+0

只有当设备的指示灯也亮时,才会显示在列表中。该解决方案适用于发现不断线的服务。 –

+0

您是否在调用'close()'之前调用了BluetoothGatt的'disconnect()'方法? – t0mm13b

回答

0

尝试:

public void disconnect() { 
    if (blegatt== null) { 
     Log.w(TAG, "BluetoothAdapter not initialized"); 
    } 
    else { 
     bleGatt.disconnect(); 
     close(); 
     gatt = null; 
    } 
} 
/** 
* After using a given BLE device, the app must call this method to ensure resources are 
* released properly. 
*/ 
public void close() { 
    if (gatt == null) { 
     return; 
    } 
    gatt.close(); 
}