我有两个使用BLE进行通信的Android设备。一个是广告而另一个是扫描和接收。当我使用服务UUID做广告时,其他设备会很好地接收它。但是,当我按服务UUID过滤时,扫描设备根本不会接收广告。两个设备都使用完全相同的UUID进行广告/扫描。扫描设备正在运行5.0.1。这是我为scanfilter设置的代码:
P_UUID = new ParcelUuid(UUID.fromString("6be5ab1b-328b-4709-a737-84db596e10d2"));
B_scanFilter = new ScanFilter.Builder().setServiceUuid(P_UUID).build();
list = new ArrayList<ScanFilter>();
list.add(B_scanFilter);
在广告设备上,这是我对AdvertiseData的代码:
//The random UUID is just a placeholder. Using P_UUID for both the serviceUUID and serviceDataUUID doesn't change anything
B_advertiseData = new AdvertiseData.Builder().addServiceUuid(P_UUID).addServiceData(new ParcelUuid(UUID.randomUUID()), getByteArray(data)).build();
最后,这里是scanCallback和startScan代码:
B_scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
Toast.makeText(getApplicationContext(),"Received Data", Toast.LENGTH_SHORT).show();
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
Toast.makeText(getApplicationContext(),"Received Data(Batch)", Toast.LENGTH_SHORT).show();
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Toast.makeText(getApplicationContext(),"Scan failed", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Scan Error: " + Integer.toString(errorCode));
}
};
B_scanner.startScan(list, B_scanSettings, B_scanCallback);
Toast.makeText(getApplicationContext(),"Scan Started", Toast.LENGTH_SHORT).show();
请注意,使用B_scanCallback时,扫描期间不会调用任何方法(甚至onScanFailed())。 任何有关此问题的帮助将不胜感激!如果你想让我发布更多代码,我可以做到。我只发布了我认为可能导致此问题的代码。