【发布时间】:2017-08-23 13:05:17
【问题描述】:
我目前正在开发一个严重依赖 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 附件的行为,并在播放列表中添加了隐私 - 蓝牙外设使用说明
【问题讨论】:
标签: ios swift bluetooth ibeacon ios-bluetooth