gpt4 book ai didi

android - BLE Gatt onConnectionStateChange 失败,状态 133 和 257

转载 作者:行者123 更新时间:2023-11-29 00:03:22 45 4
gpt4 key购买 nike

我正在尝试将我的 Beacon 连接到 Gattservice。在回调 onConnectionStateChange 中,它总是失败,我得到了

statuscodes 133 and 257.

有人写到 133 代表许多连接。在我的代码中有几行带有 gatt.disconnect()。我不知道如何修复它,因为所有其他 gattexamples 都是相同的。如果发现错误很重要,我正在使用 Android 6.0.1 版本和 API 23。这是我的代码:

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if(status == BluetoothGatt.GATT_SUCCESS) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
mBleDevices.add(gatt.getDevice());
Log.i("Beacons", "STATE_CONNECTED");

runOnUiThread(new Runnable() {
@Override
public void run() {
mBluetoothGatt.discoverServices();
}
});
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.e("Beacons", "STATE_DISCONNECTED");
mBleDevices.remove(gatt.getDevice());
mBluetoothGatt.disconnect();
mBluetoothGatt = null;
break;
default:
Log.e("Beacons", "STATE_OTHER");
}
} else {
Log.e("Beacons", "ERROR WITH CONNECTING " + status);
mBleDevices.remove(gatt.getDevice());
}
}

我的 ScanCallback 看起来像这样:

 public void onScanResult(int callbackType, ScanResult result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
BluetoothDevice btDevice = result.getDevice();
connectToDevice(btDevice);
}
});
}

然后像这样开始连接:

 runOnUiThread(new Runnable() {
@Override
public void run() {
mBluetoothGatt = btDevice.connectGatt(getApplicationContext(), true, connectCallback);
}
});

connectCallback 引发 onConnectionStateChange 函数。感谢您的帮助!

最佳答案

在某些 Android 设备上,如果 Android 手机之前未与外围设备配对(绑定(bind)),我会在连接时看到 133 错误。如果我在应用程序尝试在后台连接的同时转到“设置”->“蓝牙”,则连接有效。 (奇怪的是,你不需要在屏幕上做任何事情,它只需要启动。)一旦第一次成功,错误就永远消失了。

我猜这是一个安全限制,但我还没有找到优雅地解决它的正确方法。

关于android - BLE Gatt onConnectionStateChange 失败,状态 133 和 257,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472916/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com