我正在玩Android和iOS的实验性Bluetooth LE
程序,现在我在iOS端发送的Android端过滤信标广告上遇到了一些麻烦。 Android有一个功能,可以通过UUID使用掩码过滤广告(我需要这个,因为我试图从特定值开始获取UUID)但我无法使其工作。这是我正在尝试的内容:
private void freshen() {
// kill current scanner
if (mLEScanner != null)
mLEScanner.stopScan(mScanCallback);
// set new one to be a match scanner
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
List<ScanFilter> filters = new ArrayList<>();
// create data uuid starting with the value 1
ParcelUuid data = ParcelUuid.fromString("10000000-0000-0000-0000-000000000000");
// create a mask to ensure only that value is searched
ParcelUuid mask = ParcelUuid.fromString("10000000-0000-0000-0000-000000000000");
// build the filter
ScanFilter.Builder builder = new ScanFilter.Builder();
builder.setServiceUuid(data, mask);
ScanFilter filter = builder.build();
// add the filter to the scanner
filters.add(filter);
mLEScanner.startScan(filters, settings, mScanCallback);
}
产生零结果。即使我将所有掩码值设置为0(这是忽略的数字),它仍然会产生零结果。当我删除过滤器时,会出现以1开头的大量结果。这在当前版本的Android中是否已被破坏?
答案 0 :(得分:0)
它不能解决这个问题,但我决定使用DeviceName进行过滤,因为这可以由Android和iOS双方填充。现在我专注于iOS到Android的通信=&gt;
的iOS:
func centralManager(_: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber){
delegate?.didDiscoverPeripheral(peripheral)
let splitUp : [String] = "\(advertisementData)".components(separatedBy: "\n")
if (splitUp.count > 1)
{
var chop = splitUp[1]
let counter = chop.characters.count - 2
chop = chop[0..<counter]
let chopSplit : [String] = "\(chop)".components(separatedBy: "\"")
if !(chopSplit.count > 1 && chopSplit[1] == "Device Information" && !sending)
{
let hexString = chop[4..<7] + chop[12..<19] + chop[21..<26]
let hexArray = [hexString[0..<1], hexString[2..<3], hexString[4..<5], hexString[6..<7], hexString[8..<9], hexString[10..<11], hexString[12..<13], hexString[14..<15], hexString[16..<17]]
let charArray = hexArray.map { Character(hexToScalar(char: $0)) }
let string = String(charArray) as String
if (string == "GoTcHa" + stringify(number: writing))
{
writing += 1
let messageUUID = StringToUUID(hex: String(writing) + "hello" + String(writing))
peripheralManager.stopAdvertising()
let name = String(writing) as NSString
peripheralManager.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [CBUUID(string: messageUUID)], CBAdvertisementDataLocalNameKey: name])
}
}
}
}
func StringToUUID(hex: String) -> String
{
var rev = String(hex.characters.reversed())
let hexData: NSData! = rev.data(using: String.Encoding.utf8, allowLossyConversion: false) as NSData!
rev = hexData.toHexString()
while(rev.characters.count < 31) {
rev = "0" + rev;
}
rev = String(writing % 10) + rev[0..<30]
let finalString = rev[0..<7] + "-" + rev[8..<11] + "-" + rev[12..<15] + "-" + rev[16..<19] + "-" + rev[20..<31]
return finalString
}
更重要的是Android扫描方法,它现在通过始终通过DeviceName查找下一条消息来忽略来自iOS的重复消息:
private void freshen() {
if (mLEScanner != null)
mLEScanner.stopScan(mScanCallback);
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
.build();
List<ScanFilter> filters = new ArrayList<>();
ScanFilter filter = new ScanFilter.Builder()
.setDeviceName("" + (reading % 10))
.build();
filters.add(filter);
mLEScanner.startScan(filters, settings, mScanCallback);
}
我需要让iOS方面现在更好地过滤Android的响应,但它在Android接收方面有效。