2017-06-19 87 views
1

我正在用Xamarin和MvvmCross構建一個iOS應用程序,並要求使用藍牙LE。我想使用這個插件:ACR無效BluetoothLE插件啓動服務器時無效狀態

https://github.com/aritchie/bluetoothle

這裏是我的代碼:

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

它拋出試圖啓動服務器時異常:

{System.ArgumentException :無效狀態 - 未知於 Plugin.BluetoothLE.Server.GattServer.Start (Plugin.BluetoothLE.Server.AdvertisementData adData)[0x0005f] in < 4281c4bd57f24525b20baae1afdf610b>:0

顯然,這個插件是好用,所以我必須失去了一些東西明顯?

回答

0

我找到了解決方案。相反,配股代碼在我的核心項目,我把它放在了iOS項目本身內部,在ViewDidAppear功能,並放在一個全局變量在我UIViewController類:

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

我認爲你需要訪問適配器與適當的線程,或在適當的時間(在其他初始化之後),因此它之前爲什麼崩潰。我找不到合適的地方把它放在我的Core項目中,所以我不確定是否必須將這些代碼放入每個平臺。無論如何,這是針對此問題的其他任何人的解決方案。

1

該異常表明硬件是無效或初始化狀態,並且可以在這裏看到: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); 

我相信這是在代碼中的錯誤,因爲它不等待委託以通知國家已經改變。

+0

我想我是在錯誤的時間或線程訪問它,阻止重要的初始化,需要先完成。 – Darius