在后台运行应用程序时使用Bloothtooth LE

大流士

我正在使用Xamarin使用此BLE插件构建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);

唯一的问题是,一旦我将应用置于后台,它就会停止广播。并在我再次打开应用程序时再次恢复。

一旦它进入后台,我如何让它继续广播?我在这里阅读文档:

https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

它说我必须使用CBCentralManager该类来获取保留和恢复功能(这样我就可以一直广播UUID),但是我很难将其转换为Xamarin / C#。

编辑

在研究了更多内容之后,我读到我需要创建一个实例CBCentralManagerWillRestoreState在委托中实现我在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
    }

但这对我没有影响。而且该WillRestoreState方法永远不会被调用...如果需要,我现在不介意使用其他插件/库...

编辑2

我只是意识到该应用程序在后台时仍在广播,只是不再看到服务UUID(在我正在测试的信标的Web门户中),我只看到了手机的标识符。

大流士

经过大量研究后,我发现这仅仅是iOS的限制-当您的应用程序在后台运行时,您无法广播BLE服务的UUID。在iOS中,后台工作非常严格。

编辑以包含Paulw11注释(这是对的)(可以):您可以播发服务,但是播发的方式是只有另一个专门扫描该UUID服务的iOS设备才能看到。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在后台运行应用程序时未调用Firebase onMessageReceived

仅在打开并运行应用程序时如何在后台运行方法?

我可以在后台运行Cordova应用程序吗

如何使Android应用程序始终在后台运行?

可以在后台运行应用程序时运行CADisplayLink吗?

Firebase消息传递-在后台运行应用程序时创建抬头显示

检查应用程序是在前台运行还是在后台运行(使用同步适配器)

WKWebView在后台应用程序时停止音频

从最近使用的应用程序清除后,是否可以使应用程序在后台运行?

在后台运行应用程序时,Swift推送通知无法正常工作

应用程序在后台运行功能| 扑

如何检查应用程序是否在后台运行?

应用程序在后台运行时的问题

隐藏表单时在后台运行的应用程序

如何在后台运行封闭的应用程序?

科尔多瓦:在后台运行应用程序时运行事件

在后台应用程序时获取来电事件

在后台运行的WPF应用程序

使Web应用程序在后台运行

关闭应用程序或在后台运行的Windows Phone中,当打开应用程序时,使用Parse的Cordova Push Notification不起作用

在后台运行CPU密集型应用程序时进行多核编程?

iOS应用程序默认在后台模式下运行

最小化并在后台运行应用程序

在后台运行应用程序时,adb挂起

我怎么知道我的应用程序在后台运行?

.Net 控制台应用程序在后台运行

在后台状态后打开应用程序时黑屏

如何使用 adb 阻止应用程序在后台运行?

如何在后台运行我的 Web 应用程序?