我正在尝试使用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);
}
}
CoreBluetooth不是一种格式。它是iOS上的一组API。使用CoreBluetooth,您可以检测到各种有限制的信标格式:
要在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。