Android BLE 广告是通过任意长度的数据包来广播BLE设备的信息,以便其他设备识别和连接。下面是一个示例代码,可以在Android应用程序中使用:
// 获取 BluetoothAdapter
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
// 开启广告
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.setConnectable(false)
.build();
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(new ParcelUuid(UUID.fromString("0000110B-0000-1000-8000-00805F9B34FB")))
.addManufacturerData(0x6F6C, new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 })
.build();
bluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
bluetoothLeAdvertiser.startAdvertising(settings, data, advertisingCallback);
这段代码使用 BluetoothManager 和 BluetoothAdapter 来获取 BluetoothLeAdvertiser 对象并开启广告。在广告数据中,设置了设备名称,服务UUID和制造商数据。