onServicesDiscovered状态为129,并且在Android中为BLE连接unstable

时间:2014-06-02 09:11:41

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

我按照蓝牙低功耗页面进行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();发现服务,如下面的代码。

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会在连接后立即断开连接,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:

为什么会出现这种情况?

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

1 个答案:

答案 0 :(得分:3)

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

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

        @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();
            }
        }