简体   繁体   English

使用 Android BLE Gatt 时如何接收重新连接通知

[英]How to receive a notification on reconnect when using Android BLE Gatt

When using the BluetoothGattCallback on Android, I'm able to see when a bonded device connects and disconnects by overriding onConnectionStateChange and checking the newState field.在 Android 上使用BluetoothGattCallback时,我可以通过覆盖onConnectionStateChange并检查newState字段来查看绑定设备何时连接和断开连接。 However, I'm wondering if there's a means by which my app can be informed of a re-connect which is what I imagine when a bonded device becomes unreachable for a while.但是,我想知道是否有一种方法可以通知我的应用程序重新连接,这就是我想象的当绑定设备一段时间无法访问时的情况。

Note that I'm using bonded devices here so that I'm able to obtain a stable MAC address.请注意,我在这里使用绑定设备,以便能够获得稳定的 MAC 地址。

About the answer关于答案

While I never managed to entirely test the answer, the answer given does make sense to me.虽然我从未设法完全测试答案,但给出的答案对我来说确实有意义。 A few things I've learned:我学到了一些东西:

  • devices have to be bonded to reliably re-connect as their MAC addresses are randomised设备必须绑定才能可靠地重新连接,因为它们的 MAC 地址是随机的
  • with Android, a direct connection must first be achieved so that Android can cache connection information - subsequent connections can be performed with a re-connect对于 Android,必须首先实现直接连接,以便 Android 可以缓存连接信息 - 可以通过重新连接执行后续连接
  • bonding on Android is difficult to get right (I'm yet to) - instead, I'm now leaning more toward leveraging the system UI to pair devices Android 上的绑定很难正确(我还没有) - 相反,我现在更倾向于利用系统 UI 来配对设备
  • use a library like the Nordid Android BLE Library - there are many quirks to Android使用像 Nordid Android BLE 库这样的库 - Android 有很多怪癖

Old supplement to the question below - should now be ignored以下问题的旧补充 - 现在应该被忽略

The logs show that onConnectionStateChange is called:日志显示onConnectionStateChange被调用:

16:21:33.681 D/BluetoothGatt: connect() - device: 57:D4:E9:34:A4:CB, auto: true
16:21:33.682 D/BluetoothGatt: registerApp()
16:21:33.683 D/BluetoothGatt: registerApp() - UUID=b4751d3b-ccdd-44c2-823e-deed18057af3
16:21:33.689 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
16:22:44.154 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=6 device=57:D4:E9:34:A4:CB

If the device later becomes disconnected, I also see the state change:如果设备稍后断开连接,我还会看到 state 更改:

16:24:25.800 D/BluetoothGatt: onClientConnectionState() - status=8 clientIf=6 device=57:D4:E9:34:A4:CB

If the device reconnects given the auto-connection then a connection is re-established but I don't receive any notification:如果设备在自动连接的情况下重新连接,则重新建立连接,但我没有收到任何通知:

16:26:30.519 D/BluetoothGatt: connect() - device: 57:D4:E9:34:A4:CB, auto: true
16:26:30.520 D/BluetoothGatt: registerApp()
16:26:30.521 D/BluetoothGatt: registerApp() - UUID=a973e2a7-b881-404a-875b-b2d25460e023
16:26:30.528 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=7

So, how can an app detect a re-connect?那么,应用程序如何检测重新连接?

What you should do is to simply call connectGatt once with the autoConnect parameter set to true .您应该做的是简单地调用connectGatt一次,并将autoConnect参数设置为true When the device connects for the first time, you will get the onConnectionStateChange callback indicating that it's now connected.当设备第一次连接时,您将收到onConnectionStateChange回调,指示它现在已连接。 When the connection drops for some reason, you will get onConnectionStateChange indicating it's now disconnected.当连接由于某种原因断开时,您将获得onConnectionStateChange指示它现在已断开连接。 But you don't have to, and shouldn't, call connectGatt again.但是您不必也不应该再次调用connectGatt As soon as the peripheral advertises again, your Android device will automatically reconnect and you will get the onConnectionStateChange .一旦外围设备再次发布广告,您的 Android 设备将自动重新连接,您将获得onConnectionStateChange

This will work until Bluetooth is turned off (as all Bluetooth objects are silently destroyed at that time), or your app process is terminated.这将一直有效,直到蓝牙关闭(因为当时所有蓝牙对象都被静默销毁),或者您的应用程序进程终止。 After Bluetooth is turned on, you need to call connectGatt again.蓝牙开启后,需要再次调用connectGatt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM