0

I am trying to make my own Bluethoot client from the basic example in Microsoft combined with some yputube videos.

Everything started good but when i find MyDevice name and try to pass the "device.Id" for connection the function "BluetoothLEDevice.FromIdAsync" doesn't accept the ID: "Bluetooth#Bluetooth70:a8:d3:e4:b0:0a-xx:xx:xx:xx:xx:xx" and crashes, because it only accepts ID starting with BluetoothLE#BluetoothLE. Anyone with workaround from this issue.

The bluethoot of the device i am trying to connect is a GATT BluethootLE but with id that is not as usuall. So apparently it works because i am connecting to it with my phone.

Below is my simple code:

your text`using System;
your text`using System.Collections.Generic;
your text`using System.Diagnostics.Tracing;
your text`using System.Linq;
your text`using System.Text;
your text`using System.Threading;
your text`using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;

namespace QuickBlueToothLE
{
    class Program
    {
        static DeviceInformation device = null;
        static BluetoothLEAdvertisementReceivedEventArgs advdevice = null;
        //;public static string HEART_RATE_SERVICE_ID = "180d";
        public static string VEHICLE_DATA_SERVICE_ID = "a4cf970a";
        static async Task Main(string[] args)
        {
            // Query for extra properties you want returned
            //string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected"};

            string BleSelector = "System.Devices.DevObjectType:=5 AND (System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" OR System.Devices.Aep.ProtocolId:=\"{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}\")";
            string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.ProtocolId" };


            DeviceWatcher deviceWatcher =
            DeviceInformation.CreateWatcher(
                    BleSelector,
                    requestedProperties,
                    DeviceInformationKind.AssociationEndpoint);

            deviceWatcher.Added += DeviceWatcher_Added;
            deviceWatcher.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;  // += subscribe

            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;

            // Start the watcher.
            deviceWatcher.Start();
            while (true)
            {
                if (device == null)
                {
                    Thread.Sleep(200);
                }
                else
                {
                    Console.WriteLine("Press Any to pair with MBUX");
                    Console.ReadKey();

                   BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
                  
                    Console.WriteLine("Attempting to pair with device");
                    GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
                    Console.WriteLine(result.Status.ToString());
                    
                    //GattDeviceServicesResult result = await bluetoothLeDevice.GetRfcommServicesAsync();
                    if (result.Status == GattCommunicationStatus.Success)
                    {
                        Console.WriteLine("Pairing succeeded");
                        var services = result.Services;
                        foreach (var service in services)
                        {
                            if (service.Uuid.ToString("N").Substring(0, 8) == VEHICLE_DATA_SERVICE_ID)
                            {
                                Console.WriteLine("Found vehicle data service");
                                GattCharacteristicsResult charactiristicResult = await service.GetCharacteristicsAsync();

                                if (charactiristicResult.Status == GattCommunicationStatus.Success)
                                {
                                    var characteristics = charactiristicResult.Characteristics;
                                    foreach (var characteristic in characteristics)
                                    {
                                        Console.WriteLine("---------------");
                                        Console.WriteLine(characteristic);
                                        GattCharacteristicProperties properties = characteristic.CharacteristicProperties;

                                        if (properties.HasFlag(GattCharacteristicProperties.Read))
                                        {
                                            Console.WriteLine("Read poroperty found");


                                            GattReadResult Read_result = await characteristic.ReadValueAsync();
                                            if (Read_result.Status == GattCommunicationStatus.Success)
                                            {
                                                var reader = DataReader.FromBuffer(Read_result.Value);
                                                byte[] input = new byte[reader.UnconsumedBufferLength];
                                                reader.ReadBytes(input);
                                                Console.WriteLine(input.ToString());
                                            }

                                        }

                                    }
                                }
                            }
                        }
                    }

                    Console.WriteLine("Press Any Key to Exit application");
                    Console.ReadKey();
                    break;
                }
            }
            deviceWatcher.Stop();
        }


        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();
        }

        private static void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
        {
            Console.WriteLine(args.Name);
            if (args.Name == "MyDevice")
                //if (args.Id == "Bluetooth#Bluetooth70:a8:d3:e4:b0:0a-xx:xx:xx:xx:xx:xx")
                device = args;
           
        }
       
    }
}

I havent try much more from this because i cannot change the ID of the device that i want to connect with.

0 Answers0