Forum Libraries, Components, and Plugins
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Plugin.BluetoothLE WriteWithoutResponse not working on iOS

Hello
I have made a project where I call Write() on a characteristic and WriteWithoutResponse() on another characteristic. Characteristics on the peripheral of cause support the specific kinds of writes, but when WriteWithoutResponse() are called no data appear in the characteristic on peripheral GATT Server. I have tried with nRF Connect to do the same kind of writes to the characteristics and this works fine. Any idea why no data is written with WriteWithoutResponse()?

I'm using Plugin.BluetoothLE v6.2.3 and Xamarin.Forms v4.7.01239 in Visual Studio 19. My iPhone SE is updated to the latest 13.6

I have included a code snippet that writes to either characteristic when they are discovered. When Write() is called data is written, but the subscribed handler is never called. When WriteWithoutResponse is called no data is written, but the subscribed handler is called. Also when writing wrong pin the gatt server responds with 'write not permitted', but this is not caught as an exception.

private void OnCharacteristicsDiscovered(Plugin.BluetoothLE.IGattCharacteristic characteristic)
{
Console.WriteLine("OnCharacteristicsDiscovered - Discovered");

if (characteristic.Uuid.Equals(PinCodeCharacteristicUUID))
{
    GattPinCharacteristic = characteristic;
    try
    {
        if(GattPinCharacteristic.CanWrite())
        {
            IDisposable disposableWrite;
            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();

            Dev_state = DEV_state.Writing_Pin;

            Console.WriteLine("OnCharacteristicsDiscovered - Writing valid pin");
            disposableWrite = GattPinCharacteristic.Write(BitConverter.GetBytes((ushort)1111)).Subscribe((CharacteristicGattResult result) =>
            {
                Console.WriteLine("OnCharacteristicsDiscovered - VALID PIN WRITTEN - " + BitConverter.ToString(result.Data));
                tcs.SetResult(1);
            });
            bool completed = tcs.Task.Wait(2000);
            Console.WriteLine("OnCharacteristicsDiscovered - Disposing disposableWrite - " + (completed ? "Completed" : "Timeout"));
            disposableWrite.Dispose();

            Console.WriteLine("OnCharacteristicsDiscovered - Writing invalid pin");
            disposableWrite = GattPinCharacteristic.Write(BitConverter.GetBytes((ushort)2222)).Subscribe((CharacteristicGattResult result) => 
            {
                Console.WriteLine("OnCharacteristicsDiscovered - INVALID PIN WRITTEN - " + BitConverter.ToString(result.Data));
                tcs.SetResult(1);
            });
            completed = tcs.Task.Wait(2000);
            Console.WriteLine("OnCharacteristicsDiscovered - Disposing disposableWrite - " + (completed ? "Completed" : "Timeout"));
            disposableWrite.Dispose();

            Console.WriteLine("OnCharacteristicsDiscovered - Should not go here, since an invalid write should be caught as exception");
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("OnCharacteristicsDiscovered - Failed to write PIN " + ex.Message);
    }
}
else if(characteristic.Uuid.Equals(DataCharacteristicUUID))
{
    GattDataCharacteristic = characteristic;
    try
    {
        if (GattDataCharacteristic.CanWrite())
        {
            IDisposable disposableWrite;
            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();

            Dev_state = DEV_state.WriteData;

            Console.WriteLine("OnCharacteristicsDiscovered - Write Data");
            disposableWrite = GattDataCharacteristic.WriteWithoutResponse(BitConverter.GetBytes((ushort)1111)).Subscribe((CharacteristicGattResult result) =>
            {
                Console.WriteLine("OnCharacteristicsDiscovered - DATA WRITTEN - " + BitConverter.ToString(result.Data));
                tcs.SetResult(1);
            });
            bool completed = tcs.Task.Wait(2000);
            Console.WriteLine("OnCharacteristicsDiscovered - Disposing disposableWrite - " + (completed ? "Completed" : "Timeout"));
            disposableWrite.Dispose();
        }

        Task.Run(() =>
        {
            Thread.Sleep(10000);
            Console.WriteLine("OnCharacteristicsDiscovered - DISCONNECTING");
            disposableGattService?.Dispose();
            GattDataCharacteristic.Service.Device.CancelConnection();
        });
    }
    catch (Exception ex)
    {
        Console.WriteLine("OnCharacteristicsDiscovered - Failed to write Data " + ex.Message);
    }
}

}

Sign In or Register to comment.