【问题标题】:onServicesDiscovered status is 129 and connect unstable for BLE in AndroidonServicesDiscovered 状态为 129 并且在 Android 中为 BLE 连接不稳定
【发布时间】:2014-07-22 08:39:56
【问题描述】:

我关注低功耗蓝牙页面,在 Android 4.3 中开发低功耗蓝牙

我尝试通过以下代码连接 BLE 设备:

public void connect(final String address) {
        // TODO Auto-generated method stub
        Log.w(TAG, "BluetoothLeService Connect function.");
        if(mBluetoothAdapter == null || address == null){
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        }
        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
    }

连接到BLE设备后,会通过mBluetoothGatt.discoverServices();发现Service,如下代码。

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

        public void onConnectionStateChange(android.bluetooth.BluetoothGatt gatt, int status, int newState) {
            if(mBluetoothGatt == null){
                Log.e(TAG, "mBluetoothGatt not created!");
                return;
            }

            device = gatt.getDevice();
            address = device.getAddress();
            try {
                switch (newState) {
                case BluetoothAdapter.STATE_CONNECTED:

                    Log.i(TAG, "STATE_CONNECTED:");
                    broadcastUpdate(ACTION_GATT_CONNECTED, device, status);
                    mBluetoothGatt.discoverServices();
                    // Attempts to discover services after successful connection.

                    break;
                case BluetoothAdapter.STATE_DISCONNECTED:
                    Log.i(TAG, "STATE_DISCONNECTED:");
                    gatt.close();

                    broadcastUpdate(ACTION_GATT_DISCONNECTED, device, status);
                    break;

                default:
                    Log.i(TAG, "New state not processed: " + newState);
                    break;
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

但是Android会在connect后立即断开连接,onServicesDiscovered的状态也显示129如下日志

D/BtGatt.btif(25992): btif_gattc_upstreams_evt: Event 2
D/BtGatt.GattService(25992): onConnected() - clientIf=4, connId=4, address=20:73:20:00:6C:B4
D/BluetoothGatt(27228): onClientConnectionState() - status=0 clientIf=4 device=2
0:73:20:00:6C:B4
I/BluetoothLeService(27228): BluetoothGattCallback-----newState@@@@@@2
I/BluetoothLeService(27228): STATE_CONNECTED:
I/Device_information(27228): BroadcastReceiver---action = ti.android.ble.common.
ACTION_GATT_CONNECTED
D/BluetoothGatt(27228): discoverServices() - device: 20:73:20:00:6C:B4
D/BtGatt.GattService(25992): discoverServices() - address=20:73:20:00:6C:B4, connId=4
D/BtGatt.btif(25992): btif_gattc_search_service
D/BtGatt.btif(25992): btgattc_handle_event: Event 1006
W/qdhwcomposer(  326): Excessive delay reading vsync: took 266 ms
W/bt-l2cap(25992): L2CA_EnableUpdateBleConnParams - unknown BD_ADDR 207320006cb4
E/bt-btm  (25992): btm_sec_disconnected - Clearing Pending flag
E/MP-Decision( 2172): Error setting a sleep mode for secondary cores - -38
D/BtGatt.btif(25992): btif_gattc_upstreams_evt: Event 6
D/BtGatt.GattService(25992): onSearchCompleted() - connId=4, status=129
D/BluetoothGatt(27228): onSearchComplete() = Device=20:73:20:00:6C:B4 Status=129
I/BluetoothLeService(27228): onServicesDiscovered-----status@@@@@@ = 129
I/BluetoothLeService(27228): onServicesDiscovered-----129@@@@@@ = 129
W/BluetoothLeService(27228): BluetoothLeService Connect function.
D/BluetoothManager(27228): getConnectionState()
D/BluetoothManager(27228): getConnectedDevices
D/BluetoothAdapterService(1109096104)(25992): Get Bonded Devices being called
D/BluetoothAdapterProperties(25992): getBondedDevices: length=1
D/BtGatt.GattService(25992): getDeviceType() - device=20:73:20:00:6C:B4, type=2
W/BluetoothLeService(27228): connect----------mBluetoothGatt = android.bluetooth.BluetoothGatt@4227a800
D/BluetoothGatt(27228): connect() - device: 20:73:20:00:6C:B4, auto: true
D/BluetoothGatt(27228): registerApp()
D/BluetoothGatt(27228): registerApp() - UUID=c560ac73-9ca3-4c2b-a6d4-7c6ec57b7209
D/BtGatt.GattService(25992): registerClient() - UUID=c560ac73-9ca3-4c2b-a6d4-7c6ec57b7209
D/BtGatt.btif(25992): btif_gattc_register_app
D/BtGatt.btif(25992): btif_gattc_upstreams_evt: Event 5
D/BtGatt.GattService(25992): onDisconnected() - clientIf=4, connId=4, address=20:73:20:00:6C:B4
D/BluetoothGatt(27228): onClientConnectionState() - status=133 clientIf=4 device=20:73:20:00:6C:B4
I/BluetoothLeService(27228): BluetoothGattCallback-----newState@@@@@@0
I/BluetoothLeService(27228): STATE_DISCONNECTED:

为什么会出现这种情况?

这几天让我感到困惑......有人可以帮助我吗? 提前致谢。

【问题讨论】:

  • Android 上的 BLE 是一团糟,看看这个:code.google.com/p/android/issues/detail?id=58381
  • @Michael 但似乎没有解决这个问题的答案,是吗?
  • 没有什么我会称之为解决方案的。多个连接似乎会使情况变得更糟,因此每个 Gatt 限制为一个连接,并尝试忍受间歇性连接。
  • 基于其中一些 cmets,看起来它可能有助于同时关闭(或至少不使用)wifi,并引入一些短的(可能是几百毫秒?) 连接、发现服务、写入特征等操作之间的延迟。经过一番挖掘,我遇到了这个问题:android.googlesource.com/platform/external/bluetooth/bluedroid/… 错误 129 是一个通用错误,查找 GATT_INTERNAL_ERROR (0x0081)。这个数字一直到 Lollipop (5.1.1) 都没有改变。 HTH某人:)

标签: android bluetooth-lowenergy android-4.3-jelly-bean


【解决方案1】:

我遇到了同样的问题,然后我重新启动了蓝牙。我重启后,得到这个状态错误。

以下内容对我有用。这是蓝牙服务中的代码:

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            if (status != 0) {
                mBluetoothAdapter.disable();

        Timer single_timer = new Timer();
        single_timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mBluetoothAdapter.enable();
            }
        }, 1000);


        Timer second_timer = new Timer();
        second_timer.schedule(new TimerTask() {
            @Override
            public void run() {
                gattServiceIntent = new Intent(context, BluetoothLeService.class);
                bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
                mBluetoothLeService.initialize();
                mBluetoothLeService.connect(mDeviceAddress);
                //maybe you need wait 0.5-1 second to call connect() after called initialize()
            }
        }, 2000);


                Log.e(TAG, "An error code get at onServicesDiscovered= " + status);
            }
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mBluetoothGatt.discoverServices();
            }
        }

【讨论】:

  • 对于那些即使在多次重试和 mBluetoothGatt.DiscoverServices() 的 Thread.sleep(1000) 之后仍然有“STATUS=129”的人。可以通过断开连接并重新连接来解决此问题。意思是调用 mGatt.disconnect();和 m.Gatt.connect()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 2023-03-12
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多