• Clear, actually my laptop console application is connected to the Nordic UART service, so it should work. Unfortunately, by now, the bangle receives only the first data buffer: when I tried to send it again, no event is triggered.

        internal class Program
        {
            static DeviceInformation _device = null;
            private static string NORDIC_UART_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
            static DataFormat _dataFormat = DataFormat.UTF8;
    
            static GattCommunicationStatus result;
            static GattCharacteristic _characteristic;
            static GattLocalCharacteristic _readcharacteristic;
    
    
            static GattServiceProvider _serviceProvider;
            static Guid _serviceuuid= new Guid("F150E6C7-0DB4-4645-AE74-023C395983­72");
            static Guid _characteristicuuid = new Guid("B952F9C0-218D-42B6-8EE6-4AAB357539­22");
            static GattLocalCharacteristicParameters _characteristicparameter = new GattLocalCharacteristicParameters();
    
            static DataWriter writer = new DataWriter();
    
            static async Task Main(string[] args)
            {
                // Query for extra properties you want returned
                string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
    
                DeviceWatcher deviceWatcher =
                            DeviceInformation.CreateWatcher(
                                    BluetoothLEDevice.GetDeviceSelectorFromP­airingState(false),
                                    requestedProperties,
                                    DeviceInformationKind.AssociationEndpoin­t);
    
                // Register event handlers before starting the watcher.
                // Added, Updated and Removed are required to get all nearby devices
                deviceWatcher.Added += DeviceWatcher_Added;
                deviceWatcher.Updated += DeviceWatcher_Updated;
                deviceWatcher.Removed += DeviceWatcher_Removed;
                
    
                // EnumerationCompleted and Stopped are optional to implement.
                deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
                deviceWatcher.Stopped += DeviceWatcher_Stopped;
    
                // Start the watcher.
                deviceWatcher.Start();
    
                //STARTING CREATING SERVICE
    
                GattServiceProviderResult _result = await GattServiceProvider.CreateAsync(_service­uuid);
                Console.WriteLine("Creating new service");
    
                if (_result.Error == BluetoothError.Success)
                {
                    _serviceProvider = _result.ServiceProvider;
                    Console.WriteLine($"{_result.Error}");
                }
                byte[] value = new byte[] { 0x21 };
    
                _characteristicparameter.WriteProtection­Level = GattProtectionLevel.Plain;
                _characteristicparameter.ReadProtectionL­evel = GattProtectionLevel.Plain;
                _characteristicparameter.StaticValue = value.AsBuffer();
                _characteristicparameter.UserDescription­ = "Read Characteristic";
                _characteristicparameter.CharacteristicP­roperties = GattCharacteristicProperties.Read; 
                
    
                GattLocalCharacteristicResult characteristicResult = await _serviceProvider.Service.CreateCharacter­isticAsync(_characteristicuuid, _characteristicparameter);
    
                if (characteristicResult.Error != BluetoothError.Success)
                {
                    Console.WriteLine("Errore nella creazione della caratteristica di Read");
                    return;
                }
                _readcharacteristic = characteristicResult.Characteristic;
                Console.WriteLine("Characteristic " + _characteristicuuid + " is a " + _readcharacteristic.CharacteristicProper­ties + " characteristic");
                _readcharacteristic.ReadRequested += ReadRequested;
    
                GattServiceProviderAdvertisingParameters­ advParameters = new GattServiceProviderAdvertisingParameters­
                {         
                    IsDiscoverable = true,
                    IsConnectable = true
                };
                
                
                _serviceProvider.StartAdvertising(advPar­ameters);
                Console.WriteLine(_serviceProvider.Adver­tisementStatus.ToString());
                Console.WriteLine("The service started successfully and has " + _serviceProvider.Service.Characteristics­.Count + " characteristics");
    
                while (true)
                {
                    if (_device == null)
                    {
                        Thread.Sleep(200);
                    }
                    else
                    {
                        Console.WriteLine("Press any key to connect to the Bangle. Js");
                        Console.ReadKey();
                        BluetoothLEDevice bluetoothLEDevice = await BluetoothLEDevice.FromIdAsync(_device.Id­);
                        Console.WriteLine("Pairing with the device");
    
                        GattDeviceServicesResult servicesResult = await bluetoothLEDevice.GetGattServicesAsync()­;
    
                        if (servicesResult.Status == GattCommunicationStatus.Success)
                        {
                            Console.WriteLine("Pairing succeed");
                            var services = servicesResult.Services;
                            foreach (var service in services)
                            {
                                Console.WriteLine($"Service: {service.Uuid}");
                                Console.WriteLine("---------------------­--");
    
                                if (service.Uuid.ToString() == NORDIC_UART_SERVICE)
                                {
                                    Console.WriteLine("Connected to NUS Service");
                                    Console.WriteLine("---------------------­-------------");
    
                                    GattCharacteristicsResult characteristicsResult = await service.GetCharacteristicsAsync();
                                  
                                    if (characteristicsResult.Status == GattCommunicationStatus.Success)
                                    {
                                        var characteristics = characteristicsResult.Characteristics;
                                        foreach (var characteristic in characteristics)
                                        {
                                            Console.WriteLine($"Characteristic {characteristic.Uuid}");
                                            Console.WriteLine("---------------------­-----------");
                                            GattCharacteristicProperties properties = characteristic.CharacteristicProperties;­
    
                                            if (properties.HasFlag(GattCharacteristicPr­operties.Notify))
                                            {
                                                Console.WriteLine("Notify property found");
    
                                                GattCommunicationStatus status = await characteristic.WriteClientCharacteristic­ConfigurationDescriptorAsync(GattClientC­haracteristicConfigurationDescriptorValu­e.Notify);
                                                if (status == GattCommunicationStatus.Success)
                                                {
                                                    Console.WriteLine("Server has been informed of clients interest and has set the client in notify status");
                                                    characteristic.ValueChanged += Characteristic_ValueChanged;
                                                    _characteristic = characteristic;
                                                    
                                                }
                                            }
    
                                            else if (properties.HasFlag(GattCharacteristicPr­operties.Write))
                                            {
                                                Console.WriteLine($"\t \t Characteristic {characteristic.Uuid} has a write property ");
                                                Console.WriteLine("---------------------­----------- ");
                                                
                                                writer.WriteString("Data sent");
                                                                                           
                                                result = await characteristic.WriteValueAsync(writer.De­tachBuffer());
                                                if (result == GattCommunicationStatus.Success)
                                                {
                                                    Console.WriteLine("Data sent");  
                                                }
                                            }
                                            else if (properties.HasFlag(GattCharacteristicPr­operties.WriteWithoutResponse))
                                            {
                                                Console.WriteLine($"\t \t Characteristic {characteristic.Uuid} has a write withoutresponse property ");
                                                Console.WriteLine("---------------------­----------- ");
    
                                            }
                                        }
                                    }
                                }
                            }
                        }
    
                        Console.WriteLine("Press space to disconnect or q to exit");
    
                             
                        if (Console.ReadKey().Key == ConsoleKey.Spacebar)
                        {
                            bluetoothLEDevice.Dispose();
                            return;
                        }
                        else if (Console.ReadKey().Key == ConsoleKey.Q)
                        {
                            Console.WriteLine("Closing");
                            Thread.Sleep(1000);
                            break;
                        }
    
                        if (Console.ReadKey().Key == ConsoleKey.Enter)
                        {
                            writer.WriteByte(0x12);
    
                            result = await _characteristic.WriteValueAsync(writer.D­etachBuffer());
                            if (result == GattCommunicationStatus.Success)
                            {
                                Console.WriteLine("Data sent");
                            }
                        }
    
                    }
                    
                }
    
            }
    
    
About

Avatar for Riccardokhm @Riccardokhm started