Philip Chen

Philip Chen

  • 2.1k
  • 50
  • 611

How to read data from a Bluetooth LE Sensor?

Aug 5 2021 4:18 AM

I am totally new to Bluetooth LE programming. My task is to write a app to read in data from a skincare device called a clartici. It has a sensor that will read the skin and send the data to a computer.

I was able to detect and pair the device to the program.

I got stuck in the part where I have to read in the data. I have all the GUIDs I need for the device, but I just don't know how to use them. SHould I use advertising here?

Here is the code I have so far:

namespace DermaScanner
{
    class Program
    {
        static DeviceInformation device = null;

        public static string ClarticiID = "837d"; //Service GUID - 64E9837D-CA48-48C4-BB71-323E5A85B51F
        public static string deviceInfo = "180a"; //Device info GUID

        static async Task Main(string[] args)
        {
            // Query for extra properties you want returned
            string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };

            DeviceWatcher watcher =
                        DeviceInformation.CreateWatcher(
                                BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
                                requestedProperties,
                                DeviceInformationKind.AssociationEndpoint);

            // Register event handlers before starting the watcher.
            // Added, Updated and Removed are required to get all nearby devices
            watcher.Added += DeviceWatcher_Added;
            watcher.Updated += DeviceWatcher_Updated;
            watcher.Removed += DeviceWatcher_Removed;

            // EnumerationCompleted and Stopped are optional to implement.
            watcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            watcher.Stopped += DeviceWatcher_Stopped;

            Console.WriteLine("Clartici Scanner");
            // Start the watcher.
            watcher.Start();
            while (true)
            {
                if (device == null)
                {
                    Thread.Sleep(200);
                }
                else
                {
                    Console.WriteLine("Press Any to pair with Clartici");
                    Console.ReadKey();
                    Console.WriteLine();
                    BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
                    Console.WriteLine("Attempting to pair with device");
                    GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();


                    if (result.Status == GattCommunicationStatus.Success)
                    {

                        Console.WriteLine("Pairing succeeded");
                        var services = result.Services;
                        foreach (var service in services)
                        {
                            
                            if (service.Uuid.ToString("N").Substring(4, 4) == ClarticiID)
                            {
                                Console.WriteLine("Found Clartici service!");
                                
                                
                            }
                        }
                    }

                    break;
                }
            }
            watcher.Stop();
            Console.ReadLine();
        }

        //Looking for device
        private static void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
        {
            if (args.Name == "Clartici")
                device = args;
            //throw new NotImplementedException();

        }
        private static void DeviceWatcher_Stopped(DeviceWatcher sender, object args)
        {
            //throw new NotImplementedException();
        }

        private static void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
        {
            //throw new NotImplementedException();
        }

        private static void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
        {
            //throw new NotImplementedException();
        }

        private static void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
        {
            //throw new NotImplementedException();
        }
    }
}

Answers (1)