首页 > 解决方案 > 是否可以过滤蓝牙扫描结果以显示具有特定名称的设备

问题描述

我的应用程序扫描蓝牙设备,我只希望它显示具有特定名称的设备(限制用户查看的结果)。不完全确定这是否可以实现?请看下面的截图

当前蓝牙扫描结果: 在此处输入图像描述

期望的结果: 在此处输入图像描述

标签: android

解决方案


是的,你可以过滤掉它。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
      // Discovery has found a device. Get the BluetoothDevice
      // object and its info from the Intent.
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      String deviceName = device.getName();
      // 
      // use try-catch to keep tying on the logic -- John Melody added
      try {
        if (deviceName.contains("somethingsomething")) {
          System.out.println(String.format("Ok %s", deviceName));
        }
      } catch(Exception e) {
        e.printStackTrace();
      }
    //
    }
  }
};

结帐[this][1] 链接

[1] : https://developer.android.com/guide/topics/connectivity/bluetooth#FindingDevices


推荐阅读