我正在尝试创建一个室内位置应用,以检测自己的位置和寻路。我已经设置了蓝牙应用程序来扫描和显示找到的设备和信标。但是我发现,发现的一些设备被重复了。我需要做什么来阻止它,我只想找到某些设备的mac地址。我可以知道如何使用方法ScanFilter
吗?ScanFilter
和ScanFilter.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;
}
}