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

Android-无法连接到棒棒糖上的蓝牙设备,android,bluetooth,android-5.0-lollipop,gatt,Android,Bluetooth,Android 5.0 Lollipop,Gatt,我有一个在安卓4.3和4.4上运行良好的应用程序。 应用程序将连接自定义蓝牙设备并与之通信。 在我将Nexus5闪存到棒棒糖上之后,我突然无法连接到该设备。连接结果始终为133。这是日志: D/BluetoothGatt﹕ connect() - device: 00:07:80:04:1A:5A, auto: true D/BluetoothGatt﹕ registerApp() D/BluetoothGatt﹕ registerApp() - UUID=xxxxxx-xxxx-xxxxx-x

我有一个在安卓4.3和4.4上运行良好的应用程序。 应用程序将连接自定义蓝牙设备并与之通信。
在我将Nexus5闪存到棒棒糖上之后,我突然无法连接到该设备。连接结果始终为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;
    }

有人知道吗?

所以我发现问题在于棒棒糖的运输选择。
正如你可以在

BluetoothDevice.connectGatt(上下文上下文、布尔自动连接、BluetoothGattCallback回调)

函数正在调用

BluetoothDevice.connectGatt(上下文上下文、布尔自动连接、BluetoothGattCallback回调、int传输)

将传输设置为传输\自动。
在我的情况下,因为我将始终使用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);
            }
如果你们对我的答案有任何疑问,请在评论中提问。

谢谢。

我不认为自己使用反射调用connectGatt方法是明智的。由于私有函数可以随更新随时更改,因此会导致应用程序出现错误


无论如何,如果外设广告包中设置了正确的标志,TRANSPORT_AUTO应该尝试以所需的方式连接到外设。如果您的外设不支持传输模式,则应在广告数据中设置一个标准标志“BREDRNOTSUPPORED”,以告知central。

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


对于有相同问题的Xamarin用户,这里有一个稍微不同的解决方案。我在使用Xamarin跨平台SDK的Nexus 7 Android 6上也遇到了同样的问题。使用交通工具解决了这个问题。您可以使用反射来迭代所有方法,直到找到匹配的名称,而不是使用反射来通过其签名获取方法(不起作用)。见下面的代码:

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
}

发布代码,日志中是否有任何致命错误?@diva我发布了代码,日志中没有致命错误。您在清单文件中添加了
蓝牙管理
权限吗?@PiyushGupta是的,我有,与此问题有何关系?@PiyushGupta我想您误解了我的问题…感谢您的快速回复。我在我的Nexus5.0.1上尝试了你的解决方案,但不幸的是,它对我不起作用。我仍然得到onClientConnectionState()-status=133 clientIf=5。你知道为什么它对我不起作用吗?@molmol尝试先通过设置->应用->全部->蓝牙共享->清除数据重置蓝牙缓存谢谢,但也没有帮助:(你不能直接打电话给它的原因是@hide@MarianPaździoch很好!请使用评论字段来提供此类建议。只有在您有实际问题解决方案的情况下才能添加答案。评论需要50个信誉帐户。我没有。我仍然想帮助这家伙。我该怎么办。“?我们意识到新用户可能会有宝贵的评论,并且我们可能会因为需要50个信誉点来解锁该功能而失去一些贡献。(……)但我现在想贡献。我应该怎么做?我可以在回答中加入我的评论吗?不。“不幸的是,这就是为什么它必须保持它的内容水平,即使它需要像您这样的好贡献。