Android - 无法连接到Lollipop上的蓝牙设备

时间:2015-01-19 05:59:09

标签: android bluetooth android-5.0-lollipop gatt

我的应用程序在Android 4.3和4.4上运行良好。 该应用程序将连接并与自定义蓝牙设备通信 在我突然将Nexus 5闪存到Lollipop后,我根本无法连接到设备。连接结果始终为133.这是日志:

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

有人对此有任何想法吗?

4 个答案:

答案 0 :(得分:18)

所以我发现问题是棒棒糖的运输选择 正如您在here中看到的那样,

中的变化

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。 由于未知原因,我不能直接调用它,所以我使用反射来调用它。 到现在为止,这对我来说很好。

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

如果您对我的回答有任何疑问,请随时在评论中提问 谢谢。

答案 1 :(得分:4)

对于遇到相同问题的 Xamarin 用户,这里的解决方案略有不同。使用Xamarin跨平台SDK的Nexus 7 Android 6遇到了同样的问题。使用TRANSPORT_LE解决了这个问题。不是使用Reflection通过其签名获取方法(不起作用),而是可以使用Reflection迭代所有方法,直到找到匹配的名称。请参阅以下代码:

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
}

答案 2 :(得分:0)

我不认为使用反射调用connectGatt方法是一件明智的事情。由于更新会使您的应用程序出错,因此随时可以更改私有函数。

无论如何,如果你的外围广告包中设置了适当的标志,TRANSPORT_AUTO应该尝试以所需的方式连接到你的外围设备。如果你的外围设备不支持TRANSPORT_BREDR模式,那么就有一个标准的标志" BrEdrNotSupported"你应该在广告数据中设置让中央知道它。

答案 3 :(得分:0)

您使用什么样的BLE IC?如果它是CC254x,则可能与其外围设备的软件堆栈中的问题有关:

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

相关问题