我正试图让应用程序工作,手机发出iBeacon信号,附近的手机能够在后台检测到并执行回调方法。电话A正在广播信号,而电话B正在后台监听它并调用方法。以下是设置全部的代码:
var centralManager: CBCentralManager!
var peripheralManager: CBPeripheralManager = CBPeripheralManager()
var locationManager: CLLocationManager = CLLocationManager()
let uuid: NSUUID = NSUUID(UUIDString: "DCEF54A2-31EB-467F-AF8E-350FB641C97B")!
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.requestAlwaysAuthorization()
self.centralManager = CBCentralManager(delegate: self, queue: nil)
self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "device")
locationManager.startMonitoringForRegion(beaconRegion)
这是我用于广告和监控信标的代码:
func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Beacon has been found")
print(region)
}
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) {
print("PM state updated")
let advertisingData = [CBAdvertisementDataLocalNameKey:"device", CBAdvertisementDataServiceUUIDsKey: uuid]
peripheralManager.startAdvertising(advertisingData)
}
由于我希望所有具有相同UUID的设备,我没有指定主要和次要,但是我想在监听电话A时调用它所调用的方法的主要和次要电话B.
我的问题是手机A能够成功开始广告,但手机B似乎无法识别任何设备。
答案 0 :(得分:0)
设置蓝牙广告的代码不是设置iBeacon传输:
let advertisingData = [CBAdvertisementDataLocalNameKey:"device", CBAdvertisementDataServiceUUIDsKey: uuid]
peripheralManager.startAdvertising(advertisingData)
该代码实际上设置为通告服务ID为DCEF54A2-31EB-467F-AF8E-350FB641C97B的蓝牙LE服务。这与作为制造商广告的iBeacon广告不同,其中ProximityUUID(不同于服务UUID)以及主要和次要标识符。您使用ProximityUUID DCEF54A2-31EB-467F-AF8E-350FB641C97B主要1小调1设置iBeacon传输,如下所示:
let advertisingData = beacon.peripheralDataWithMeasuredPower(nil) as Dictionary
peripheralManager.startAdvertising(advertisingData as? [String : AnyObject])