【问题标题】:Android Bluetooth LE scan for UUID用于 UUID 的 Android 蓝牙 LE 扫描
【发布时间】:2014-11-05 10:20:06
【问题描述】:

我尝试了 BLE 的 android 示例应用程序, 但我无法获得所需的输出。 需要知道如何专门扫描 UUID 并获取设备正在传输的消息。

对于 BLE 发射器,我使用我的 macbook 作为发射器,使用 MacRadius 使其充当信标。

我只想检测我的 mac 的 uuid,然后从中获取一些信息。

需要代码示例

谢谢

【问题讨论】:

    标签: android bluetooth-lowenergy android-ibeacon


    【解决方案1】:

    可以使用以下 sn-p 从传递给 BluetoothAdapter.LeScanCallback#onLeScan() 的 scanRecord 参数解析主服务 UUID:

    public static List<UUID> parseServiceUuids(final byte[] advertisedData)
        {
            List<UUID> uuids = new ArrayList<UUID>();
    
             if( advertisedData == null )  return uuids;
    
            int offset = 0;
            while(offset < (advertisedData.length - 2))
            {
                int len = advertisedData[offset++];
                if(len == 0)
                    break;
    
                int type = advertisedData[offset++];
                switch(type)
                {
                    case 0x02: // Partial list of 16-bit UUIDs
                    case 0x03: // Complete list of 16-bit UUIDs
                        while(len > 1)
                        {
                            int uuid16 = advertisedData[offset++];
                            uuid16 += (advertisedData[offset++] << 8);
                            len -= 2;
                            uuids.add(UUID.fromString(String.format("%08x-0000-1000-8000-00805f9b34fb", uuid16)));
                        }
                        break;
                    case 0x06:// Partial list of 128-bit UUIDs
                    case 0x07:// Complete list of 128-bit UUIDs
                          // Loop through the advertised 128-bit UUID's.
                        while(len >= 16)
                        {
                            try
                            {
                                // Wrap the advertised bits and order them.
                                ByteBuffer buffer = ByteBuffer.wrap(advertisedData, offset++, 16).order(ByteOrder.LITTLE_ENDIAN);
                                long mostSignificantBit = buffer.getLong();
                                long leastSignificantBit = buffer.getLong();
                                uuids.add(new UUID(leastSignificantBit, mostSignificantBit));
                            }
                            catch(IndexOutOfBoundsException e)
                            {
                                // Defensive programming.
                                Log.e(TAG, e.toString());
                                continue;
                            }
                            finally
                            {
                                // Move the offset to read the next uuid.
                                offset += 15;
                                len -= 16;
                            }
                        }
                        break;
                    default:
                        offset += (len - 1);
                        break;
                }
            }
    
            return uuids;
        }
    

    然后您必须自己进一步解析 scanRecord 以从中获取更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2021-12-15
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多