3

This is the link to the GATT Server sample for Android Things on GitHub:

https://github.com/androidthings/sample-bluetooth-le-gattserver

Setting up the server on RPi-3 is easy enough.

What I do not understand is why the GATT server stops advertising once you have connected to then disconnected from the device (BLE connect).

...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

The above is what shows in the LogCat for the device. First line shows that my phone was able to connect to the device. (using this free and excellent app: https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp )

When connected I can read it's characteristics (Read CurrentType, Read LocalTimeInfo etc.)

When disconnecting the phone/app from the device the GattServerActivity states that I disconnected with grace and keeps on running...

But trying to scan for devices from the phone/app again reveals that the GATT Server on the RPi has gone zombie...

No errors in the LogCat (not in app, not in system)...

Thoughts anyone?

Ove Stoerholt
  • 3,843
  • 4
  • 25
  • 33
  • So the issue is that the GATT server doesn't start advertising again on disconnection? Can you just add a new line [here](https://github.com/androidthings/sample-bluetooth-le-gattserver/blob/master/app/src/main/java/com/example/androidthings/gattserver/GattServerActivity.java#L317) to start advertising again? – Nick Felker Dec 06 '17 at 18:21
  • @Nick Felker I can (and thanks). It's just that I never expected it to stop... Is this normal behavior for BLE advertising? – Ove Stoerholt Dec 07 '17 at 07:27
  • I'm not an expert in the BLE behavior, so I do not know if it is intentional. I will check with the sample. But I will reformat the comment above as the canonical answer. – Nick Felker Dec 07 '17 at 18:32

2 Answers2

2

The issue seems to be that the GATT server doesn't start advertising again on disconnection? You should be able to add a new line here to start advertising again.

Nick Felker
  • 11,536
  • 1
  • 21
  • 35
1

Tried just re-starting advertising, but that didn't work so I stopped and started and that worked. Test on 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();
        }
    }