UWP Bluetooth LE Implementation

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. using System.Linq;  
  5. using System.Runtime.InteropServices.WindowsRuntime;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using Windows.Devices.Bluetooth;  
  9. using Windows.Devices.Bluetooth.GenericAttributeProfile;  
  10. using Windows.Devices.Enumeration;  
  11.   
  12. namespace BLE_Connected_Car_UI.Connectivity  
  13. {  
  14.   
  15.     class BluetoothImplementation  
  16.     {  
  17.         // Define BLE current device getters and setters.  
  18.         public BluetoothLEDevice currentDevice { getset; }  
  19.         // Define Devices List.  
  20.         public List<string> deviceList = new List<string>();  
  21.         // Define Service List.  
  22.         public List<string> serviceList = new List<string>();  
  23.         // Define Characteristic List.  
  24.         public List<string> characteristicList = new List<string>();  
  25.         // Define the selectedCharacteristic variable.  
  26.         private GattCharacteristic selectedCharacteristic;  
  27.         // Define the selectedService variable.  
  28.         private GattDeviceService selectedService;  
  29.         /// <summary>  
  30.         /// Characteristic index.  
  31.         /// </summary>  
  32.         private const int CHARACTERISTIC_INDEX = 0;  
  33.         /// <summary>  
  34.         /// Characteristic notification type Notify enable.  
  35.         /// </summary>  
  36.         private const GattClientCharacteristicConfigurationDescriptorValue CHARACTERISTIC_NOTIFICATION_TYPE = GattClientCharacteristicConfigurationDescriptorValue.Notify;  
  37.   
  38.         /// <summary>  
  39.         /// Get bluetooth devices.  
  40.         /// </summary>  
  41.         /// <returns>deviceList</returns>  
  42.         public async Task<List<string>> getDevices()  
  43.         {  
  44.             foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()))  
  45.             {  
  46.                 BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id);  
  47.                 // Add the dvice name into the list.  
  48.                 deviceList.Add(bleDevice.Name);  
  49.             }  
  50.             return deviceList;  
  51.         }  
  52.         /// <summary>  
  53.         /// Connect to the desired device.  
  54.         /// </summary>  
  55.         /// <param name="deviceName"></param>  
  56.         public async void selectDevice(string deviceName)  
  57.         {  
  58.             foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()))  
  59.             {  
  60.                 BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id);  
  61.                 // Check if the name of the device founded is EMU Bridge.  
  62.                 if (bleDevice.Name == deviceName)  
  63.                 {  
  64.                     // Save detected device in the current device variable.  
  65.                     currentDevice = bleDevice;  
  66.                     break;  
  67.                 }  
  68.             }  
  69.         }  
  70.         /// <summary>  
  71.         /// Return the services of the selected device.  
  72.         /// </summary>  
  73.         /// <param name="device"></param>  
  74.         /// <returns>service list</returns>  
  75.         public List<string> getServiceList(BluetoothLEDevice device)  
  76.         {  
  77.             foreach (var service in device.GattServices)  
  78.             {  
  79.                 serviceList.Add(service.Uuid.ToString());  
  80.             }  
  81.             return serviceList;  
  82.         }  
  83.         /// <summary>  
  84.         /// Select the defined service from the defined device.  
  85.         /// </summary>  
  86.         /// <param name="device"></param>  
  87.         /// <param name="service"></param>  
  88.         /// <returns>selected service</returns>  
  89.         public GattDeviceService selectService(BluetoothLEDevice device, string service)  
  90.         {  
  91.             return selectedService = device.GetGattService(new Guid(service));  
  92.         }  
  93.         /// <summary>  
  94.         /// Get all characteristics of the defined service.  
  95.         /// </summary>  
  96.         /// <param name="service"></param>  
  97.         /// <returns>characteristic list</returns>  
  98.         public List<string> getCharacteristicList(GattDeviceService service)  
  99.         {  
  100.             foreach (var characteristic in service.GetAllCharacteristics())  
  101.             {  
  102.                 characteristicList.Add(characteristic.Uuid.ToString());  
  103.             }  
  104.             return characteristicList;  
  105.         }  
  106.         /// <summary>  
  107.         /// Select the defined characteristic from the defined service.  
  108.         /// </summary>  
  109.         /// <param name="service"></param>  
  110.         /// <param name="characteristic"></param>  
  111.         /// <returns>selected characteristic</returns>  
  112.         public GattCharacteristic selectCharacteristic(GattDeviceService service, string characteristic)  
  113.         {  
  114.             return selectedCharacteristic = service.GetCharacteristics(new Guid(characteristic))[CHARACTERISTIC_INDEX];  
  115.         }  
  116.         /// <summary>  
  117.         /// Read value from characteristic. Reverse the result using Array.Reverse.  
  118.         /// </summary>  
  119.         /// <returns>read response</returns>  
  120.         public async Task<byte[]> read()  
  121.         {  
  122.             byte[] response = (await selectedCharacteristic.ReadValueAsync()).Value.ToArray();  
  123.             Array.Reverse(response, 0, response.Length);  
  124.             return response;  
  125.         }  
  126.         /// <summary>  
  127.         /// Write a byte[] into characteristic.  
  128.         /// </summary>  
  129.         /// <param name="characteristic"></param>  
  130.         /// <param name="data"></param>  
  131.         /// <returns>communication status</returns>  
  132.         public async Task<GattCommunicationStatus> write(GattCharacteristic characteristic, byte[] data)  
  133.         {  
  134.             return await characteristic.WriteValueAsync(data.AsBuffer(), GattWriteOption.WriteWithResponse);  
  135.         }  
  136.         /// <summary>  
  137.         /// Enable notifications from the specified characteristic.  
  138.         /// </summary>  
  139.         /// <param name="characteristic"></param>  
  140.         /// <returns>communication status</returns>  
  141.         public async Task<GattCommunicationStatus> enableNotifications(GattCharacteristic characteristic)  
  142.         {  
  143.             GattCommunicationStatus status = await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(CHARACTERISTIC_NOTIFICATION_TYPE);  
  144.             if (status == GattCommunicationStatus.Unreachable)  
  145.             {  
  146.                 Debug.WriteLine("Your device is unreachable!");  
  147.             }  
  148.             else  
  149.             {  
  150.                 Debug.WriteLine("Service initializated");  
  151.             }  
  152.             return status;  
  153.         }  
  154.   
  155.     }  
  156. }