简体   繁体   English

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

[英]How to stop bluetooth from scanning repeated found devices and how to use ScanFilter to restrict scan results

I am trying to create an indoor location app to detect own location and wayfinding. 我正在尝试创建一个室内位置应用,以检测自己的位置和寻路。 I have already set up my bluetooth app to scan and display found devices and beacons. 我已经设置了蓝牙应用程序来扫描和显示找到的设备和信标。 However i found out that some of the devices found were repeated. 但是我发现,发现的某些设备被重复了。 What do i need to do to stop it and i wanted to find only certain devices mac address. 我需要做什么才能停止它,我只想找到某些设备的mac地址。 May i know how to use the method ScanFilter and what is the difference between ScanFilter and ScanFilter.Builder ? 可我知道如何使用方法ScanFilter和之间有什么区别ScanFilterScanFilter.Builder Do i need to implement ScanSetting ? 我需要实施ScanSetting吗?

i have try putting this 我尝试把这个

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

I change the mac address only but didn't get any result. 我仅更改了mac地址,但未得到任何结果。 and can i do something like this since i know the first 6 digit is the same 我可以做这样的事情吗,因为我知道前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);
            }
        });
    }
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;
  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM