如何在进入信标区域时作为信标在后台做广告

时间:2017-08-23 13:05:18

标签: ios swift bluetooth ibeacon ios-bluetooth

我正在开发一款严重依赖iBceaons的应用程序。

我设法创建了一个信标区域,在进入该区域时会在后台唤醒应用程序进程10秒钟。

下一步是让手机在后台进入该区域时自我宣传

目前,当应用程序处于打开状态时,它会保持测距并在区域中作为信标传输 当应用程序在后台时,它的范围为10秒,但在进入某个区域时不会作为信标传输

有没有办法允许手机在进入某个地区时在后台作为信标进行传输?

我已经看过Apple's background Bluetooth,其中提到广告包在后台传输时有所不同。我也查看了This Solution,但此解决方案不使用核心位置,因此在进入区域时无法唤醒应用程序

import UIKit
import CoreLocation
import CoreBluetooth
class ViewController: 
UIViewController,CLLocationManagerDelegate,CBPeripheralManagerDelegate {

var locationManager: CLLocationManager!
var localBeacon: CLBeaconRegion!
var beaconPeripheralData: NSDictionary!
var peripheralManager: CBPeripheralManager!

let myCustomServiceUUID = CBUUID(string:"5A4BCFCE-174E-4BAC-A814-092E77F6B7E5")

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()

    }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways {
        if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
            if CLLocationManager.isRangingAvailable() {
                startScanning()
            }
        }
    }
}

func startScanning() {
    let uuid = UUID(uuidString: "48de4980-968d-11e4-b4a9-0800200c9a66")!
    let beaconRegion = CLBeaconRegion(proximityUUID: uuid, major: 1, minor: 1, identifier: "region1")
       beaconRegion.notifyEntryStateOnDisplay=true;
    locationManager.startMonitoring(for: beaconRegion)
    locationManager.startRangingBeacons(in: beaconRegion)
}

func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {

    if(beacons.count > 0) {
        let nearestBeacon:CLBeacon = beacons[0] as CLBeacon

    NSLog("RSSI value is %ld", nearestBeacon.rssi);
        initLocalBeacon() // Here I'm transmitting as a beacon along with ranging when entering or within a region  which works fine the app is open but not transmitting while in background 



    }
}

func locationManager(_ manager: CLLocationManager,didDetermineState state: CLRegionState,for region: CLRegion)    {

    switch state {
    case .inside:
        NSLog("locationManager didDetermineState INSIDE  %@", region.identifier);


    case .outside:
        NSLog("locationManager didDetermineState OUTSIDE  %@", region.identifier);

    case .unknown:
        NSLog("locationManager didDetermineState OTHER  %@", region.identifier);

    }
}

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

    let localBeaconUUID = "5A4BCFCE-174E-4BAC-A814-092E77F6B7E5"
    let localBeaconMajor: CLBeaconMajorValue = 2
    let localBeaconMinor: CLBeaconMinorValue = 1

    let uuid = UUID(uuidString: localBeaconUUID)!
    localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: "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()
    }
}
}

我在功能的后台模式中选择了BLE附件,并在播放列表中添加了隐私 - 蓝牙外设使用说明

1 个答案:

答案 0 :(得分:2)

抱歉,iOS不会让您的应用在后台播放符合iBeacon规范的BLE数据包。正如您在问题中提到的,Apple会改变背景中发布的背景广告的外观,因此,它们不会触发CoreLocation输入事件以唤醒在后台接收应用程序。

有一些不完美的选择:

  1. 您可以在后台使用其他信标广告并唤醒您的应用。这不会像iBeacon一样快,但它会在几分钟内唤醒你的应用程序。这是一个设置:https://github.com/Decemberlabs/AltBeacon

  2. 只要应用程序位于前台,您就可以宣传iBeacon,并尝试让用户通过本地通知将应用程序带到前台。

  3. 如果附近有应用程序的其他前景副本(基于后台信标检测),您可以使用Web服务在应用程序在后台运行的10秒内通知他们您的状态。此通知可以告诉他们代表您开始投放广告。

相关问题