【问题标题】:Can an Android 4.4 device act as an iBeacon? [closed]Android 4.4 设备可以充当 iBeacon 吗? [关闭]
【发布时间】:2015-12-22 21:11:03
【问题描述】:

answer to another question 中,我看到“您也可以在有根的 Android 4.4.3 设备上作为信标传输,但它需要安装具有系统权限的应用程序。”

如何做到这一点?

【问题讨论】:

    标签: android bluetooth-lowenergy ibeacon android-bluetooth android-ibeacon


    【解决方案1】:

    是的,这在 4.4.3 上是可能的,但关键的 API 方法 startAdvertising()stopAdvertising()getAdvScanData()(允许您读取和写入广告中发送的原始信息)被禁止使用除非应用有android.permission.BLUETOOTH_PRIVILEGED。这是一个系统级别的权限,因此获取此权限的唯一方法是为您的自定义应用程序设置您的手机,然后将您的应用程序安装在 /system/priv-app 目录中。

    如果你能做到这一点,执行此操作的基本代码是:

    byte[] advertisingBytes;
    advertisingBytes = new byte[] { 
      (byte) 0x18, (byte) 0x01,   // Radius Networks manufacturer ID
      (byte) 0xbe, (byte) 0xac,   // AltBeacon advertisement identifier
      // 16-byte Proximity UUID follows  
      (byte) 0x2F, (byte) 0x23, (byte) 0x44, (byte) 0x54, (byte) 0xCF, (byte) 0x6D, (byte) 0x4a, (byte) 0x0F,
      (byte) 0xAD, (byte) 0xF2, (byte) 0xF4, (byte) 0x91, (byte) 0x1B, (byte) 0xA9, (byte) 0xFF, (byte) 0xA6,
      (byte) 0x00, (byte) 0x01,   // major: 1
      (byte) 0x00, (byte) 0x02 }; // minor: 2
    BluetoothManagerbluetoothManager = (BluetoothManager) this.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
    BluetoothAdvScanData scanData = bluetoothAdapter.getAdvScanData();
    scanData.removeManufacturerCodeAndData(0x01);
    scanData.setManufacturerData((int) 0x01, advertisingBytes);
    scanData.setServiceData(new byte[]{});  // clear out service data.  
    bluetoothAdapter.startAdvertising(advertiseCallback);   
    

    上面的代码向您展示了如何传输一个开源的 AltBeacon。但是您可以通过更改字节模式来传输其他信标类型。

    Android 4.4 中的另一个重要限制是,一个错误会阻止您发布超过 24 个字节的数据,而不是应该允许的 26 个字节。这意味着如果信标广告需要超过 24 个字节,则它们可能会被截断。例如,AltBeacon 使用最后两个字节中的第二个字节来存储校准的发射机功率。由于无法发送,这意味着无法使用 Android 信标库的标准 API 进行距离估计。

    您可以看到有关如何完成此操作的说明here

    【讨论】:

      猜你喜欢
      • 2013-11-05
      • 1970-01-01
      • 2020-06-09
      • 2014-01-16
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多