Bluetooth 当iOS设备正在进行广告时,在android上读取广告数据

Bluetooth 当iOS设备正在进行广告时,在android上读取广告数据,bluetooth,bluetooth-lowenergy,android-bluetooth,ios-bluetooth,Bluetooth,Bluetooth Lowenergy,Android Bluetooth,Ios Bluetooth,我有个问题。我的android代码与iOS蓝牙广告不兼容。我在网上看到,广告是在iOS设备之间进行的 但我认为如果iOS可以阅读,android为什么不能呢?android设备是否接收字节数组中的UUID? 我如何解析这些呢?可能吗 当iOS使用以下代码时,如何在Android设备上读取广告数据: CBPeripheralManager *manager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; NSArra

我有个问题。我的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;
}
public void onLeScan(蓝牙设备,int-rssi,字节[]扫描记录){
parceluid[]uuids=device.getUuids();
列表ud=parseUuids(扫描记录);
如果(ud!=null){
适用于(UUID u:ud){
Timber.v(“UUID:%s”,u);
}
}
}
专用静态列表ParseUUID(字节[]广告数据){
List uuids=new ArrayList();
ByteBuffer buffer=ByteBuffer.wrap(广告数据).order(ByteOrder.LITTLE_ENDIAN);
while(buffer.remaining()>2){
字节长度=buffer.get();
如果(长度==0)中断;
字节类型=buffer.get();
开关(类型){
案例0x02://16位UUID的部分列表
案例0x03://16位UUID的完整列表
而(长度>=2){
添加(UUID.fromString(String.format(
“%08x-0000-1000-8000-00805f9b34fb”,buffer.getShort());
长度-=2;
}
打破
案例0x06://128位UUID的部分列表
案例0x07://128位UUID的完整列表
案例0x15:
而(长度>=16){
long lsb=buffer.getLong();
long msb=buffer.getLong();
添加(新的UUID(msb,lsb));
长度-=16;
}
打破
违约:
buffer.position(buffer.position()+长度-1);
打破
}
}
返回uuid;
}

播发限制为31字节,因此没有空间放置几个128位UUID。您可以尝试将
[CBUUID uuid with string:@“16位uuid”]
传递到广告数据。谢谢。它正在工作。