这是GitHub上针对Android Things的GATT Server示例的链接:
https://github.com/androidthings/sample-bluetooth-le-gattserver
在RPi-3上设置服务器非常简单。
我不明白为什么GATT服务器一旦连接到设备断开连接就停止广告(BLE连接)。
...gattserver I/GattServerActivity: BluetoothDevice CONNECTED: 67:2F:1A:B4:1F:86
...gattserver D/BluetoothGattServer: onConnectionUpdated() - Device=67:2F:1A:B4:1F:86 interval=6 latency=0 timeout=2000 status=0
...gattserver D/BluetoothGattServer: onConnectionUpdated() - Device=67:2F:1A:B4:1F:86 interval=39 latency=0 timeout=2000 status=0
...gattserver I/GattServerActivity: Read CurrentTime
...gattserver I/GattServerActivity: Read CurrentTime
...gattserver I/GattServerActivity: Read CurrentTime
...gattserver I/GattServerActivity: Read CurrentTime
...gattserver I/GattServerActivity: Read CurrentTime
...gattserver D/GattServerActivity: Config descriptor read
...gattserver I/GattServerActivity: Read LocalTimeInfo
...gattserver D/BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=67:2F:1A:B4:1F:86
...gattserver I/GattServerActivity: BluetoothDevice DISCONNECTED: 67:2F:1A:B4:1F:86
...gattserver I/GattServerActivity: No subscribers registered
以上是LogCat中显示的设备。 第一行显示我的手机能够连接到设备。 (使用这个免费且优秀的应用程序:https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp)
连接后,我可以读取它的特性(Read CurrentType,Read LocalTimeInfo等)
当断开手机/应用程序与设备的连接时,GattServerActivity指出我已经断开了优雅并继续运行......
但是尝试再次从手机/应用程序扫描设备后发现RPi上的GATT服务器已经僵尸......
LogCat中没有错误(不在应用程序中,不在系统中)...
有人想到吗?
答案 0 :(得分:2)
问题似乎是GATT服务器在断开连接时没有再次开始做广告?您应该可以添加新行here以再次开始投放广告。
答案 1 :(得分:0)
尝试过只是重新开始投放广告,但这没有用,所以我停下了脚步,然后开始运作。在RPi 3B上测试
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "BluetoothDevice CONNECTED: " + device);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "BluetoothDevice DISCONNECTED: " + device);
//Remove device from any active subscriptions
mRegisteredDevices.remove(device);
stopAdvertising();
startAdvertising();
}
}