Mar*_*tin 3 android bluetooth-lowenergy android-bluetooth ibeacon android-ibeacon
在另一个问题的回答中,我看到“您也可以在有根的 Android 4.4.3 设备上作为信标传输,但它需要安装有系统权限的应用程序。”
如何才能做到这一点?
是的,这在 4.4.3 上是可能的,但是关键 API 方法startAdvertising(), stopAdvertising()and 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);
Run Code Online (Sandbox Code Playgroud)
上面的代码向您展示了如何传输一个开源的 AltBeacon。但是您可以通过更改字节模式来传输其他信标类型。
Android 4.4 中的另一个重要限制是,一个错误阻止您通告超过 24 个字节的数据,而不是应该允许的 26 个字节。这意味着如果信标广告需要超过 24 个字节,它们可能会被截断。例如,AltBeacon 使用最后两个字节中的第二个字节来存储校准的发射机功率。由于无法发送,这意味着无法使用 Android Beacon 库的标准 API 进行距离估计。
| 归档时间: |
|
| 查看次数: |
911 次 |
| 最近记录: |