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



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

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

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

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

那么有没有办法让手机在进入某个区域时在后台作为信标传输?

我已经查看了Apple的后台蓝牙,其中提到在后台传输时广告数据包是不同的。我也看过这个解决方案,但这个解决方案不使用核心位置,所以在进入一个区域时无法唤醒应用程序

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 配件的行为

抱歉, iOS 不会让您的应用在后台宣传与 iBeacon 规范匹配的 BLE 数据包. 正如您在问题中提到的,Apple 会改变后台发出的后台广告的外观,因此,它们不会触发 CoreLocation 输入事件以唤醒后台接收应用程序。

有一些不完美的选择:

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

  2. 您可以在应用处于前台时播发 iBeacon,并尝试让用户通过本地通知将应用带到前台。

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

最新更新