Android BLE:onServicesDiscovered永远不会触发Nexus 5或Samsung Note 3

时间:2014-07-03 17:04:04

标签: android bluetooth-lowenergy

我正在开发一款连接自定义蓝牙设备的Android应用。许多帖子都非常有用(例如thisthis),但我遇到了一个问题,如果这些建议没有解决。

蓝牙连接成功后,我拨打BluetoothGatt.discoverServices,这在大多数设备上都能正常运行。在Nexus 5和Samsung Note 3上,我从未收到任何响应(onServicesDiscovered),无论是成功还是其他。这是在第一次尝试连接时发生的,disconnect()close()似乎无法解决此问题。

我有传闻证明这是一个Android操作系统错误;只有我们仍然在4.4.2上的Nexus 5遇到了这个问题,升级到4.4.3似乎已经解决了这个问题。这对于三星来说还不是一个选择,如果可能的话我想找一个解决方法。我知道我们过去没有这些问题就连接到了Nexus 5;当我试图追踪可能发生的变化时,我会感激任何想法。

三星Note 3日志:

07-02 17:29:54.891 I/PeripheralManager( 8282): Connect has been called on HFEC161332B90C15C29E DF:5F:C8:DF:04:35 -43 , shouldPair = true
07-02 17:29:54.891 I/PeripheralManager( 8282): BTSTATE acquiring new gatt and connecting...
07-02 17:29:54.891 D/BluetoothGatt( 8282): connect() - device: DF:5F:C8:DF:04:35, auto: false
07-02 17:29:54.891 D/BluetoothGatt( 8282): registerApp()
07-02 17:29:54.896 D/BluetoothGatt( 8282): registerApp() - UUID=2fc3ce73-c50c-4cda-8b82-1532a5dccb14
07-02 17:29:54.896 D/BtGatt.GattService( 3434): registerClient() - UUID=2fc3ce73-c50c-4cda-8b82-1532a5dccb14
07-02 17:29:54.896 D/BtGatt.btif( 3434): btif_gattc_register_app
07-02 17:29:54.896 D/BtGatt.btif( 3434): btgattc_handle_event: Event 1000
07-02 17:29:54.896 D/BtGatt.btif( 3434): btif_gattc_upstreams_evt: Event 0
07-02 17:29:54.901 D/BtGatt.GattService( 3434): onClientRegistered() - UUID=2fc3ce73-c50c-4cda-8b82-1532a5dccb14, clientIf=5
07-02 17:29:54.901 I/BluetoothGatt( 8282): Client registered, waiting for callback
07-02 17:29:54.901 D/BluetoothGatt( 8282): onClientRegistered() - status=0 clientIf=5
07-02 17:29:54.901 D/BtGatt.GattService( 3434): clientConnect() - address=DF:5F:C8:DF:04:35, isDirect=true
07-02 17:29:54.901 D/BtGatt.btif( 3434): btif_gattc_open
07-02 17:29:54.901 D/BluetoothAdapter( 8282): stopLeScan()
07-02 17:29:54.906 D/BtGatt.btif( 3434): btgattc_handle_event: Event 1004
07-02 17:29:54.906 D/BtGatt.btif( 3434): btif_get_device_type: Device [df:5f:c8:df:04:35] type 2, addr. type 1
07-02 17:29:54.906 D/BtGatt.btif( 3434): btif_gattc_upstreams_evt: Event 2
07-02 17:29:54.906 D/BtGatt.GattService( 3434): onConnected() - clientIf=5, connId=5, address=DF:5F:C8:DF:04:35
07-02 17:29:54.906 D/BluetoothGatt( 8282): onClientConnectionState() - status=0 clientIf=5 device=DF:5F:C8:DF:04:35
07-02 17:29:54.906 I/PeripheralManager( 8282): Gatt State Connected
07-02 17:29:54.906 D/BluetoothGatt( 8282): discoverServices() - device: DF:5F:C8:DF:04:35
07-02 17:29:54.911 D/BtGatt.GattService( 3434): discoverServices() - address=DF:5F:C8:DF:04:35, connId=5
07-02 17:29:54.911 D/BtGatt.btif( 3434): btif_gattc_search_service
07-02 17:29:54.911 I/PeripheralManager( 8282): Gatt Service Discovery Started
07-02 17:29:54.911 D/BtGatt.btif( 3434): btgattc_handle_event: Event 1006
…
07-02 17:30:24.901 W/PeripheralManager( 8282): Connection time out hard
07-02 17:30:24.921 I/PeripheralManager( 8282): canceling connection

连接代码:

public void connect(final Peripheral peripheral, boolean pair) {
    Log.i(TAG,  "Connect has been called on " + peripheral.toString() + ", shouldPair = " + pair);
    this.shouldPair = pair;
    bluetoothHandler.post(new Runnable() {
         @Override
         public void run() {
            if(isConnected) {
                Log.w(TAG, "Connecting when already connected!");
                return;
            }
            if (mBluetoothAdapter == null || peripheral == null || peripheral.getAddress() == null) {
                Log.e(TAG, "BluetoothAdapter not initialized or unspecified address.");
                return;
            }
            final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(peripheral.getAddress());
            if (device == null) {
                Log.e(TAG, "Device not found.  Unable to connect.");
                return;
            }
            if(mBluetoothGatt != null) { // use existing gatt if present and not closed
                Log.i(TAG, "connecting to existing gatt...");
                mBluetoothGatt.connect();
            } else {
                Log.i(TAG, "acquiring new gatt and connecting...");
                mBluetoothGatt = device.connectGatt(context, false, mGattCallback);
            }
         }
    });

    lastConnectedPeripheral = peripheral;
}

发现服务:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (newState == BluetoothGatt.STATE_CONNECTED) {
                Log.i(TAG,"Gatt State Connected");
                isBluetoothConnected = true;
                tryDiscoveringServices();
                ...

请告诉我有哪些其他信息可以解释这一点。谢谢!

编辑:在我们的固件更改后,问题已经减轻:连接只需要一分多钟,而不是从不连接。这表明问题至少部分是由于我们特定的蓝牙设备以及Android操作系统的任何变化。如果我发现如何解决这个相关问题,我会更新。

0 个答案:

没有答案