BluetoothAdapter.LeScanCallback 的 onLeScan 未在 Android Marshmallow 中调用

onLeScan from BluetoothAdapter.LeScanCallback not called in Android Marshmallow

我刚刚更新了我的 Nexus 5,但我的应用程序功能已停止工作。

原因是接口LeScanCallback没有调用函数onLeScan。在下面的代码中,我使用了一些已弃用的代码,但我需要这样做,因为我正在测试一些技术。这就是我启动蓝牙适配器的方式:

@Override
protected void onStart()
{
    super.onStart();

    // Check if the device has BLE
    if (!this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
    {
        bleNotSupported();
        return;
    }

    // Initializes a Bluetooth adapter.
    final BluetoothManager bluetoothManager = (BluetoothManager)this.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())
    {
        bleNotEnabled();

        /*
        * Bluetooth can be enabled in app:
        *
        *    Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        *    btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        *    mAppContext.startActivity(btIntent);
        */
        return;
    }

    boolean bluetoothScanning = mBluetoothAdapter.startLeScan(mScanCallback); // this is true as well
    progressBar.setVisibility(View.VISIBLE);
}

@Override
protected void onStop() {
    super.onStop();

    if (mBluetoothAdapter != null)
    {
        mBluetoothAdapter.stopLeScan(mScanCallback);
    }
    progressBar.setVisibility(View.GONE);
    mDeviceAdapter.clearList();
}

BluetoothAdapter.LeScanCallback mScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord)
    {
//            if (previousBluetoothSelected == null)
        mDeviceAdapter.refreshList(device);

        if (device.getAddress().equalsIgnoreCase(previousBluetoothSelected)) {
            selectDeviceAndGo(device);
            mBluetoothAdapter.stopLeScan(this);
        }
    }
};

它适用于 API < 23 的每个设备,但不适用于 API = 23。

可能是什么原因?

非常感谢您。

此致。

拉斐尔.

某些装有 Marshmallow 的 Nexus 5 手机存在错误。在某些手机中,如果您的 GPS 关闭,扫描将不会开始。