0

当应用程序打开(前台)时,代码运行良好,但它不在后台运行。我启用了所有后台模式。我还使用实现的 .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)}

}

先感谢您!!!

4

1 回答 1

0

处于后台模式时,您的外围广告行为有所不同:-

  1. CBAdvertisementDataLocalNameKey广告键被忽略,并且外围设备的本地名称不被广告。
  2. 包含在CBAdvertismentDataServiceUUIDS广告键值中的所有服务 UUIDS都放置在特殊的溢出区域中。只有 iOS 设备显式扫描它们才能发现它们。
  3. 如果所有应用都在做广告,那么广告包的频率就会降低。

我认为您遇到了第二个问题。有关更多详细信息,请参阅Apple 使用核心蓝牙进行后台处理的编程指南

于 2018-07-23T12:32:30.793 回答