当我启动ble(蓝牙Le)扫描几秒钟,然后停止扫描。然后开始,然后停止。在大约5-8圈之后,开始动作将没有效果,这意味着没有扫描记录可以接收。1.这种情况只出现在Android7.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);
}
};发布于 2017-06-16 06:03:49
在Android 7中,您可能会遇到新的、无文档的行为更改,从而阻止应用程序频繁扫描。
我写了一篇关于它的博文:https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983
发布于 2017-06-10 09:34:23
我也有这样的问题。它是通过启用位置解决的。在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();
}定位后开始扫描,得到扫描结果。
发布于 2017-08-02 07:25:55
你需要确保你停止和重新启动你的扫描后,只有在,6秒,的所有设备与安卓N及以上。
根据引入的限制,允许应用程序在30秒内最多扫描5次。
我们已经改变了从DP4开始的BLE扫描行为。我们将防止应用程序在30秒内启动和停止扫描超过5次。对于长期运行的扫描,我们将把它们转换为机会扫描。
请参阅本讨论这里
https://stackoverflow.com/questions/43114913
复制相似问题