当应用程序打开(前景)时,代码运行良好,但是,它不在后台运行。我启用了所有背景模式。我还使用实现的.allowsBackgroundLocationUpdates添加了背景位置更新。不知道从这里去哪里。代码如下:
import UIKit
import CoreLocation
import CoreBluetooth
class iDevice: UIViewController, CBPeripheralManagerDelegate, CLLocationManagerDelegate{
@IBOutlet weak var device: UILabel!
@IBOutlet weak var deviceImg: UIImageView!
@IBOutlet weak var distance: UILabel!
var beaconRegion : CLBeaconRegion!
var beaconPeripheralData : NSDictionary!
var peripheralManager : CBPeripheralManager!
var locationManager : CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
initBeaconRegion()
initTransmit()
locationManager = CLLocationManager.init()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager!.allowsBackgroundLocationUpdates = true
locationManager!.pausesLocationUpdatesAutomatically = false
startScanningForBeaconRegion(beaconRegion: getBeaconRegion())
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad) {
self.device.text = "iPhone"
self.deviceImg.image = UIImage(named: "phone")!
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone) {
self.device.text = "iPad"
self.deviceImg.image = UIImage(named: "ipad")!
}
}
func initTransmit(){
beaconPeripheralData = beaconRegion .peripheralData(withMeasuredPower: nil)
peripheralManager = CBPeripheralManager.init(delegate: self, queue: nil)
}
func initBeaconRegion() {
beaconRegion = CLBeaconRegion.init(proximityUUID: UUID.init(uuidString: "E06F95E4-FCFC-42C6-B4F8-F6BAE87EA1A0")!,
major: 1233,
minor: 45,
identifier: "com.peard.idevices")
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if (peripheral.state == .poweredOn) {
peripheralManager .startAdvertising(beaconPeripheralData as? [String : Any])
print("Powered On")
} else {
peripheralManager .stopAdvertising()
print("Not Powered On, or some other error")
}
}
func getBeaconRegion() -> CLBeaconRegion {
let beaconRegion = CLBeaconRegion.init(proximityUUID: UUID.init(uuidString: "E06F95E4-FCFC-42C6-B4F8-F6BAE87EA1A0")!, identifier: "com.peard.idevices")
return beaconRegion
}
func startScanningForBeaconRegion(beaconRegion: CLBeaconRegion) {
print(beaconRegion)
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startMonitoring(for: beaconRegion)
locationManager.startRangingBeacons(in: beaconRegion)
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
let beacon = beacons.last
if beacons.count > 0 {
if beacon?.proximity == CLProximity.unknown {
distance.text = "Unknown"
} else if beacon?.proximity == CLProximity.immediate {
distance.text = "within 3ft"
} else if beacon?.proximity == CLProximity.near {
distance.text = "within 5ft"
} else if beacon?.proximity == CLProximity.far {
distance.text = "within 20 ft"
}
} else {
print("no")
}
print("Ranging")
}
override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent}
@IBAction func dismiss(_ sender: Any) { self.dismiss(animated: true, completion: nil)}
}
先谢谢您!
答案 0 :(得分:0)
在后台模式下,您的外围广告行为有所不同:-
我认为您遇到第二个问题。有关详细信息,请参见Apple programming guide for background processing with core bluetooth。