安卓4.4设备可以作为iBeacon吗?

3
在另一个问题的答案中,我看到“您还可以在经过root的Android 4.4.3设备上作为信标进行传输,但需要安装具有系统特权的应用程序。”
如何实现这一点?
1个回答

5
是的,在4.4.3上是可能的,但关键的API方法startAdvertising()stopAdvertising()getAdvScanData()(允许您读写在广告中发送的原始信息)被阻止使用,除非应用程序具有android.permission.BLUETOOTH_PRIVILEGED。这是系统级权限,因此唯一获得该权限的方式是将您的自定义应用程序root您的手机,并将其安装在/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 Beacon Library的标准API时不能进行距离估算。
您可以在此处查看如何完成此操作的描述。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接