我正在创建一个扫描外围设备的应用程序。这是我开始扫描的代码:
private void startScan(){
bleScanner = bleAdapter.getBluetoothLeScanner();
List<ScanFilter> filters = new ArrayList<ScanFilter>();
filters.add(new ScanFilter.Builder().build());
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.setReportDelay(1000)
.build();
bleScanner.startScan(filters,settings,scanCallback);
Log.i(TAG,"Scan started");
}
这是扫描回叫:
private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.i("scan found something");
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
if(!results.isEmpty()){
BluetoothDevice advert = results.get(0).getDevice();
Log.i("found device: "+advert.getAddress());
stopScan();
startClient(advert);
}else{
Log.i("scanning...");
}
}
@Override
public void onScanFailed(int errorCode) {
Log.i("Scan failed to start");
}
};
我有两个Android设备,我正在测试这个应用程序,一个在API 21上运行,另一个在23上运行。当我在带有API 22的设备上测试此应用程序时,回调功能正常,但在设备上测试时API 23我确实得到了“扫描启动”日志,但scanCallBack无效。 我添加了所有必需的权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
任何帮助将不胜感激,谢谢!