Bluetooth Low Energy : Android Gatt-Client connect

2019-02-16 01:20发布

问题:

I try to connect android to linux via bluetooth low energy GATT mode.

There is a example code in Bluez: btgatt-server.c, I use it as server.

The Android Side be as the Android official example:Connecting to a GATT Server. That is, use function BluetoothDevice::connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback).

After running, the Android part returned the connection be successful,(the parameter of BluetoothGattCallback::onConnectionStateChange be STATE_CONNECTED), but the program in linux(btgatt-server) did not get any response, that still block in waiting accepting. ( line 485).

I also used another example code : l2cap-ble.c, the result was totally the same.

Anyone could help me? my goal is : after connecting, linux gatt server would send a data to Android. Then disconnecting.

thank you lots.

回答1:

I just now faced the same problems.

This seems like a bug that happens with Android and Broadcom bluetooth modules. Android somehow connects to the wrong l2cap channel.

A workaround is to explicitely disable BR/EDR support in the advertisements.

The bug report on google with the solution in #8 can be found here, the quote on the hint that helped me:

I have obtained information from Broadcom regarding this bug and I don't think its a bug on their part. If one does not specifically set the 'BR/EDR not supported' bit in the advertisement (bit 2), the Broadcom stack will use BR/EDR and BTLE. Changing the leading advertisement package from 020102 to 020106 solved the problem.

Using bluez btmgmt for configuring the advertisements on device hci0 i issue the following commands:

btmgmt -i hci0 power off 
btmgmt -i hci0 le on
btmgmt -i hci0 connectable on
btmgmt -i hci0 bredr off        # Disables BR/EDR !
btmgmt -i hci0 advertising on
btmgmt -i hci0 power on

Starting the bluez btgatt-server example:

btgatt-server -i hci0 -s low -t public -r -v