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

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

问题描述

我有在Android 4.3和4.4运转良好的应用程序。
该应用程序会自动连接并使用自定义的蓝牙设备进行通信。结果
我亮出我的Nexus 5之后突然棒棒堂我无法连接到该设备的。连接结果总是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

我的code:

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?

推荐答案

所以我想通了,问题是交通运输的选择在棒棒糖。结果
正如你所看到的<一个href=\"http://grep$c$c.com/file_/repository.grep$c$c.com/java/ext/com.google.android/android/5.0.0_r1/android/bluetooth/BluetoothDevice.java/?v=diff&id2=4.4.4_r1\">here在变化

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(上下文的背景下,布尔自动连接,BluetoothGattCallback回调)

功能是调用

BluetoothDevice.connectGatt(上下文的背景下,布尔自动连接,BluetoothGattCallback回调,INT运输)

与交通设置为TRANSPORT_AUTO。结果
在我的情况,因为我会一直用TRANSPORT_LE(值为2)结果
我试图从我的code调用第二种方法和传输设置为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的 - 无法连接到蓝牙棒棒糖设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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