Bluetooth LE Devices¶
Bluetooth Low Energy (Bluetooth LE) is a wireless communication technology designed for short-range, low-power data transfer. It enables reliable connections while using minimal energy. Here are some Bluetooth LE devices, D600, S550 and S370.
Discovering Bluetooth LE Devices¶
The Device Manager drives the Bluetooth LE experience and communication with the Socket Mobile
Bluetooth LE based devices. To interact with the Device Manager register a handler to the Capture
Helper event: DeviceManagerArrival
.
Here is an example on how you can register this handle:
public MainPage()
{
InitializeComponent();
// initialize CaptureHelper
capture = new CaptureHelper();
// register for the Capture Helper events
capture.DeviceManagerArrival += Capture_DeviceManagerArrival;
}
The Device Manager has a property called “favorite” to configure and identify the Bluetooth LE devices that needs to be discovered. Favorite can be a single value or a semi-colon separated list. It can be a combination of any of the following values:
Value |
Description |
---|---|
* |
Discover and connect to the first available Bluetooth LE devices |
Unique Device Identifier |
Identifier for a specific Bluetooth LE devices |
“” |
Empty for when the app is not interested in Bluetooth LE devices |
The “favorite” property can be set as follow:
private void Capture_DeviceManagerArrival(object sender, CaptureHelper.DeviceArgs e)
{
e.CaptureDevice.SetFavoritesAsync("*");
}
Each value in the favorite list represents a Bluetooth LE device. To start the auto discovery for Bluetooth LE devices devices set favorite to a valid value. Auto discovery will stop if the number of devices represented in the favorite are connected.
If favorite is set to empty, auto discovery will not be triggered. Please note that setting favorite to empty, will not disconnect an already connected device. However, if the device gets disconnected, then auto discovery will not start until favorite is set to a valid value.
Once a Bluetooth LE device is discovered, a connection is established. After this it can be used
to read NFC cards or barcodes. To interact with the device, register a handler to the Capture Helper event: DeviceArrival
.:
public MainPage()
{
InitializeComponent();
// initialize CaptureHelper
capture = new CaptureHelper();
// register for the Capture Helper events
capture.DeviceArrival += Capture_DeviceArrival;
}
Note
You can programmatically disconnect a Bluetooth LE (or Bluetooth Classic) device by using the following SetDisconnectAsync()
method
on a received device via the Capture Helper’s DeviceArrival
event.:
CaptureHelperDevice device;
// Device arrival handler
private void Capture_DeviceArrival(object sender, CaptureHelper.DeviceArgs e)
{
device = e.CaptureDevice;
}
private async void DisconnectDevice()
{
await device.SetDisconnectAsync();
}
Manual Discovery¶
This feature lists all the Bluetooth LE devices available in the vicinity by running the discovery process for a
specified time. Using the Device Manager, to trigger a manual discovery, use the StartDeviceDiscoveryAsync(duration)
along with the duration in milliseconds.
Manual discovery runs for the specified duration and sends a DeviceDiscovered
event each time a Socket Mobile Bluetooth LE
device is discovered. Discovery stops automatically once the duration in milliseconds has been reached and sends a DiscoveryEnded
event.
The Device Manager’s favorites should be empty before triggering a manual discovery, else it will return a ESKT_FAVORITENOTEMPTY error because the auto discovery is running.
You can start the manual discovery using a button as follow.:
CaptureHelperDevice deviceManager;
private void Capture_DeviceManagerArrival(object sender, CaptureHelper.DeviceArgs e)
{
deviceManager = e.CaptureDevice;
}
private async void Button_TriggerScan(object sender, EventArgs e)
{
await deviceManager.StartDeviceDiscoveryAsync(5000);
}
To receive the list of the discovered devices, register a handler to the Capture Helper event: DeviceDiscovered
.:
public MainPage()
{
InitializeComponent();
// initialize CaptureHelper
capture = new CaptureHelper();
// register for the Capture Helper events
capture.DeviceDiscovered += Capture_DeviceDiscovered;
}
private void Capture_DeviceDiscovered(object sender, CaptureHelper.DeviceDiscoveredArgs e)
{
string deviceDiscovered = e.Device;
}
The handler will receive a string each time a Socket Mobile Bluetooth LE device is discovered. This deviceDiscovered
string
will have a format similar to the one shown below.:
{
\"name\":\"S550\",
\"identifierUUID\":\"aca92313-bc43-0000-0000-000000000000\",
\"serviceUUID\":\"7a4375c9-f7c7-4e22-9afd-16v68fc588ca\"
}
Using the identifierUUID, you can connect to a specific Bluetooth LE device. See Connect to Specific Bluetooth LE Device
.
To be notified when the discovery ends, register a handler to the Capture Helper DiscoveryEnded
event.:
public MainPage()
{
InitializeComponent();
// initialize CaptureHelper
capture = new CaptureHelper();
// register for the Capture Helper events
capture.DiscoveryEnded += Capture_DiscoveryEnded;
}
private void Capture_DiscoveryEnded(object sender, CaptureHelper.DiscoveryEndedArgs e)
{
Debug.WriteLine("DiscoveryEnded");
}
Connect to Specific Bluetooth LE Device¶
The Unique Device Identifier can be acquired via a Manual Discovery or directly from a connected device. It is used to connect to a specific Bluetooth LE device.
From Manual Discovery, you can extract the identifierUUID
from the string received via the Capture Helper DeviceDiscovered
event.:
private void Capture_DeviceDiscovered(object sender, CaptureHelper.DeviceDiscoveredArgs e)
{
string deviceDiscovered = e.Device;
// extract identifierUUID
deviceDiscovered = jsonString.Replace("\\\"", "\"");
var jsonDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(deviceDiscovered);
string deviceIdentifier = jsonDict["identifierUUID"];
}
Alternatively, you can retrieve the Unique Device Identifier from a connected Bluetooth LE device via the Capture
Helper DeviceArrival
event.:
private void Capture_DeviceArrival(object sender, CaptureHelper.DeviceDiscoveredArgs e)
{
CaptureHelperDevice device = e.CaptureDevice;
// get identifierUUID
device.GetDeviceIdentifierAsync().ContinueWith(result =>
{
string deviceIdentifier = result.Result.Identifier;
});
}
To establish a connection, set the Device Manager’s favorite as shown below.:
{
// deviceManager: device received via the Capture helper's DeviceManagerArrival event
// deviceIdentifier: Unique device identifier
deviceManager.SetFavoritesAsync(deviceIdentifier);
}
Reading Data¶
Once the connection is established successfully, data read from the device (NFC card. Smartphone wallet card, or Barcode) can
be received by registering a handler to the Capture Helper event: DecodedData
.:
public MainPage()
{
InitializeComponent();
// initialize CaptureHelper
capture = new CaptureHelper();
// register for the Capture Helper events
capture.DecodedData += Capture_DecodedData;
}