Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在应用程序处于后台时使用Bloothle_Ios_Xamarin_Xamarin.ios - Fatal编程技术网

Ios 在应用程序处于后台时使用Bloothle

Ios 在应用程序处于后台时使用Bloothle,ios,xamarin,xamarin.ios,Ios,Xamarin,Xamarin.ios,我正在使用Xamarin构建一个iOS应用程序,使用以下可扩展插件: 我只是通过BLE广播UUID,它可以工作。这是我的密码: var data = new Plugin.BluetoothLE.Server.AdvertisementData { LocalName = "MyServer", }; data.ServiceUuids.Add(new Guid("MY_UUID_HERE")); await

我正在使用Xamarin构建一个iOS应用程序,使用以下可扩展插件:

我只是通过BLE广播UUID,它可以工作。这是我的密码:

var data = new Plugin.BluetoothLE.Server.AdvertisementData
            {
                LocalName = "MyServer",
            };

data.ServiceUuids.Add(new Guid("MY_UUID_HERE"));

await this.server.Start(data);
唯一的问题是,一旦我把应用程序放到后台,它就会停止广播。并在我再次打开应用程序时再次恢复

我怎样才能让它在后台继续播放?我在这里阅读了文档:

它说我必须使用
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
    }
但这对我来说没什么区别。而且,
WillRestoreState
方法永远不会被调用。。。我不介意使用不同的插件/库,如果我必须在这一点上

编辑2


我刚刚意识到应用程序仍在后台播放,我再也看不到服务UUID(在我测试的信标的门户网站中),我只看到手机的标识符。

经过大量研究,我发现这只是一个iOS限制——当你的应用程序在后台时,你不能广播一个BLE服务的UUID。在iOS中,后台工作非常严格

编辑以包含Paulw11注释(正确):
您可以发布服务,但它的发布方式只有另一台专门扫描该服务UUID的iOS设备才能看到。

尽管您不能在您的iOS应用程序处于后台时广播可扩展服务的UUID,但对于任何试图执行类似操作的人,您应该查看iBeacon。这是苹果的协议,允许iOS应用程序在后台执行蓝牙功能。

据我所知,你不能只是继续广播。您可以指定蓝牙中心或蓝牙外围设备处于两种可能的模式。你应该在plist中指定,在我链接的文档中有一个叫做“国家保存和恢复”的部分说你可以。它引用:
由于状态保存和恢复内置于核心蓝牙中,因此您的应用程序可以选择使用此功能,要求系统保留应用程序中央和外围管理器的状态,并代表它们继续执行某些与蓝牙相关的任务,即使您的应用程序不再运行。
“继续代表他们执行某些与蓝牙相关的任务“什么任务?不仅仅是你想要的任何任务,而是取决于你所处模式的某些任务,对吗?应用程序可能会声明两种核心蓝牙后台执行模式,一种用于实现中心角色的应用程序,另一种用于实现外围角色的应用程序。如果您的应用程序同时实现这两个角色,它可能会声明它支持这两种后台执行模式。通过将UIBackgroundModes键添加到Info.plist文件并将该键的值设置为包含以下字符串之一的数组来声明核心Bluetooth后台执行模式:Bluetooth central Bluetooth Peripherald公布服务的UUID是使用BLE可以做的最简单的事情。。。它还引用了一个例子:
一些应用可能需要使用核心蓝牙框架在后台执行长期操作。例如,假设您正在为iOS设备开发一个家庭安全应用程序,该设备与门锁(配备蓝牙低能耗技术)通信。应用程序和锁相互作用,当用户离开家时自动锁门,当用户返回时自动开锁,而应用程序处于后台。
好的,你可以发布服务,但发布服务的方式只有另一个专门扫描该服务uuid的iOS设备才能看到。