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

Nik*_*ono 14 android bluetooth gatt android-5.0-lollipop

我有一个在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
Run Code Online (Sandbox Code Playgroud)

我的代码:

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;
    }
Run Code Online (Sandbox Code Playgroud)

有人对此有任何想法吗?

Nik*_*ono 18

所以我发现问题是Lollipop中的运输选择.
正如你在这里看到的变化

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);
            }
Run Code Online (Sandbox Code Playgroud)

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

  • 你不能直接打电话的原因是@hide (2认同)