Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onServicesDiscovered status is 129 and connect unstable for BLE in Android

I following the page Bluetooth Low Energy for developing in Android 4.3 for Bluetooth Low Energy

I try connect BLE device by following code:

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

After connect to the BLE device , it will discover the Service by mBluetoothGatt.discoverServices(); like the following code.

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

But the Android will disconnect immediately after connect , and the status of onServicesDiscovered also show 129 like the following log

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:

Why this condition happened ?

It confuse me a few day...Does someone can help me? Thanks in advance.

like image 955
Wun Avatar asked Jun 02 '14 09:06

Wun


1 Answers

I faced the same problem, then I restarted bluetooth. After the restart I, got this status error.

The following worked for me. Here's the code in Bluetooth service:

        @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();
            }
        }
like image 184
heinousdog Avatar answered Nov 08 '22 08:11

heinousdog