如何阻止蓝牙扫描重复发现的设备以及如何使用ScanFilter限制扫描结果

时间:2019-07-11 02:57:27

标签: java android bluetooth-lowenergy

我正在尝试创建一个室内位置应用,以检测自己的位置和寻路。我已经设置了蓝牙应用程序来扫描和显示找到的设备和信标。但是我发现,发现的某些设备被重复了。我需要做什么才能停止它,我只想找到某些设备的mac地址。我可以知道如何使用方法ScanFilterScanFilterScanFilter.Builder有什么区别?我需要实现ScanSetting吗?

我尝试将其放置

ScanFilter scanFilterMac = new ScanFilter.Builder().setDeviceAddress("88:88:88:B0:03:DB").build(); 

我仅更改了mac地址,但未得到任何结果。我可以做这样的事情吗,因为我知道前6位数字是相同的

ScanFilter scanFilterMac = new ScanFilter.Builder().setDeviceAddress("68:54:F5:([A-Fa-f0-9]{2}:){2}([A-Fa- 
f0-9]{2})").build();
    public void startScanning() {

        final BluetoothLeScanner btScanner = btAdapter.getBluetoothLeScanner();
        if (btScanner == null) {
            // not enabled yet or not supported
            return;
        }

        System.out.println("start scanning");
        peripheralTextView.setText("");
        startScanningButton.setVisibility(View.INVISIBLE);
        stopScanningButton.setVisibility(View.VISIBLE);
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                ScanFilter scanFilterMac = new ScanFilter.Builder().setDeviceAddress("68:54:F5:88:88:88").build();
                listFilter.add(scanFilterMac);
                btScanner.startScan(listFilter,leScanCallback);
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

final class BleScanCallback extends ScanCallback {
  // address -> device map
  private final Map<String, BluetoothDevice> scanResults;

  public void onScanResult(int callbackType,  ScanResult result) {
     this.addScanResult(result);
  }

  public void onBatchScanResults( List results) {
     for (final Object result1 : results) {
        ScanResult result = (ScanResult) result1;
        this.addScanResult(result);
     }

  }

  public void onScanFailed(int errorCode) {
  }

  private void addScanResult(ScanResult result) {
     BluetoothDevice device = result.getDevice();
     String name = device.getName();
     if (name != null  && !this.scanResults.containsKey(device.getAddress())) {
        String var6 = device.getAddress();
        this.scanResults.put(var6, device);
     } else {
        StringBuilder var10001 = (new StringBuilder()).append("Ble addScanResult ignore ");
        Log.d("Ble", var10001.append(device.getName()).append(' ').append(device.getType()).append(' ').append(device.getAddress()).append(' ').toString());
     }

  }

  public BleScanCallback( Map<String, BluetoothDevice> scanResults) {
     super();
     this.scanResults = scanResults;
  }
 }