none
c# program cannot find GATT services RRS feed

  • Question

  • I wrote a c# console application that tries to read/write to a BLE device. (The device is a u-blox EVK-NINA-B1 ). The program successfully connects to the device. However when I try to look for services provided by the BLE device it ONLY RETURNS 3, even though the BLE device has more services. (I checked using LightBlue app on my iphone). Then, I tried programming another ble device (a different u-blox EVK-NINA-B1) with the same program as the first ble device, and my console application program can connect but RETURNS 0 services. Does anyone know why this happens? Any help will be greatly appreciated.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Devices.Bluetooth;
    using Windows.Devices.Bluetooth.Advertisement;
    using Windows.Devices.Bluetooth.GenericAttributeProfile;
    using Windows.Storage.Streams;
    
    namespace app_target
    {
        class Program
       {
           private Guid OutputCommand = new Guid("0A0BEADC-4F70-4C05-8D67-
    BBC792CCF406");
        private Guid IO = new Guid("00004F0E-1212-efde-1523-785feabcd123");
        BluetoothLEDevice _device;
        IReadOnlyList<GattDeviceService> _services;
        IReadOnlyList<GattDeviceService> _allcharacteristics;
        GattDeviceService _service;
        GattCharacteristic _characteristic;
        GattDeviceService selectedService;
        private const int bytesPerRow = 16;
        private const int bytesPerSegment = 2;
        public uint chunkSize = 4096;
        string text = " ";
    
        private Guid service = new Guid("00001801-0000-1000-8000-00805f9b34fb");
        private Guid service2 = new Guid("0000a000-0000-1000-8000-
    00805f9b34fb");
    
        static void Main(string[] args)
        {
            // Start the program
            var program = new Program();
    
            // Close on key press
            Console.ReadLine();
        }
        public int i = 0;
        public Program()
        {
            // Create Bluetooth Listener
            var watcher = new BluetoothLEAdvertisementWatcher();
    
            watcher.ScanningMode = BluetoothLEScanningMode.Active;
    
            // Only activate the watcher when we're recieving values >= -80
            watcher.SignalStrengthFilter.InRangeThresholdInDBm = -80;
    
            // Stop watching if the value drops below -90 (user walked away)
            watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -90;
    
            // Register callback for when we see an advertisements
            watcher.Received += OnAdvertisementReceived;
    
            // Wait 5 seconds to make sure the device is really out of range
            watcher.SignalStrengthFilter.OutOfRangeTimeout = 
    TimeSpan.FromMilliseconds(5000);
            watcher.SignalStrengthFilter.SamplingInterval = 
    TimeSpan.FromMilliseconds(2000);
    
            // Starting watching for advertisements
            watcher.Start();
    
        }
    
        async void DeviceConnected(BluetoothLEDevice deviceoperations)
        {
    
            Console.WriteLine("Device Connected Method");
            int number = deviceoperations.GattServices.Count;
            Console.WriteLine("DEVICE NAME {0}", 
    deviceoperations.DeviceInformation.Name);
            Console.WriteLine("Number of services {0} ", number);
            _services = deviceoperations.GattServices;
    
            foreach (var service in deviceoperations.GattServices)
            {
                Console.WriteLine(service.Uuid);
            }
    
            int i = 0;
    
            foreach (GattDeviceService s in _services)
            {
                Console.WriteLine("num {0}", i);
                Console.WriteLine(s.Uuid);
                if (s.Uuid.Equals(service2))// == '00001801 - 0000 - 1000 - 8000 
    - 00805f9b34fb')//new Guid("00001801-0000-1000-8000-00805f9b34fb"))
                {
                    Console.WriteLine("blah");
                    Console.WriteLine(s.Uuid);
                    _service = s;
                    break;
                    Console.WriteLine("Service Found");
                    Console.WriteLine("Connected to service: ", _service.Uuid);
                }
                i++;
            }
    
            Console.WriteLine("blah");
            Console.WriteLine(_service.Uuid);
    
            foreach (var c in _service.GetCharacteristics(new Guid("0000a002-
    0000-1000-8000-00805f9b34fb")))
            {
                _characteristic = c;
                Console.WriteLine("Characteristic found");
                Console.WriteLine(c.Uuid);
            }
    
            GattCharacteristicProperties properties = 
    _characteristic.CharacteristicProperties;
    
            if (properties.HasFlag(GattCharacteristicProperties.Read))
            {
                // This characteristic supports reading from it.
                Console.WriteLine("Supports Reading");
    
            }
            if (properties.HasFlag(GattCharacteristicProperties.Write))
            {
                Console.WriteLine("Supports writing");
                // This characteristic supports reading from it.
    
            }
            if (properties.HasFlag(GattCharacteristicProperties.Notify))
            {
                Console.WriteLine("Supports Subscribing notifications");
                // This characteristic supports subscribing to notifications.
            }
    
            DataWriter writer = new DataWriter();
            byte[] data = new byte[] { 0x01 };
            writer.WriteBytes(data);
            GattCommunicationStatus resultado = 0;//-->need this?
            var buf = writer.DetachBuffer();//-->need this??
            resultado = await _characteristic.WriteValueAsync(buf);//-->need 
    this?
    
            Console.WriteLine("SUCSESSS!!");
    
            Console.WriteLine("Calling Readng Method");
    
    
        }
    
    
        async void Read(GattReadResult result)
        {
            Console.WriteLine("Entered reading method");
    
            if (result.Status == GattCommunicationStatus.Success)
            {
    
                DataReader reader = DataReader.FromBuffer(result.Value);
                byte[] input = new byte[reader.UnconsumedBufferLength];
                string text = reader.ReadString(result.Value.Length);
                // reader.ReadBytes(input);
                //reader.FromBuffer
                // await reader.LoadAsync(chunkSize);
                //IAsyncOperation<uint> taskload = reader.LoadAsync(chunkSize);
                // taskload.AsTask().Wait();
                Console.WriteLine("Status == Success");
                Console.WriteLine("data {0}", text);
                var information = " ";
                Console.WriteLine("size", reader.UnconsumedBufferLength);
                while (reader.UnconsumedBufferLength > 0)
                {
                    float bytesToRead = reader.ReadSingle();
                    // information += reader.ReadString(bytesToRead);
                    Console.Write("DATA: ", bytesToRead);
                }
            }
    
           }
    
        async void ConnectDevice(BluetoothLEAdvertisementReceivedEventArgs 
    eventArgs)
        {
            BluetoothLEDevice device = await 
    BluetoothLEDevice.FromBluetoothAddressAsync(eventArgs.BluetoothAddress);
            // var service = device.GetGattService()
            Console.WriteLine("Device Connected");
            DeviceConnected(device);
    
    
        }
    
    
    
    
        private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
            // Tell the user we see an advertisement and print some properties
            Console.WriteLine(String.Format("Advertisement:"));
            Console.WriteLine(String.Format("  BT_ADDR: {0}", eventArgs.BluetoothAddress));
            Console.WriteLine(String.Format("  FR_NAME: {0}", eventArgs.Advertisement.LocalName));
            Console.WriteLine("name {0}", eventArgs.Advertisement.LocalName);
            Console.WriteLine("blah : ", eventArgs.BluetoothAddress.GetType());
            Console.WriteLine();
            string name = "LED\u0000";
            string namet = eventArgs.Advertisement.LocalName;
            // Console.WriteLine("nameafter{0} ", namet);
            Console.WriteLine(name.Length);
            Console.WriteLine(namet.Length);
            //String b2 = String.Format(eventArgs.BluetoothAddress);         
            i++;
            if (i == 6)
            {
                Console.ReadKey();
            }
            bool result = string.Equals(namet, name, StringComparison.Ordinal);//(name, StringComparison.Ordinal);
            Console.WriteLine(result.ToString());
            if (result)
            {
                ConnectDevice(eventArgs);
            }
    
        }
    }
    }

    Tuesday, August 29, 2017 9:42 PM