Judson Abraham
Judson Abraham

Reputation: 412

Why I'm getting pairing request dialog in iOS for BLE after write operation in xamarin forms?

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?

Upvotes: 0

Views: 907

Answers (1)

Judson Abraham
Judson Abraham

Reputation: 412

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);

Upvotes: 1

Related Questions