+-
Android棉花糖中未调用BluetoothAdapter.LeScanCallback上的onLeScan
我刚刚更新了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. 可能是什么原因? 提前非常感谢您. 问候. 拉斐尔.

最佳答案
某些配备棉花糖的Nexus 5手机存在错误.在某些手机中,如果您的gps已关闭,则扫描不会开始.
点击查看更多相关文章

转载注明原文:Android棉花糖中未调用BluetoothAdapter.LeScanCallback上的onLeScan - 乐贴网