簡體   English   中英

如何阻止藍牙掃描重復發現的設備以及如何使用ScanFilter限制掃描結果

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

我正在嘗試創建一個室內位置應用,以檢測自己的位置和尋路。 我已經設置了藍牙應用程序來掃描和顯示找到的設備和信標。 但是我發現,發現的某些設備被重復了。 我需要做什么才能停止它,我只想找到某些設備的mac地址。 可我知道如何使用方法ScanFilter和之間有什么區別ScanFilterScanFilter.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);
            }
        });
    }
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