5

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

Illuminati0x5B
  • 602
  • 7
  • 24

2 Answers2

2

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

Raamakrishnan A.
  • 515
  • 3
  • 14
2

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());
Carter
  • 3,053
  • 1
  • 17
  • 22
  • If the GATT services are not available, you should pair to the device and use the DeviceWatcher api. The people at Microsoft are working on a better API, but for now this is the way to do it. more information can be found here: http://stackoverflow.com/questions/35420940/windows-uwp-connect-to-ble-device-after-discovery/39040812#39040812 – LanderV Aug 19 '16 at 14:01
  • So, I'm guessing this is in C#? It's not mentioned anywhere. – Thom May 04 '21 at 19:11