xamarin.androidandroid-bluetooth

Xamarin.Android using ParcelUuid cause a crash


I try to make BLE Server on android

try
            {
                _bluetoothManager = (BluetoothManager)ctx.GetSystemService(Context.BluetoothService);
                _bluetoothAdapter = _bluetoothManager.Adapter;

                _bluettothServerCallback = new BleGattServerCallback();
                _bluetoothServer = _bluetoothManager.OpenGattServer(ctx, _bluettothServerCallback);

                var service = new BluetoothGattService(UUID.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"),
                    GattServiceType.Primary);
                _characteristic = new BluetoothGattCharacteristic(UUID.FromString("d8de624e-140f-4a22-8594-e2216b84a5f2"), GattProperty.Read | GattProperty.Notify | GattProperty.Write, GattPermission.Read | GattPermission.Write);
                _characteristic.AddDescriptor(new BluetoothGattDescriptor(UUID.FromString("28765900-7498-4bd4-aa9e-46c4a4fb7b07"),
                    GattDescriptorPermission.Read | GattDescriptorPermission.Write));

                service.AddCharacteristic(_characteristic);

                _bluetoothServer.AddService(service);

                _bluettothServerCallback.CharacteristicReadRequest += _bluettothServerCallback_CharacteristicReadRequest;
                _bluettothServerCallback.NotificationSent += _bluettothServerCallback_NotificationSent;

                Debug.WriteLine("Server created!");

                BluetoothLeAdvertiser myBluetoothLeAdvertiser = _bluetoothAdapter.BluetoothLeAdvertiser;

                var builder = new AdvertiseSettings.Builder();
                builder.SetAdvertiseMode(AdvertiseMode.Balanced);
                builder.SetConnectable(true);
                builder.SetTimeout(0);
                builder.SetTxPowerLevel(AdvertiseTx.PowerMedium);
                AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
                dataBuilder.SetIncludeDeviceName(true);
                dataBuilder.AddServiceUuid(new ParcelUuid(UUID.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5")));
                dataBuilder.SetIncludeTxPowerLevel(true);

                myBluetoothLeAdvertiser.StartAdvertising(builder.Build(), dataBuilder.Build(), new BleAdvertiseCallback());
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                throw;
            }

but when I added this line of code dataBuilder.AddServiceUuid(new ParcelUuid(UUID.FromString("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"))); app dont want to launch, and I dont have any useful information in console why it is hapening

I tried to modify this code and understand that issue is caused by ParcelUuid but I dont know why If I remove this line of code, app launch normally


Solution

  • Ok I found a solution, it was linker issue, after adding this to LinkDescription, it started working. About linker description: https://learn.microsoft.com/uk-ua/xamarin/cross-platform/deploy-test/linker

    <?xml version="1.0" encoding="utf-8"?>
    <linker>
        <assembly fullname="Mono.Android">
            <type fullname="Java.Interop.TypeManager" preserve="all" />
        </assembly>
    </linker>