【问题标题】:Connect to gatt server not working on Samsung Galaxy Core Prime (Android 5.1.1)连接到无法在三星 Galaxy Core Prime (Android 5.1.1) 上运行的 gatt 服务器
【发布时间】:2018-11-12 11:22:39
【问题描述】:

我有一个适用于 Android 6.0 及更高版本的设备的 BLE 应用,但我在 Samsung Galaxy Core Prime (Android 5.1.1) 上遇到问题。调用connectGatt 后没有任何反应,回调只接收断开状态。

我已经剪掉了相应的代码:

BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
final BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
Log.e("BluetoothLeService", "connect: connectGatt(this, false, mGattCallback)");
BluetoothGatt bluetoothGatt = device.connectGatt(this, false, mGattCallback);

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.e("BluetoothLeService", "onConnectionStateChange: STATE_CONNECTED");
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.e("BluetoothLeService", "onConnectionStateChange: STATE_DISCONNECTED");
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    }

    @Override
    public void onMtuChanged (BluetoothGatt gatt, int mtu, int status) {
    }
};

这是日志:

E/BluetoothLeService: connect: connectGatt(this, false, mGattCallback)
I/StatusBar: Icon Only
D/BluetoothGatt: connect() - device: CC:4B:73:22:9D:DB, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=7d428718-b549-4021-8a45-9d3bd2afd462
D/BtGatt.GattService: registerClient() - UUID=7d428718-b549-4021-8a45-9d3bd2afd462
D/SamsungIME: Dismiss ExpandCandiate
D/PanelView: There is/are notification(s) 
D/CustomFrequencyManagerService: releaseDVFSLockLocked : Getting Lock type frm List : DVFS_MIN_LIMIT  frequency : 1248000  uid : 1000  pid : 2975  tag : ACTIVITY_RESUME_BOOSTER@4
I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{2bfa99a5 u0 com.streamunlimited.streamsdkdemo/.ui.setup.SetupDeviceActivity t43} time:3838458
W/ActivityManager: mDVFSHelper.release()
D/CustomFrequencyManagerService: acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 1248000  uid : 1000  pid : 2975  pkgName : ACTIVITY_RESUME_BOOSTER@8
D/BtGatt.GattService: onClientRegistered() - UUID=7d428718-b549-4021-8a45-9d3bd2afd462, clientIf=6
D/BluetoothLeService: Trying to create a new connection.
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
D/BtGatt.GattService: clientConnect() - address=CC:4B:73:22:9D:DB, isDirect=true set own addr = false own addr type:0
D/BtGatt.btif: btif_get_device_type: Device [cc:4b:73:22:9d:db] type 3, addr. type 0
D/IOP_DB_BT: db_query_create: id EnforceMasterRole :: key KEY_BDADDR, value cc:4b:73:22:9d:db
D/IOP_DB_BT: db_query_add_key: key KEY_LMP_MFCT, value 15
D/IOP_DB_BT: db_query_add_key: key KEY_LMP_VER, value 9:24838
D/IOP_DB_BT: db_query_add_key: key KEY_DIR_ALL, value 1
D/IOP_DB_BT: db_query_execute: result 1
D/bluedroid-mrvl: bt_vnd_mrvl_if_op(L95): opcode = 7
I/SurfaceFlinger: id=153 Removed TetupScanAc (3/9)
I/SurfaceFlinger: id=153 Removed TetupScanAc (-2/9)
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@21558ec5 time:3838516
W/SurfaceFlinger: couldn't log to binary event log: overflow.
D/CustomFrequencyManagerService: releaseDVFSLockLocked : Getting Lock type frm List : DVFS_MIN_LIMIT  frequency : 1248000  uid : 1000  pid : 2975  tag : ACTIVITY_RESUME_BOOSTER@8
D/BatteryService: !@BatteryListener : batteryPropertiesChanged!
W/bt-btif: bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0004
W/bt-btif: bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0004
W/bt-btif: bta_gattc_conn_cback() - cif=6 connected=0 conn_id=6 reason=0x0004
D/BtGatt.GattService: onConnected() - clientIf=6, connId=0, address=CC:4B:73:22:9D:DB
D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=6 device=CC:4B:73:22:9D:DB
E/BluetoothLeService: onConnectionStateChange: STATE_DISCONNECTED

看起来可疑的行是:

W/bt-btif: bta_gattc_conn_cback() - cif=4 connected=0 conn_id=4 reason=0x0004
W/bt-btif: bta_gattc_conn_cback() - cif=5 connected=0 conn_id=5 reason=0x0004
W/bt-btif: bta_gattc_conn_cback() - cif=6 connected=0 conn_id=6 reason=0x0004

我发现原因4的意思是:

PAGE TIMEOUT (0x04)
The Page Timeout error code indicates that a page timed out because of the
Page Timeout configuration parameter. This error code may occur only with the
Remote_Name_Request and Create_Connection commands.

在文档中https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=421043&_ga=2.243934277.1321770839.1542020684-854809025.1539785109

还有这一行:

D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=6 device=CC:4B:73:22:9D:DB

只是 GATT_ERROR 的一个宏,基于:

https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-4.4.4_r2.0.1/stack/include/gatt_api.h

我也尝试过 nRF Connect 应用,但它也无法连接:错误 133 (0x85):GATT ERROR

您知道任何可能的原因和解决方案吗? 谢谢

【问题讨论】:

  • 我很确定这是三星一般只是愚蠢。在我为我工作的公司制作的项目中遇到了同样的问题。我们最终连续几次重试连接,因为这与 NRFConnect 人员所做的事情相同。他们的 github 上有一个问题。好吧,他们是制造 BLE IC 的北欧人。显然三星在内部服务缓存刷新方面存在问题,因此您也可以破解一个私有方法并通过反射调用它,它显然解决了它。我们选择连续连接几次。
  • @RobertoNicbaAnićBanić 感谢您的建议。所以你说当我收到断开电话device.connectGatt(this, false, mGattCallback); 时我应该尝试拨打device.connectGatt(this, false, mGattCallback); 直到我连接或达到最大尝试次数?关于最大重试次数有什么建议吗?谢谢
  • 我们重试了 5 次,但请确保您检查断开连接错误是 133

标签: android bluetooth bluetooth-lowenergy gatt


【解决方案1】:

错误状态133可能有多种原因,因为它是Android系统报告的一般错误。可能的原因:

  1. 当外围​​设备超出范围/在建立时关闭 连接
  2. 当 Android BLE 堆栈无法正常工作时(有时关闭和打开蓝牙适配器和 WiFi 会有所帮助)
  3. 当外围​​设备未正确符合 BLE 规范时
  4. 当手机 BLE 驱动不够好时
  5. 其他(查看谷歌)

最简单的解决方法是在连接失败并出现此类错误时重试。

我强烈推荐使用Polidea's RxAndroidBle Library来实现BLE通信,非常有用。

如果您开始使用它,那么您可以尝试建立连接,如果连接失败,则使用运算符retryWhen 重试。 You can get some clues in this answer.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2015-03-19
    相关资源
    最近更新 更多