Can an Android 4.4 device act as an iBeacon? [closed]

99封情书 提交于 2019-12-01 06:56:06

问题


In an answer to another question, I saw that "You can also transmit as a beacon on rooted Android 4.4.3 devices, but it requires an app installed with system privileges."

How can this be done?


回答1:


Yes, this is possible on 4.4.3, but the critical API methods startAdvertising(), stopAdvertising() and getAdvScanData() (which allows you to read and write the raw information sent out in the advertisement) are blocked from use unless an app has android.permission.BLUETOOTH_PRIVILEGED. This is a system-level permission, so the only way to get this is for your custom app is to root your phone, and install your app in the /system/priv-app directory.

If you can accomplish that, the basic code to do this is:

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);   

The above code shows you how to transmit an open source AltBeacon. But you can transmit other beacon types by changing the byte pattern.

Another important restriction in Android 4.4 is that a bug prevents you from advertising more than 24 bytes of data, instead of the 26 that should be allowed. This means that beacon advertisements may be truncated if they require more than 24 bytes. AltBeacon, for example, uses the second of those last two bytes to store the calibrated transmitter power. Because this cannot be sent, that means distance estimates are not possible using the Android Beacon Library's standard APIs.

You can see a description of how this is done here



来源:https://stackoverflow.com/questions/34424479/can-an-android-4-4-device-act-as-an-ibeacon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!