0
votes

I created an app where I am able to connect to BLE device and fetch the GATT services and characteristics. But after I write something in GATT characteristics I get pairing request dialog in iOS and the characteristics value becomes null after I accept the pairing request. I don't know why this pairing request is coming only when I'm writing in my GATT characteristics. And BLE doesn't ask for pairing in iOS as per my research. Even though the pairing request dialog is coming I need the pairing request to come after connection or before connection not after write operation. This is the image of the pairing request I'm receiving.

enter image description here

This is my code where I'm connecting and writing to GATT server

 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("00002A39-0000-1000-8000-00805F9B34FB"));
               
                    try
                    {
                        
                     
                        if (characteristic != null)
                        {
                        var sbnew = new StringBuilder("BLE Characteristics\n");
                        byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "Judson was here" : SendMessageLabel.Text);
                            await Task.Delay(300);
                            var newbytes = await characteristic.WriteAsync(senddata);
                        
                            var bytes = await characteristic.ReadAsync();
                        var str = Encoding.UTF8.GetString(bytes);

                        sbnew.AppendLine($"Characteristics found on this device: {string.Join(", ", str.ToString())}");
                        CharactericsLabel.Text = sbnew.ToString();
                       
                    }

                }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                
                return CharactericsLabel.Text;
           
            }
            catch (Exception ex)
            {
                
                return ex.ToString();
            }
 }

My characteristic value becomes null just after WriteAsync method. I have no clue how to fix this any suggestions?

1
The characteristic you are writing too must have encryption required. If encryption is required then pairing/bonding is required. Remove the requirement for encryption and the pairing request will not be shown.Paulw11
@Paulw11 how can I remove encryption? can u give me an example please.Judson Abraham
It will be in your peripheral code, which you haven't shown.Paulw11
@Paulw11 Can u share the code to remove encryption because I'm not getting it.Judson Abraham
Is there any other peripheral code in your project?Jack Hua

1 Answers

1
votes

There is no permission given for the write operation from peripheral side. I added this code for my characteristic in peripheral side and it worked

BluetoothGattCharacteristic sampleText = new BluetoothGattCharacteristic(SAMPLE_TEXT,
        //Read-only characteristic
        BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
        BluetoothGattCharacteristic.PERMISSION_WRITE);