我有个问题。我的 android 代码与 iOS 蓝牙广告不兼容。我在苹果文档上读到,广告是在 iOS 设备之间进行的。
但是我认为如果iOS可以读取它,那么android为什么不可以呢?android 设备是否接收到字节数组中的 uuid?我该如何解析这些?可能吗?
当 iOS 使用此代码时,如何在 Android 设备上读取广告数据:
CBPeripheralManager *manager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
NSArray *uuids = @[[CBUUID UUIDWithString:@"128bit uuudid"],
[CBUUID UUIDWithString:@"other 128bit uuudid"],
[CBUUID UUIDWithString:@"128bit uuudid"],
....];
NSDictionary *data = @[CBAdvertisementDataLocalNameKey: @"My name",
CBAdvertisementDataServiceUUIDsKey: uuids];
[manager startAdvertising:data];
我尝试在 Android 上扫描 UUID,但它只读取第一个 uuid。我如何阅读其他人?
我使用此代码在 Android 上解析 UUID,但它不起作用。
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
ParcelUuid[] uuids = device.getUuids();
List<UUID> ud = parseUuids(scanRecord);
if (ud != null) {
for (UUID u : ud) {
Timber.v("UUUUID: %s", u);
}
}
}
private static List<UUID> parseUuids(byte[] advertisedData) {
List<UUID> uuids = new ArrayList<UUID>();
ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(ByteOrder.LITTLE_ENDIAN);
while (buffer.remaining() > 2) {
byte length = buffer.get();
if (length == 0) break;
byte type = buffer.get();
switch (type) {
case 0x02: // Partial list of 16-bit UUIDs
case 0x03: // Complete list of 16-bit UUIDs
while (length >= 2) {
uuids.add(UUID.fromString(String.format(
"%08x-0000-1000-8000-00805f9b34fb", buffer.getShort())));
length -= 2;
}
break;
case 0x06: // Partial list of 128-bit UUIDs
case 0x07: // Complete list of 128-bit UUIDs
case 0x15:
while (length >= 16) {
long lsb = buffer.getLong();
long msb = buffer.getLong();
uuids.add(new UUID(msb, lsb));
length -= 16;
}
break;
default:
buffer.position(buffer.position() + length - 1);
break;
}
}
return uuids;
}