Android - Could not connect to bluetooth device on Lollipop

余生颓废 提交于 2019-11-30 06:46:43

So I figured out that the problem is the Transport choice in Lollipop.
As you can see in here the change in the

BluetoothDevice.connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback)

function is calling

BluetoothDevice.connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport)

with transport set to TRANSPORT_AUTO.
In my case because I will always use TRANSPORT_LE (The value is 2)
I tried to call the second method from my code and set the transport to TRANSPORT_LE. For unknown reason I can't call it directly so I'm using reflection to call it. Until now this works fine for me.

            if(TTTUtilities.isLollipopOrAbove()) {
                // Little hack with reflect to use the connect gatt with defined transport in Lollipop
                Method connectGattMethod = null;

                try {
                    connectGattMethod = device.getClass().getMethod("connectGatt", Context.class, boolean.class, BluetoothGattCallback.class, int.class);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }

                try {
                    mBluetoothGatt = (BluetoothGatt) connectGattMethod.invoke(device, BluetoothConnectService.this, false, mGattCallback, TRANSPORT_LE);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            } else  {
                mBluetoothGatt = device.connectGatt(BluetoothConnectService.this, true, mGattCallback);
            }

If any of you have question about my answer feel free to ask in the comment.
Thank you.

For Xamarin users who have the same issue, here is a slightly different solution. I had the same issue with a Nexus 7 Android 6 using the Xamarin cross platform SDK. The use of TRANSPORT_LE fixed the issue. Rather than using Reflection to get the method by its signature (does not work), one can use Reflection to iterate through all the methods until you find a matching name. See code below:

BluetoothDevice bd = (BluetoothDevice)device.NativeDevice;
Java.Lang.Reflect.Method[] methods = bd.Class.GetDeclaredMethods();

foreach (Java.Lang.Reflect.Method possibleConnectGattMethod in methods) 
{
// Find matching method name connectGatt and then invoke it with     TRANSPORT_LE
}

I do not think calling connectGatt method youself using reflection is a wise thing. As the private functions can get changed anytime with the update leaves your application buggy.

And anyways TRANSPORT_AUTO should try to connect to your peripheral in a desired way if proper flags are set in your peripheral advertisement packet. If your peripheral does not support TRANSPORT_BREDR mode then there is a standard flag "BrEdrNotSupported" that you should set in advertisement data to let central know about it.

What kind of BLE IC are you using? If it is a CC254x it may be related to problems in their software stack for peripheral devices:

https://e2e.ti.com/support/wireless_connectivity/f/538/t/401240

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!