Dar*_*ius 5 xamarin.ios ios xamarin
我正在使用这个BLE插件构建一个带有Xamarin的iOS应用程序:
https://github.com/aritchie/bluetoothle
我只是通过BLE播放UUID,它可以工作.这是我的代码:
var data = new Plugin.BluetoothLE.Server.AdvertisementData
{
LocalName = "MyServer",
};
data.ServiceUuids.Add(new Guid("MY_UUID_HERE"));
await this.server.Start(data);
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,一旦我将应用程序放在后台,它就会停止广播.当我再次打开应用程序时再次恢复.
一旦它在后台,我怎么能让它继续播放?我在这里阅读文档:
它说我必须使用CBCentralManager该类来获取保存和恢复功能(所以我可以随时继续广播UUID),但我很难将其转换为Xamarin/C#.
编辑
在研究了一些之后,我读到我需要在委托中创建一个实例CBCentralManager并实现WillRestoreState它.我是这样做的AppDelegate:
[Register("AppDelegate")]
public class AppDelegate : MvxApplicationDelegate, ICBCentralManagerDelegate
{
private IGattServer server = CrossBleAdapter.Current.CreateGattServer();
private CBCentralManager cbCentralManager;
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// irrelevant code...
this.Ble();
return true;
}
private async Task Ble()
{
try
{
await Task.Delay(5000); // wait for it to finish initializing so I can access BLE (it crashes otherwise)
var options = new CBCentralInitOptions();
options.RestoreIdentifier = "myRestoreIndentifier";
this.cbCentralManager = new CBCentralManager(this,null,options);
var data = new Plugin.BluetoothLE.Server.AdvertisementData
{
LocalName = "MyServer",
};
data.ServiceUuids.Add(new Guid("MY_UUID_HERE"));
await this.server.Start(data);
}
catch (Exception e)
{
}
}
public void UpdatedState(CBCentralManager central)
{
//throw new NotImplementedException();
}
[Export("centralManager:willRestoreState:")]
public void WillRestoreState(CBCentralManager central, NSDictionary dict)
{
//never gets called
}
Run Code Online (Sandbox Code Playgroud)
但它对我没有任何影响.并且该WillRestoreState方法永远不会被调用...我不介意使用不同的插件/库,如果我必须在这一点上...
编辑2
我刚才意识到应用程序仍处于后台播放,我只是不再看到服务UUID(在我正在测试的信标的门户网站中),我只看到手机的标识符.
经过大量研究后,我发现这只是 iOS 的限制 - 当您的应用程序处于后台时,您无法广播 BLE 服务的 UUID。iOS 中的后台工作受到很大限制。
编辑以包含 Paulw11 评论(这是正确的):您可以宣传一项服务,但它的宣传方式只有专门扫描该服务 UUID 的另一台 iOS 设备才能看到。
| 归档时间: |
|
| 查看次数: |
906 次 |
| 最近记录: |