Android Bluetooth ScanFilter Partial String Matching

落爺英雄遲暮 提交于 2020-02-15 08:01:07

问题


I'm looking to use a ScanFilter to search for a set of bluetooth devices. I know the address of all these devices starts with 00:A0:50, and then the last 6 digits vary, so all addresses will look like 00:A0:50:XX:XX:XX. I'm looking for a way to use setDeviceAddress to find devices with addresses beginning with those 6 digits. This takes a string as input. The relevant code is below.

ScanFilter cypressFilter = new ScanFilter().Builder()
    //we know that their mac address will always start with 00:A0:50
    //so we should filter out any devices without that
    .setDeviceAddress(/* Address string goes here */)
    .build();

I think I'll need to use something like a regular expression for this, but I'm fairly new to Java, Android, and regex, and I'm not sure if I can pass in a regular expression to this function? Looking at the docs, I think I will need a Pattern or Matcher class to find the relevant strings. However, I'm not sure if this will work with this specific method which wants a specific string as input. I'm surprised it doesn't take an array as input, I would think that would be a more common use case than a single MAC address.


回答1:


Yes you can use regex & Pattern class

use following regex to match string address

00:A0:50:([A-Fa-f0-9]{2}:){2}[A-Fa-f0-9]

see demo DEMO

I'm surprised it doesn't take an array as input, I would think that would be a more common use case than a single MAC address.

For now you can use loop(i.e for , while.. etc) and match the address



来源:https://stackoverflow.com/questions/35322388/android-bluetooth-scanfilter-partial-string-matching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!