BLE startLeScan():null

时间:2017-01-27 07:54:40

标签: android bluetooth bluetooth-lowenergy android-bluetooth

现在我正在研究BLE。我发现了一个问题,它显示了startLeScan(): null的日志,扫描也无法正常工作我尝试了所有内容并搜索startLeScan(): null但是还没有得到任何解决方案。

这是我的BLE连接代码:

    private void scanLeDevice(boolean enable) {

            if (enable) {
                mBluetoothAdapter.startLeScan(mLeScanCallback);
            } else {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
            }
}

这是我发现此事的日志

01-27 13:12:21.357 4657-4657/com.icuisine D/BluetoothAdapter: stopLeScan()
01-27 13:12:23.003 4657-4657/com.icuisine D/BluetoothAdapter: startLeScan(): null
01-27 13:12:23.004 4657-4657/com.icuisine D/BluetoothAdapter: STATE_ON
01-27 13:12:23.007 4657-4668/com.icuisine D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5

我访问了许多网站,发现课程现已弃用,但我不知道如何获得替代方式进行扫描。

我正在使用Nexus 5 Ver Android 6.1进行测试,并且还为蓝牙应用运行时权限。

我知道这是重复的问题,但我没有找到原始解决方案的任何解决方案,这就是我重新发布的原因!

希望这将是足够的细节。需要帮助来做这件事。

提前致谢。

3 个答案:

答案 0 :(得分:0)

BluetoothAdapter.startLeScan已被弃用,可能正在使用BluetoothAdapter.bluetoothLeScanner.startScan来解决问题。

答案 1 :(得分:0)

我遇到了相同的问题,我找不到原因,但是当我清除对 Bluetooth 应用程序的缓存时,我的代码开始工作。值得尝试。

您可以按照以下步骤清除蓝牙应用程序缓存;

  1. 转到设置
  2. 选择“应用和通知”
  3. 点击“查看所有#个应用”
  4. 从右上方菜单中选择“显示系统”选项
  5. 找到“蓝牙”应用并单击它
  6. 选择“存储”选项
  7. 清除缓存

答案 2 :(得分:0)

在主要活动的onCreate()中调用以下函数 checkLocationPermission()

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

    public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                new AlertDialog.Builder(this)
                        .setTitle(R.string.title_location_permission)
                        .setMessage(R.string.text_location_permission)
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //Prompt the user once explanation has been shown
                                ActivityCompat.requestPermissions(DeviceControlActivity.this,
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        MY_PERMISSIONS_REQUEST_LOCATION);
                            }
                        })
                        .create()
                        .show();
            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }

还要在清单文件中添加以下权限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
相关问题