Dan*_*Kee 3 android bluetooth-lowenergy android-7.0-nougat
当我开始ble(蓝牙Le)扫描几秒钟,然后停止扫描.然后启动,然后停止......大约5-8次循环后,启动动作将无效,这意味着无法接收扫描记录.1.此条件仅出现在Android 7.0或更高版本(7.1.1); 2.我尝试了两种扫描方法:BluetoothAdapter.startLeScan()和Scanner.startScan(),没有区别.
private void scanToggle(final boolean enable) {
mScanHandler.removeCallbacks(scanTask);
if (enable) {
TelinkLog.i("ADV#scanner#startScan");
scanner = mBluetoothAdapter.getBluetoothLeScanner();
scanner.startScan(null, settings, scanCallback);
mScanning = true;
mDeviceList.clear();
mListAdapter.notifyDataSetChanged();
//mBluetoothAdapter.startLeScan(leScanCallback);
mScanHandler.postDelayed(scanTask, SCAN_PERIOD);
} else {
TelinkLog.i("ADV#scanToggle#stopScan");
mScanning = false;
//mBluetoothAdapter.stopLeScan(leScanCallback);
scanner.stopScan(scanCallback);
}
invalidateOptionsMenu();
}
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
TelinkLog.d("scan:" + device.getName());
for (AdvDevice advDevice : mDeviceList) {
if (device.getAddress().equals(advDevice.device.getAddress())) return;
}
mDeviceList.add(new AdvDevice(device, rssi, scanRecord));
runOnUiThread(new Runnable() {
@Override
public void run() {
mListAdapter.notifyDataSetChanged();
}
});
}
};
private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
for (AdvDevice advDevice : mDeviceList) {
if (result.getDevice().getAddress().equals(advDevice.device.getAddress())) return;
}
mDeviceList.add(new AdvDevice(result.getDevice(), result.getRssi(), result.getScanRecord().getBytes()));
runOnUiThread(new Runnable() {
@Override
public void run() {
mListAdapter.notifyDataSetChanged();
}
});
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
}
};
Run Code Online (Sandbox Code Playgroud)
您可能会遇到Android 7中新的和未记录的行为更改,这些更改会阻止应用程序过于频繁地扫描.
我写了一篇关于它的博客文章:https: //blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983
小智 4
我也遇到过这样的问题。通过启用位置即可解决。在android中,我们需要启用位置扫描附近的设备。所以请检查位置是否启用。下面的代码很有用。我希望它能起作用,就我而言,效果很好。
LocationManager manager = (LocationManager) mainActivity.getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps(mainActivity);
}else{ //scanning part}
public void buildAlertMessageNoGps(MainActivity mainActivity){
final AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it? It is required for this application.")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
mainActivity.startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), MainActivity.LOCATION_ENABLE);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
mainActivity.finishAffinity();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Run Code Online (Sandbox Code Playgroud)
到达位置后开始扫描并获取扫描结果。
| 归档时间: |
|
| 查看次数: |
5482 次 |
| 最近记录: |