Getting exception Read async error: "The handle is invalid." for BLE in xamarin forms

Viewed 236

I'm trying to implement BLE application in Xamarin forms. But the read operation gives me exception even though I gave permission for read in peripheral.

{Plugin.BLE.Abstractions.Exceptions.CharacteristicReadException: Read async error: Error Domain=CBATTErrorDomain Code=1 "The handle is invalid." UserInfo={NSLocalizedDescription=The handle is invalid.}

This is my BLE code

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 =  deviceInfoService.GetCharacteristicsAsync();
                var characteristic = await deviceInfoService.GetCharacteristicAsync(Guid.Parse("00002B0F-0000-1000-8000-00805F9B34FB"));
                
                try
                {
                    
                    if (characteristic != null)
                    {
                        var sbnew = new StringBuilder("BLE Characteristics\n");
                        byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "12" : SendMessageLabel.Text);

 
                    MainThread.BeginInvokeOnMainThread(async () =>
                       {
                        
                        characteristic.ValueUpdated += (o, args) =>
                          {
                        
                         var bytes = characteristic.Value;
                                                 };
                                               await characteristic.WriteAsync(senddata);

                                                                                              string str = Encoding.UTF8.GetString(senddata);

  sbnew.AppendLine($"Characteristics found on this device: {string.Join(", ", str.ToString())}");
                            CharactericsLabel.Text = sbnew.ToString();
                        }
                }
                catch (Exception ex)
                {
                    //return ex.Message;
                    DisplayAlert("Notice", ex.Message.ToString(), "OK");
               }

This is the code of my peripheral

// Current Time characteristic
BluetoothGattCharacteristic currentTime = new BluetoothGattCharacteristic(CURRENT_TIME,
        //Read-only characteristic, supports notifications
        BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PROPERTY_WRITE);
BluetoothGattDescriptor configDescriptor = new BluetoothGattDescriptor(CLIENT_CONFIG,
        //Read/write descriptor
        BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE);
currentTime.addDescriptor(configDescriptor);
// Local Time Information characteristic
BluetoothGattCharacteristic localTime = new BluetoothGattCharacteristic(LOCAL_TIME_INFO,
        //Read-only characteristic
        BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ);
BluetoothGattCharacteristic sampleText = new BluetoothGattCharacteristic(SAMPLE_TEXT,
        //Read-only characteristic
        BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
        BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ);

I'm using characteristic sampleText.I'm using Plugin.Ble 2.0.1 latest version and iOS version 14.0 . I have no clue how to fix this any suggestions.

0 Answers
Related