【问题标题】:Advertise in CoreBluetooth format using the AltBeacon library使用 AltBeacon 库以 CoreBluetooth 格式做广告
【发布时间】:2020-06-17 10:12:26
【问题描述】:

我正在尝试使用 BLE 在 Android 和 ios 之间实现操作系统间广告和扫描功能。 我需要了解从 Android 设备在 CoreBluetooth 中投放广告需要遵循的流程。 我一直在使用 AltBeacon 库以 iBeacon 格式进行广告,效果很好,但是 ios 无法扫描 iBeacon 上有限数量的信标的限制迫使我转向 CoreBluetooth 框架。

这是我用来以 iBeacon 格式做广告的示例代码:

BluetoothManager bluetoothManager =
            (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager != null) {
        BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();
        BluetoothLeAdvertiser mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
        if (mBluetoothLeAdvertiser != null) {
            beacon = new Beacon.Builder()
                    .setId1(userId)
                    .setId2("1")
                    .setId3("1")
                    .setManufacturer(0x004C)
                    .setTxPower(-75)
                    .setDataFields(Arrays.asList(new Long[]{0l}))
                    .build();
            beaconParser = new BeaconParser()
                    .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
            beaconTransmitter = new BeaconTransmitter(InventaSdk.getContext(), beaconParser);
            beaconTransmitter.setBeacon(beacon);
        }
    }

【问题讨论】:

    标签: java android bluetooth-lowenergy core-bluetooth altbeacon


    【解决方案1】:

    CoreBluetooth 不是一种格式。它是 iOS 上的一组 API。使用 CoreBluetooth,您可以检测各种有限制的信标格式:

    • AltBeacon 格式只能在 iOS 应用处于前台时检测到
    • 可以在前景和背景中检测到 Eddystone 格式,但在背景中检测速度比 iBeacon 慢。
    • CoreBluetooth 无法检测到 iBeacon 格式。为此,您必须使用 CoreLocation。

    要在 AltBeacon 中做广告(可被核心蓝牙检测到),请使用以下代码:

    Beacon beacon = new Beacon.Builder()
            .setId1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")
            .setId2("1")
            .setId3("2")
            .setManufacturer(0x0118)
            .setTxPower(-59)
            .setDataFields(Arrays.asList(new Long[] {0l}))
            .build();
    BeaconParser beaconParser = new BeaconParser()
            .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
    BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
    beaconTransmitter.startAdvertising(beacon);
    

    使用 CoreBlutooth 解析信标很棘手。您可能想使用iOS Beacon Tools 之类的工具来执行此操作。

    另外值得补充的是,iOS 不能宣传 AltBeacon 或 Eddystone 格式。它只能通告 iBeacon 和 GATT 服务 UUID。

    【讨论】:

    • 感谢大卫的快速回复。这确实帮助我树立了理解局限性的观点。再快一点,ios 在后台做广告有哪些不同的方式?
    • iOS 上的后台广告仅限于溢出区域广告。你可以阅读更多关于这些here
    • 基于溢出区域广告博客中建议的功能,您是否有一个示例应用程序可以在以 iBeacon 格式扫描时扫描位掩码中的每个可能位?
    • 上述存储库中使用的格式是基于 GATT 服务 UUID 的,对吗?是否有任何特定于 iBeacon 的内容,我们可以在后台扫描并以 iBeacon 格式进行广告宣传?
    猜你喜欢
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2017-01-30
    • 2023-03-19
    • 1970-01-01
    • 2010-11-04
    相关资源
    最近更新 更多