GATT Connection State Change Detection


The standard Microsoft UWP GATT API has some problems with device connections management. When your application connects to a remote GATT-enabled device (peripheral), you cannot disconnect it from the server in an easy way. Sometimes a device remains connected even after you close your application. The Microsoft UWP GATT API does not provide a Disconnect method, and none of Microsoft's recommended solutions work. Also, when your application acts as a GATT server (peripheral), there is no event that can notify your application when a remote client is connected to and disconnected from your server. And this is the big problem.

When your GATT client application finished its work with a peripheral device, it needs to disconnect. The disconnection is critical because most peripheral devices will not accept any new connections while there is an active one. Also, peripheral devices usually stop advertising when any active connection exists. So your or any other application is not able to find your peripheral device because WinRT API did not close the connection. You need to close your application and start again to release the device. And even closing the application may not work if the device is paired: the system may keep the device connected, and you need to unpair it.

If your application is a GATT server (peripheral), you need to know when a remote client (central) is connected to and disconnected from your application. There are many reasons why you need it: your application may need to prepare data, to initialize internal state, and many other reasons. However, the standard Microsoft UWP GATT API does not provide any events to notify your application about a remote client connection state. You even cannot know when a client disconnected to do a cleanup.

Fortunately, the Bluetooth Framework library solves all the GATT connection problems. If your application is connected to a remote GATT-enabled device and, when it finishes all the communication and needs to disconnect, the Bluetooth Framework executes the real disconnection immediately. So now your peripheral device can be discovered and connected again. Also, the library can detect when a remote GATT server (peripheral) terminates connection for different reasons, even if it was turned off.

To test this feature, use the GattClient sample application from the Bluetooth Framework package.

The other problem is the connection state detection when your application runs as a GATT server. The standard Microsoft UWP GATT API can notify your application only when a client subscribes, unsubscribes, reads, or writes a characteristic. It is not very useful if your application needs a complex initialization when the client connects. Also, the client can subscribe to more than one characteristic and read or write a few times. And of course there is no event that can notify your application when a remote client disconnects. That makes it impossible to do a clean-up and makes client connection management too complex.

The Bluetooth Framework is able to do a real client connection detection. The library is able to detect when a client connects to the server without needing to subscribe, read, or write a characteristic. The notification is immediately sent to your application when a client connects and disconnects. Also, the library allows your application to disconnect any connected client.

To test this unique feature, use the GattServer sample application from the Bluetooth Framework package. Should you have any questions, please do not hesitate to contact us.