Android 7.0 ble扫描没有结果

时间:2017-03-30 10:16:46

标签: 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);
    }
};

4 个答案:

答案 0 :(得分:5)

您可能会遇到Android 7中新的和未记录的行为更改,导致应用程序无法经常扫描。

我写了一篇关于它的博文: https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983

答案 1 :(得分:2)

我也遇到过这种问题。它通过启用位置来解决。在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();
}

在位置开始扫描并获得扫描结果后。

答案 2 :(得分:1)

对于Android N及以上版本的所有设备,您需要确保仅在 6秒之后停止并重新开始扫描。

这是根据引入的限制,允许应用在30秒内最多扫描5次。

  

我们已经改变了从DP4开始的BLE扫描行为。我们会阻止   30秒内启动和停止扫描的应用程序超过5次   秒。对于长时间运行的扫描,我们会将它们转换为机会主义   扫描。

请参阅此讨论here

答案 3 :(得分:0)

您还必须在 清单 中包含位置权限,否则搜索将不会返回任何内容。对于蓝牙 Wifi 搜索,这是强制性的。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

与Android中BLE的develoepr部分相同。

enter image description here