Android - 无法连接到 Lollipop 上的蓝牙设备 [英] Android - Could not connect to bluetooth device on Lollipop

查看:22
本文介绍了Android - 无法连接到 Lollipop 上的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Android 4.3 和 4.4 上运行良好的应用程序.该应用程序将连接自定义蓝牙设备并与之通信.
在我将 Nexus 5 刷入 Lollipop 后,我突然无法连接到该设备.连接结果总是 133.这是日志:

I have an application that working well on Android 4.3 and 4.4. The application will connect and communicate with a custom bluetooth device.
After I flashed my Nexus 5 to Lollipop suddenly the I can't connect to the device at all. The connection result always 133. This is the log :

D/BluetoothGatt﹕ connect() - device: 00:07:80:04:1A:5A, auto: true
D/BluetoothGatt﹕ registerApp()
D/BluetoothGatt﹕ registerApp() - UUID=xxxxxx-xxxx-xxxxx-xxxx-xxxxxxxx
D/BluetoothGatt﹕ onClientRegistered() - status=0 clientIf=6
D/BluetoothGatt﹕ onClientConnectionState() - status=133 clientIf=6 device=00:07:80:04:1A:5A

我的代码:

public boolean connect(final String address) {
        if (mBluetoothAdapter == null || address == null) {
            return false;
        }
        Handler handler = new Handler(Looper.getMainLooper());
        // Previously connected device.  Try to reconnect.
        if (mBluetoothDeviceAddress != null
                && address.equals(mBluetoothDeviceAddress)
                && mBluetoothGatt != null) {

            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (mBluetoothGatt.connect()) {
                        mConnectionState = STATE_CONNECTING;
                    }
                }
            });
            if (mConnectionState == STATE_CONNECTING) {
                return true;
            } else {
                return false;
            }
        }

        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            return false;
        }

        handler.post(new Runnable() {
            @Override
            public void run() {
                mBluetoothGatt = device.connectGatt(BluetoothConnectService.this, true, mGattCallback);
            }
        });
        mBluetoothDeviceAddress = address;
        mConnectionState = STATE_CONNECTING;
        return true;
    }

有人对此有任何想法吗?

Anybody have any idea about this?

推荐答案

于是我发现问题出在 Lollipop 中的 Transport 选择上.
正如您在 这里 改变了

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)

函数正在调用

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

传输设置为 TRANSPORT_AUTO.
就我而言,因为我将始终使用 TRANSPORT_LE(值为 2)
我试图从我的代码中调用第二种方法并将传输设置为 TRANSPORT_LE.由于未知原因,我不能直接调用它,所以我使用反射来调用它.到目前为止,这对我来说很好用.

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.

这篇关于Android - 无法连接到 Lollipop 上的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆