Ios 如何在xcode(swift)和phonegap中在后台运行iPhone作为信标

Ios 如何在xcode(swift)和phonegap中在后台运行iPhone作为信标,ios,swift,cordova,phonegap-plugins,ibeacon,Ios,Swift,Cordova,Phonegap Plugins,Ibeacon,我成功地创建了一个应用程序,将手机变成一个信标发射器 它在xcode(Swift)和phonegap上运行良好 对我来说,下一步就是让它作为一个信号灯在后台工作(我正在为iOS 10开发) 我在网上发现了与此类似的问题,但所有问题都过时了,即旧的ios、objective-C和过时的插件 如果有人能为我提供一个工作示例或指导我如何更改xcode s(wift)和phonegap的代码,我将不胜感激 Xcode(Swift 3) 对于这两种代码,在目标的后台功能中检查位置更新和后台提取。此外,在两

我成功地创建了一个应用程序,将手机变成一个信标发射器

它在xcode(Swift)和phonegap上运行良好

对我来说,下一步就是让它作为一个信号灯在后台工作(我正在为iOS 10开发)

我在网上发现了与此类似的问题,但所有问题都过时了,即旧的ios、objective-C和过时的插件

如果有人能为我提供一个工作示例或指导我如何更改xcode s(wift)和phonegap的代码,我将不胜感激

Xcode(Swift 3)


对于这两种代码,在目标的后台功能中检查位置更新和后台提取。此外,在两个播放列表中都添加了隐私-位置始终使用说明

不幸的是,苹果阻止了在iOS上作为后台信标进行传输的能力。所有CoreBluetooth标准广告实际上都被阻止,因为广告时段是一个有限的资源,后台的多个应用程序会很快耗尽这个资源

苹果确实有一个专有的备份系统,用于在后台宣传蓝牙服务,该系统使用一个特殊的溢出区域来包含服务标识符。但是,尽管这个方案在后台为蓝牙服务做广告有效,但它不适用于广告信标,因为它破坏了格式


一句话:iOS上的背景灯塔广告根本不起作用。很抱歉。

不幸的是,苹果在iOS上阻止了在后台作为信标传输的功能。所有CoreBooth标准广告实际上都被阻止了,因为广告时段是有限的资源,后台的多个应用程序会很快耗尽这些资源

苹果确实有一个专有的备份系统,用于在后台宣传蓝牙服务,该系统使用一个特殊的溢出区域来包含服务标识符。但是,尽管这个方案在后台为蓝牙服务做广告有效,但它不适用于广告信标,因为它破坏了格式


一句话:iOS上的背景灯塔广告根本不起作用。对不起。

在后台扫描信标怎么样?是的,扫描在后台工作。请看我的回答:感谢您的回复,我从下载了您的示例,并对其进行了一些修改,以便在应用程序位于后台时继续获取信标的RSSI值,并且在其位于后台时仅获取约5个RSSI读数,然后在一段时间后提供另外5个RSSI值。有没有办法让它继续测距@DAVIDGYOUNG当监控在后台工作时,距离限制在最后一次区域进入/退出事件后的5-10秒左右。这就是你所看到的。您可以使用下面的技巧将此扩展最多3分钟:谢谢,我现在明白了:)。不过,监控会持续多久,因为它不是即时的@davidgyoung在后台扫描信标怎么样?是的,扫描在后台工作。请看我的回答:感谢您的回复,我从下载了您的示例,并对其进行了一些修改,以便在应用程序位于后台时继续获取信标的RSSI值,并且在其位于后台时仅获取约5个RSSI读数,然后在一段时间后提供另外5个RSSI值。有没有办法让它继续测距@DAVIDGYOUNG当监控在后台工作时,距离限制在最后一次区域进入/退出事件后的5-10秒左右。这就是你所看到的。您可以使用下面的技巧将此扩展最多3分钟:谢谢,我现在明白了:)。不过,监控会持续多久,因为它不是即时的@davidgyoung
import UIKit
import CoreLocation
import CoreBluetooth

class ViewController: UIViewController,CLLocationManagerDelegate,CBPeripheralManagerDelegate,UITextFieldDelegate{

    var locationManager = CLLocationManager()
    var startLocation: CLLocation!
    var localBeacon: CLBeaconRegion!
    var beaconPeripheralData: NSDictionary!
    var peripheralManager: CBPeripheralManager!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationManager.delegate = self

        locationManager.requestAlwaysAuthorization()

        bleSwitch.addTarget(self, action: #selector(ViewController.bleSwitchUpdated(_:)), for: UIControlEvents.valueChanged)

        major.delegate=self
        minor.delegate=self
        txPower.delegate=self

    }

    @IBOutlet weak var minor: UITextField!
    @IBOutlet weak var major: UITextField!
    @IBOutlet weak var txPower: UITextField!
    @IBOutlet weak var bleSwitch: UISwitch!
    @IBOutlet weak var bleView: UIView!
    @IBAction func bleSwitchUpdated(_ sender: UISwitch) {
        if bleSwitch.isOn {
            initLocalBeacon()
            bleView.isHidden = false;
        //bleSwitch.setOn(true, animated: false)
        }
        else {
        stopLocalBeacon()
            bleView.isHidden = true;
        }
    }

    func initLocalBeacon() {
        if localBeacon != nil {
            stopLocalBeacon()
        }

        let localBeaconUUID = "5A4BCFCE-174E-4BAC-A814-092E77F6B7E5"
        let localBeaconMajor: CLBeaconMajorValue = UInt16(major.text!)!
        let localBeaconMinor: CLBeaconMinorValue = UInt16(minor.text!)!

        let uuid = UUID(uuidString: localBeaconUUID)!
        localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: "Your private identifer here")

        beaconPeripheralData = localBeacon.peripheralData(withMeasuredPower: -59)
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil)
    }

    func stopLocalBeacon() {
        peripheralManager.stopAdvertising()
        peripheralManager = nil
        beaconPeripheralData = nil
        localBeacon = nil
    }

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        if peripheral.state == .poweredOn {
            peripheralManager.startAdvertising(beaconPeripheralData as! [String: AnyObject]!)
        } else if peripheral.state == .poweredOff {
            peripheralManager.stopAdvertising()
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
        if bleSwitch.isOn == true{
        stopLocalBeacon()
            initLocalBeacon()
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    var app = (function()
{

           // Application object.
    var app = {};

    // Background flag.
    var mAppInBackground = false;

        app.initialize = function()
    {
        document.addEventListener('deviceready', onDeviceReady, false);
        document.addEventListener('pause', onAppToBackground, false);
        document.addEventListener('resume', onAppToForeground, false);
    };

    function onDeviceReady()
    {
        startMonitoringAndRanging();
        startNearestBeaconDisplayTimer();
        displayRegionEvents();

    }

    function onAppToBackground()
    {
        mAppInBackground = true;
        stopNearestBeaconDisplayTimer();
    }

    function onAppToForeground()
    {
        mAppInBackground = false;
        startNearestBeaconDisplayTimer();
        displayRegionEvents();
    }


    function startMonitoringAndRanging()
    {

        // Request permission from user to access location info.
        cordova.plugins.locationManager.requestAlwaysAuthorization();

           BleconOn();

    }


           function BleconOn(){
           var uuid = '00000000-0000-0000-0000-000000000000';
           var identifier = 'advertisedBeacon';
           var minor = 2000;
           var major = 5;
           var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid, major, minor);

           var delegate = new cordova.plugins.locationManager.Delegate();


           delegate.peripheralManagerDidStartAdvertising = function(pluginResult) {
           console.log('peripheralManagerDidStartAdvertising: '+ JSON.stringify(pluginResult.region));
           };
           // Event when bluetooth transmission state changes
           // If 'state' is not set to BluetoothManagerStatePoweredOn when advertising cannot start
           delegate.peripheralManagerDidUpdateState = function(pluginResult) {
           console.log('peripheralManagerDidUpdateState: '+ pluginResult.state);
           };

           cordova.plugins.locationManager.setDelegate(delegate);

           // Verify the platform supports transmitting as a beacon
           cordova.plugins.locationManager.isAdvertisingAvailable()
           .then(function(isSupported){

                 if (isSupported) {
                 cordova.plugins.locationManager.startAdvertising(beaconRegion)
                 .fail(conole.error)
                 .done();
                 } else {
                 console.log("Advertising not supported");
                 }
                 })
           .fail(function(e) { console.error(e); })
           .done();
           }

           function BleconOff(){

           cordova.plugins.locationManager.stopAdvertising()
           .fail(console.error)
           .done();
           }



    return app;

})();

app.initialize();