使用Android的BluetoothLeScanner时如何过滤制造商数据? [英] How to filter on manufacturer data when using BluetoothLeScanner for android?

查看:951
本文介绍了使用Android的BluetoothLeScanner时如何过滤制造商数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自己的BLE设备.当侦听这些设备时,我想使用ScanFilter,所以我只会得到我感兴趣的设备.我现在的解决方案是在回调内部进行过滤,但是如果这种过滤可以更早地进行,并且根据规范应该是可能的.我正在尝试过滤制造商特定的数据,但无法正常工作.这是我的代码:

I am working with my own BLE-devices. When listening after these devices I would like to use ScanFilter, so I only get the devices I am interested in. My solution right now is to filter inside the callback but it would be better if this filtration could happen earlier and according to the specification it should be possible. I am trying to filter on the manufacturer specific data but I can not get it to work. This is my code:

BluetoothLeScanner bleScanner = bluetoothAdapter.getBluetoothLeScanner();
ScanFilter filter = getScanFilter();
List<ScanFilter> scanFilters = new ArrayList<>();
scanFilters.add(filter);
ScanSettings scanSettings = getScanSettings();
bleScanner.startScan(scanFilters, scanSettings, scanCallback);

这是创建过滤器和设置的功能:

This is the functions that creates the filter and settings:

private ScanSetting getScanSettings(){
    ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setReportDelay(0);
    builder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
    return builder.build();
}

private ScanFilter getScanFilter(){
    ScanFilter.Builder builder = new ScanFilter.Builder();
    ByteBuffer manData = ByteBuffer.allocate(6); //The sensors only sends 6 bytes right now
    ByteBuffer manMask = ByteBuffer.allocate(6);
    manData.put(0, (byte)0x50);
    manData.put(1, (byte)0x41);
    manData.put(2, (byte)0x43);
    manData.put(3, (byte)0x4b);
    manData.put(4, (byte)0x45);
    manData.put(5, (byte)0x54);
    for(int i = 0; i < 6; i++){
        manMask.put((byte)0x01);
    }
    builder.setManufacturerData(20545, manData.array(), manMask.array()); //Is this id correct?
    return builder.build();
}

如果仅此功能不使用任何过滤器或设置:

If I don't use any filters or settings with only this function:

bluetoothLeScanner.startScan(scanCallback);

我得到了我的BLE设备,所以我知道它们正在正确广播.我还可以打印制造商特定的数据,并且可以看到它是我在过滤器中使用的6个相同的字节.我不确定id(.setManufacturerData函数中的第一个参数)是否正确,因为我能找到的唯一信息是来自ScanFilter.Builder:

I get my BLE-devices, so I know they are broadcasting correctly. I can also print the manufacturer specific data and can see that it is the 6 same bytes that I use in my filter. I am unsure if the id (the first parameter in the .setManufacturerData function) is correct because the only info about this I could find was from the following text from the android developer page for the ScanFilter.Builder:

"Note the first two bytes of the manufacturer Data is the manufacturerId"

当我使用此代码并尝试扫描设备后,我什么也没得到.我在这里想念什么?

When I use this code and try to scan after the devices I get nothing. What am i missing here?

推荐答案

我设法使其正常运行.这是ManufacturerId不正确.不是从前两个字节得到的20545.相反,我发现可以通过执行以下操作从ScanResult(当我不使用过滤器时)获取此ID:

I manage to get it to work. It was the manufacturerId that was not correct. It was not 20545 which I got from the first two bytes. Instead I found out that I could get this id from the ScanResult (when I used no filter) by doing the following:

ScanRecord scanRecord = scanResult.getScanRecord();
SparseArray<byte[]> manufacturerData = scanRecord.getManufacturerSpecificData();
for(int i = 0; i < manufacturerData .size(); i++){
    int manufacturerId = manufacturerData.keyAt(i);
}

这样做,我得到了正确的制造商ID,然后可以将其放置在函数中.

By doing this I got the correct manufacturerId that I then could place in the bleScanner.startScan function.

这篇关于使用Android的BluetoothLeScanner时如何过滤制造商数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆