首页 > 解决方案 > iOS CLLocationManager 未检测到从 Android 发送的 Android 信标库 iBeacon 信号

问题描述

应用目的

  1. 从 iOS 传输 Beacon 信号并在 Android 和 iOS 设备上检测该信号。
  2. 从 Android 传输 Beacon 信号并在 Android 和 iOS 设备上检测该信号。

技术

Android:Android 信标库。

iOS:CoreLocation CLLocationManager iBeacon。

问题

    locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion)

使用相同的 UUID 通过 Android 信标库从 Android 传输 iBeacon 信号,iOS 上的上述 CLLocationManager 委托方法未检测到信标。

编码

    func startRangingBeacons(satisfying constraint: CLBeaconIdentityConstraint)

在 iOS 上检测

    var locationManager: CLLocationManager!
    var localBeaconUUID = "578f4eba-a16e-11ea-bb37-0242ac130002"
    let beaconIdentifier = "MyBeacon"

    override func viewDidLoad() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
   }

    func startScanning() {
        let uuid = UUID(uuidString: localBeaconUUID)!
        let beaconRegion = CLBeaconRegion(uuid: uuid, identifier: beaconIdentifier)
        locationManager.startMonitoring(for: beaconRegion)
        locationManager.startRangingBeacons(in: beaconRegion)
    }

    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        print("Ranged beacons: \(beacons.count)")
    }

在 Android 上传输 iBeacon

    private val uuid = "578f4eba-a16e-11ea-bb37-0242ac130002"

    fun transmitBeacon() {
        val beacon = Beacon.Builder()
                .setId1(uuid)
                .setId2("1")
                .setId3("1")
                .setManufacturer(0x004c)
                .setTxPower(-59)
                .setDataFields(listOf(0L))
                .build()
        val beaconParser = BeaconParser()
                .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24")
        val beaconTransmitter = BeaconTransmitter(applicationContext, beaconParser)
        beaconTransmitter.startAdvertising(beacon)
    }

标签: androidiosibeaconaltbeaconandroid-ibeacon

解决方案


此处显示的布局:

val beaconParser = BeaconParser()
                .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24")

用于 AltBeacon,iOS 设备无法通过 CoreBluetooth 检测到。您只能使用该 iOS API 来检测具有以下布局的 iBeacon:

val beaconParser = BeaconParser()
                .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")

在另一个论坛的较早答案中,我注意到 OP 正在使用添加了数据字段的布局并建议他将其删除,但没有意识到问题中的布局使用beac仅有效的匹配字节的更根本问题用于 AltBeacon 格式。对于 iBeacon,您必须匹配0215


推荐阅读