I have multiple BLE devices with which I need to communicate to. How do I connect to a specific device and communicate with it?
In Windows 10, there doesn't seem to have a connect method.
Thank you
I have multiple BLE devices with which I need to communicate to. How do I connect to a specific device and communicate with it?
In Windows 10, there doesn't seem to have a connect method.
Thank you
I think it's not available yet. You need to wait till Anniversary update(hopefully). Check this out on Windows Developer Feedback Uservoice page https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/7176829-gatt-server-api
GATT Server APIs will be available to developers in that update so please stay tuned
He points to the update shown in Build 2016, the Anniversary Update
Right now, Windows can only be a GATT Client; however, it can still read and write to BLE devices that are GATT Servers. There are a couple steps to connect to a BLE device in Windows 10.
Permissions
First, make sure that you have the correct capabilities set. Go to Package.appxmanifest, Capabilities tab, and turn on Bluetooth.
Package.appxmanifest > Capabilities > Turn on Bluetooth
Finding a BLE Device
Important note. Right now, Windows 10 does not support connecting to an unpaired BLE device. You must pair the device in the settings page, or use the in app pairing APIs.
Knowing the device is paired, there are a few ways to find a BLE device. You can find by Appearance, BluetoothAddress, ConnectionStatus, DeviceName, or PairingState. Once you find the device you are looking for, you use its ID to connect to it. Below is an example to find the device by its name:
string deviceSelector = BluetoothLEDevice.GetDeviceSelectorFromDeviceName("SOME_NAME");
var devices = await DeviceInformation.FindAllAsync(deviceSelector);
// Choose which device you want, name it yourDevice
BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(yourDevice.Id);
The FromIdAsync method is where Windows will connect to the BLE device.
Communicating
You can read from and write to characteristics on a device by the following.
// First get the characteristic you're interested in
var characteristicId = new Guid("SOME_GUID");
var serviceId = new Guid("SOME_GUID");
var service = device.GetGattService(serviceId);
var characterstic = service.GetCharacteristics(characteristicId)[0];
// Read from the characteristic
GattReadResult result = await characterstic.ReadValueAsync(BluetoothCacheMode.Uncached);
byte[] data = (result.Value.ToArray());
// Write to the characteristic
DataWriter writer = new DataWriter();
byte[] data = SOME_DATA;
writer.WriteBytes(data);
GattCommunicationStatus status = await characteristic.WriteValueAsync(writer.DetachBuffer());