当前位置: 首页 > 知识库问答 >
问题:

如何按类型过滤附近的蓝牙设备

康恩
2023-03-14

我想浏览一下附近运行WearOS的Android智能手表,并与之建立联系。我使用CompanionDeviceManager遵循了这里的说明:https://developer.android.com/guide/topics/connectivity/companion-device-pairing

CompanyDeviceManager始终显示所有设备类型的列表,包括其他手机、笔记本电脑、蓝牙扬声器和手表(包括绑定和未配对的)。与Google Wear OS应用程序相比,它也使用了CompanyDeviceManager(因为我看到了相同的弹出界面),并且它总是只显示Android手表。

它如何在众多其他蓝牙设备中过滤智能手表?

这是我的代码

private  fun scanLeDevice(enable: Boolean) {
    val deviceFilter: BluetoothLeDeviceFilter = BluetoothLeDeviceFilter.Builder()
        .build()

    // The argument provided in setSingleDevice() determines whether a single
    // device name or a list of device names is presented to the user as
    // pairing options.
    val pairingRequest: AssociationRequest = AssociationRequest.Builder()
        .setSingleDevice(false)
        .addDeviceFilter(deviceFilter)
        .build()

    // When the app tries to pair with the Bluetooth device, show the
    // appropriate pairing request dialog to the user.
    deviceManager.associate(pairingRequest,
        object : CompanionDeviceManager.Callback() {

            override fun onDeviceFound(chooserLauncher: IntentSender) {
                startIntentSenderForResult(chooserLauncher,
                    Companion.SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0)
            }

            override fun onFailure(error: CharSequence?) {
                // Handle failure
            }
        }, null)
}

共有3个答案

龙凯
2023-03-14

尝试重新启动设备。仅在主要时间,因为一旦它出现在SmartWatch应用程序列表中,您可能不会遇到相同的问题。

曹均
2023-03-14

请看那篇文章:https://medium.com/@martijn.van.welie/making-android-ble-work-part-1-a736dcd53b02

TL; TR

String[] names = new String[]{"Polar H7 391BB014"};
List<ScanFilter> filters = null;
if(names != null) {
    filters = new ArrayList<>();
    for (String name : names) {
        ScanFilter filter = new ScanFilter.Builder()
                .setDeviceName(name)
                .build();
        filters.add(filter);
    }
}
scanner.startScan(filters, scanSettings, scanCallback);
伊俊能
2023-03-14

备注:这不是完整的答案,因为我不知道每种设备类型的过滤掩码是什么,但至少它可以帮助过滤Android智能手表

我反编译了Google WearOS应用程序并查看源文件FindDeviceControlllerSdk26.java.以下是感兴趣的事情:

 private static final byte[] BLE_WEAR_MATCH_DATA = { 0, 20 };
  private static final byte[] BLE_WEAR_MATCH_DATA_LEGACY = { 0, 19 };
  private static final byte[] BLE_WEAR_MATCH_MASK = { 0, -1 };
  private final void showAssociationDialog()
  {
    Object localObject1 = new ArrayList();
    Object localObject3 = new FindDeviceDeviceFilterBuilder();
    medium = 1;
    Object localObject4 = new ScanFilter.Builder();
    byte[] arrayOfByte = BLE_WEAR_MATCH_DATA;
    Object localObject2 = BLE_WEAR_MATCH_MASK;
    scanFilter = ((ScanFilter.Builder)localObject4).setManufacturerData(224, arrayOfByte, (byte[])localObject2).build();
    FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject3, (List)localObject1);
    localObject3 = new FindDeviceDeviceFilterBuilder();
    medium = 1;
    scanFilter = new ScanFilter.Builder().setManufacturerData(224, BLE_WEAR_MATCH_DATA_LEGACY, (byte[])localObject2).build();
    FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject3, (List)localObject1);
    if (!deviceWhitelist.shouldShowAllDevices())
    {
      localObject2 = (GServicesFindDeviceWhitelist)deviceWhitelist;
      ((GServicesFindDeviceWhitelist)localObject2).initializeIfNecessary();
      localObject2 = cachedDeviceWhitelistRegularExpression;
      if (!TextUtils.isEmpty((CharSequence)localObject2))
      {
        localObject3 = new FindDeviceDeviceFilterBuilder();
        medium = 0;
        namePatternString = ((String)localObject2);
        FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject3, (List)localObject1);
      }
    }
    else
    {
      localObject2 = new FindDeviceDeviceFilterBuilder();
      medium = 0;
      namePatternString = ".*";
      FindDeviceAssociationRequestBuilder.unbox_addDeviceFilter$ar$ds((FindDeviceDeviceFilterBuilder)localObject2, (List)localObject1);
    }
    localObject2 = new AssociationRequest.Builder().setSingleDevice(false);
    localObject3 = ((List)localObject1).iterator();
    while (((Iterator)localObject3).hasNext())
    {
      localObject1 = (FindDeviceDeviceFilterBuilder)((Iterator)localObject3).next();
      if (medium != 0)
      {
        localObject4 = new BluetoothLeDeviceFilter.Builder();
        localObject1 = scanFilter;
        if (localObject1 != null) {
          ((BluetoothLeDeviceFilter.Builder)localObject4).setScanFilter((ScanFilter)localObject1);
        }
        localObject1 = ((BluetoothLeDeviceFilter.Builder)localObject4).build();
      }
      else
      {
        localObject4 = new BluetoothDeviceFilter.Builder();
        if (!Platform.stringIsNullOrEmpty(namePatternString)) {
          ((BluetoothDeviceFilter.Builder)localObject4).setNamePattern(Pattern.compile(namePatternString));
        }
        localObject1 = ((BluetoothDeviceFilter.Builder)localObject4).build();
      }
      ((AssociationRequest.Builder)localObject2).addDeviceFilter((DeviceFilter)localObject1);
    }
    localObject1 = ((AssociationRequest.Builder)localObject2).build();
    viewClient.hideRefreshButton();
    cwEventLogger.incrementCounter(Counter.COMPANION_PAIR_CDM_ASSOCIATE_REQUEST);
    companionDeviceManager.associate((AssociationRequest)localObject1, deviceManagerCallback, null);
  }
  

然后,我将扫描html" target="_blank">函数重写如下

    private val BLE_WEAR_MATCH_DATA = byteArrayOf(0, 20)
    private val BLE_WEAR_MATCH_DATA_LEGACY = byteArrayOf(0, 19)
    private val BLE_WEAR_MATCH_MASK = byteArrayOf(0, -1)
   private  fun scanLeDevice(enable: Boolean) {
        val scanFilter = ScanFilter.Builder().setManufacturerData(224,BLE_WEAR_MATCH_DATA_LEGACY,BLE_WEAR_MATCH_MASK).build()
        val deviceFilter: BluetoothLeDeviceFilter = BluetoothLeDeviceFilter.Builder()
            .setScanFilter(scanFilter)
            .setNamePattern(Pattern.compile(".*"))
            .build()
        // The argument provided in setSingleDevice() determines whether a single
        // device name or a list of device names is presented to the user as
        // pairing options.
        val pairingRequest: AssociationRequest = AssociationRequest.Builder()
            .setSingleDevice(false)
            .addDeviceFilter(deviceFilter)
            .build()

        // When the app tries to pair with the Bluetooth device, show the
        // appropriate pairing request dialog to the user.
        deviceManager.associate(pairingRequest,
            object : CompanionDeviceManager.Callback() {

                override fun onDeviceFound(chooserLauncher: IntentSender) {
                    startIntentSenderForResult(chooserLauncher,
                        Companion.SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0)
                }

                override fun onFailure(error: CharSequence?) {
                    // Handle failure
                }
            }, null)
    }

我希望有人能帮我描述一下这些面具代表着什么,然后我会帮助更新,使这个答案完整。

私有BLE_WEAR_MATCH_DATA=byteArrayOf(0,20)

private val BLE\u WEAR\u MATCH\u DATA\u LEGACY=byteArrayOf(0,19)

private val BLE\u WEAR\u MATCH\u MASK=byteArrayOf(0,-1)

 类似资料:
  • 问题内容: 我正在寻找一种以编程方式列出我的设备所找到的附近任何蓝牙设备(可发现)的方法。我无法找到有关在Swift3.0中执行此调用的任何信息或教程。这篇QA帖子讨论了使用Swift 1.0查找这些设备并在Xcode 6(而不是最新版本8)中进行构建。 我尽力将代码从1.0转换为3.0语法,但是在运行以下代码时,Playground不返回任何内容: 问题答案: 正确使用IOBluetooth 下

  • 我试图用BLE API扫描附近的蓝牙设备,但它似乎不起作用 我已经在清单中添加了权限 以下内容在创建对象时 创建扫描仪回调对象并进一步扫描 在logcat中,我只看到以下内容 这是我的app build gradle } 有人能指出我在这里遗漏了什么吗?

  • ap.stopBluetoothDevicesDiscovery() 停止搜寻附近的蓝牙外围设备。 错误码说明 error 描述 12 停止搜索失败 代码示例 <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script> <button class

  • ap.startBluetoothDevicesDiscovery(OPTION | services | service, CALLBACK) 开始搜寻附近的蓝牙外围设备。搜索结果将在 bluetoothDeviceFound 事件中返回。可直接传入一个数组作为 OPTION.services 参数。也可直接传入一个字符串作为 OPTION.services 的第一项。 OPTION 参数说明

  • 我有一个通过蓝牙连接的配对设备列表。但是我想把它们归类为iPhone或Android。这怎么可能呢?

  • 问题内容: 我正在尝试使用Linux上的Python 列出所有附近的/可发现的蓝牙设备, 包括已经配对的 蓝牙设备。 我知道如何使用其地址列出设备的服务,并且可以成功连接: 如果我不指定任何条件,则阅读PyBluez文档时,我希望附近的任何设备都能显示出来: “如果未指定任何条件,则返回检测到的所有附近服务的列表。” 我现在唯一需要的是能够列出已经配对的设备,无论它们是打开,关闭,附近还是不打开。