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