进入信标区域时如何在后台做广告作为信标 [英] how to advertise in the background as a beacon when entering a beacon region

查看:102
本文介绍了进入信标区域时如何在后台做广告作为信标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个严重依赖iBceaons的应用程序.

I'm currently working on developing an app that is heavily dependent on iBceaons.

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

I managed to create a beacon region which wakes up the app process in the background for 10 seconds when entering the region.

下一步是让手机在后台进入该区域时自行做广告

The next step is making phone it self advertise when it enters the region while in background

当前,当应用打开时,它会保持测距并在其区域内作为信标进行传输
就像应用程序在后台运行一样,它的运行时间为10秒,但进入一个区域时不会作为信标传输

Currently while the app is open it keeps ranging and transmits as a beacon while its in a region
where as when the app is in the background , it ranges for 10 seconds but doesn't transmit as a beacon when it enters a region

那么有没有一种方法可以让手机在进入某个区域时在后台作为信标进行传输?

so is there a way to allow the phone to transmit as a beacon when its in the background when it enters a region ?

我已经查看了​​此解决方案,但该解决方案没有不要使用核心位置,以免进入区域时无法唤醒应用程序

I already had a look into Apple's background Bluetooth which mentioned that the advertising packet is differs when transmitting in the background. I have also looked at This Solution but this solution doesn't use core location so it can't wake the app when entering a region

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附件,并在播放列表中添加了隐私-蓝牙外围设备使用说明"

I have the act as a BLE Accessory selected in the background modes in capabilities and the Privacy - Bluetooth Peripheral Usage Description added in the playlist

推荐答案

对不起,iOS不允许您的应用在后台发布与iBeacon规范匹配的BLE数据包.正如您在问题中提到的那样,Apple更改了在背景中发出的背景广告的方式,因此,它们不会触发CoreLocation输入事件来唤醒在后台接收应用程序.

Sorry, iOS will not let your app advertise a BLE packet matching the iBeacon spec when it is in the background. As you have mentioned in your question, Apple alters the way background advertisements emitted in the background look, and as a result, they will not trigger CoreLocation enter events to wake up receiving apps in the background.

有一些不完善的选择:

  1. 您可以在后台使用其他信标广告并唤醒您的应用程序.这不会像iBeacon那样快,但是它将在几分钟之内唤醒您的应用程序.这是执行此操作的设置: https://github.com/Decemberlabs/AltBeacon

您可以在应用程序处于前台时对iBeacon进行广告宣传,并尝试通过本地通知吸引用户将该应用程序置于前台.

You can advertise iBeacon whenever the app is in the foreground, and try to get the user to bring the app to the foreground through a local notification.

如果附近有其他应用程序的前台副本(基于后台信标检测),则可以在您的应用程序在后台运行的10秒钟内使用Web服务将其存在通知给他们.此通知可以告诉他们开始代表您做广告.

If there are other foreground copies of your app in the vicinity (based on background beacon detection), you can use a web service to notify them of your presence during the 10 seconds your app runs in the background. This notification can tell them to start advertising on your behalf.

这篇关于进入信标区域时如何在后台做广告作为信标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆