7

I am working on a BLE application. I am able to connect to MI band and get the services through my Xamarin forms BLE app. But when I am trying to write characteristics I am getting exception. I'm getting exception

Characteristic does not support write.

for the method WriteAsync(). This is my code where I am writing to characteristics:

private async Task<string> ProcessDeviceInformationService(IService deviceInfoService)
    {
        try
        {
           await adapter.ConnectToDeviceAsync(device);
            var sb = new StringBuilder("Getting information from Device Information service: \n");
            var characteristics = await deviceInfoService.GetCharacteristicsAsync();
            var characteristic = await deviceInfoService.GetCharacteristicAsync(Guid.Parse("00002A27-0000-1000-8000-00805F9B34FB"));
           // characteristic.CanWrite = true;
            //foreach (var characteristic in characteristics)
            //{
                try
                {
                   // await Task.Delay(300);
                    var bytes = await characteristic.ReadAsync();
                    var str = Encoding.UTF8.GetString(bytes, 0, 0);
                    ManufacturerLabel.Text = str;
                    //var characteristic = await deviceInfoService.GetCharacteristicAsync(GattCharacteristicIdentifiers.DataExchange);
                    if (characteristic != null)
                    {
                        byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "jenx.si was here" : SendMessageLabel.Text);
                        await Task.Delay(300);
                        var newbytes = await characteristic.WriteAsync(senddata);
                        var strnew = Encoding.UTF8.GetString(senddata, 0, 0);
                        ManufacturerLabel.Text = newbytes.ToString();
                        //var strnew = Encoding.UTF8.GetString(newbytes, 0, 0);
                    }
                   
                   // ManufacturerLabel.Text = str;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            //}

            return sb.ToString();
        }

I have no clue how to fix this any suggestions?

Cfun
  • 8,442
  • 4
  • 30
  • 62
Judson Abraham
  • 316
  • 2
  • 11
  • 37
  • 1
    You read/write are backwards. You want to read the characteristics from the BLE and write results to a byte array. You are writing the characteristics from a byte array. – jdweng Nov 05 '20 at 21:47
  • @jdweng can u explain it through code. Sorry I'm new to this. – Judson Abraham Nov 05 '20 at 21:56
  • 1
    Following is writing to controlCharacteristic. You want to read.: controlCharacteristic.WriteAsync(new byte[] {COMMAND_BYTE, COMMAND_HEART_RATE_CONTINUOUS, COMMAND_ON}); – jdweng Nov 05 '20 at 23:15
  • @jdweng I'm reading the characterics aswell before writing it to byte array. I'm editing my post u can look into it. – Judson Abraham Nov 06 '20 at 09:50
  • See code on following page. The Write parameter is an enumeration (not a byte array) : https://github.com/jeroendesloovere/examples-windows/blob/master/Bluetooth%20Generic%20Attribute%20Profile%20-%20Heart%20Rate%20Service/C%23/Shared/HeartRateService.cs – jdweng Nov 06 '20 at 10:41
  • @jdweng That project is for windows will it work for android? – Judson Abraham Nov 06 '20 at 13:42
  • @jdweng do u know any xamarin project where it is used? – Judson Abraham Nov 06 '20 at 13:43
  • Visual Studio 4.7.2 and later you can write the same c# code for both Windows (Net) and Android/Xamarin (Core) and you can target the code for either Net or Core. The source code doesn't change. In some cases Core is a compact version of Net and doesn't include all the libraries. When a library doesn't exist you may need to add a Github library if one exists. The source doesn't usually have to change if the library is available in both Core and Net. – jdweng Nov 06 '20 at 13:58

1 Answers1

3

I resolved this. First of all we need to check if the Characteristic has the write operation in it for that you can download an app called BLE scanner from play store and connect to that device. When we connect to that BLE we will be able to see the available services and characteristics of the BLE Peripheral. And there we need to check which characteristics has write operation. So if you try to do write characteristics for characteristics which doesn't have a write operation in peripheral it will give exception write not permitted.

Judson Abraham
  • 316
  • 2
  • 11
  • 37