ACR Reactive BluetoothLE Plugin Invalid server startup state

I am creating an iOS application with Xamarin and MvvmCross, which is required to use Bluetooth LE. I am trying to use this plugin:

https://github.com/aritchie/bluetoothle

Here is my code:

var server = CrossBleAdapter.Current.CreateGattServer(); await server.Start(new AdvertisementData()); // throws exception 

It throws an exception when trying to start the server:

{System.ArgumentException: invalid state - unknown in Plugin.BluetoothLE.Server.GattServer.Start (Plugin.BluetoothLE.Server.AdvertisementData adData) [0x0005f] in <4281c4bd57f24525b20baae1afdf610b>: 0

Apparently this plugin is easy to use, so should I miss something obvious?

+1
source share
2 answers

I have found a solution. Instead of placing the code in my main project, I placed it inside the iOS project itself in the ViewDidAppear function and put the global variable in my UIViewController class:

private IGattServer server = CrossBleAdapter.Current.CreateGattServer();

I think you need to access the adapter with the appropriate thread or at the appropriate time (after another initialization), therefore, why did it crash before. I could not find a suitable place to put this in my main project, so I'm not sure if I should put this code on every platform. Anyway, here is a solution for someone else with this problem.

0
source

This exception indicates that the hardware is invalid or initializing the state and can be seen here: https://github.com/aritchie/bluetoothle/blob/master/Plugin.BluetoothLE.Apple.Shared/Server/GattServer.cs#L74

 if (this.manager.State != CBPeripheralManagerState.PoweredOn) throw new ArgumentException("Invalid State - " + this.manager.State); 

I believe that this is a mistake in the code, because it does not wait until the delegate reports that the state has changed.

+1
source

Source: https://habr.com/ru/post/1269047/


All Articles