Android蓝牙ScanFilter部分字符串匹配 [英] Android Bluetooth ScanFilter Partial String Matching
问题描述
我正在寻找使用ScanFilter搜索一组蓝牙设备.我知道所有这些设备的地址都以00:A0:50开头,然后最后6位数字不同,因此所有地址看起来都像00:A0:50:XX:XX:XX.我正在寻找一种使用 setDeviceAddress 查找地址以这6位数字开头的设备.这将字符串作为输入.相关代码如下.
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();
我认为我需要为此使用诸如正则表达式之类的东西,但是我对Java,Android和regex还是相当陌生,我不确定是否可以将正则表达式传递给此函数?看文档,我想我需要一个Pattern或Matcher类来找到相关的字符串.但是,我不确定这是否可以与需要特定字符串作为输入的特定方法一起使用.我很惊讶它没有采用数组作为输入,我认为这将是比单个MAC地址更常见的用例.
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.
推荐答案
是的,您可以使用 regex & 模式 课
Yes you can use regex & Pattern class
使用以下regex
来匹配字符串地址
use following regex
to match string address
00:A0:50:([A-Fa-f0-9]{2}:){2}[A-Fa-f0-9]
请参见演示演示
我很惊讶它没有采用数组作为输入,我认为 比单个MAC地址更常见.
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.
目前,您可以使用循环(即,while ..等)并匹配地址
For now you can use loop(i.e for , while.. etc) and match the address
这篇关于Android蓝牙ScanFilter部分字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!