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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With