【问题标题】:Background Bluetooth(BLE) Transmit and Scan后台蓝牙(BLE)传输和扫描
【发布时间】:2018-12-30 19:47:25
【问题描述】:

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

}

提前谢谢你!!!

【问题讨论】:

    标签: swift core-location core-bluetooth cbperipheralmanager


    【解决方案1】:

    在后台模式下你的周边广告行为是不同的:-

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

    我认为您遇到了第二个问题。有关详细信息,请参阅Apple programming guide for background processing with core bluetooth

    【讨论】:

    • 由于#2,iOS 设备无法在后台宣传为 iBeacon
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多