【问题标题】:onLeScan function not called when trying to detect BLE devices尝试检测 BLE 设备时未调用 onLeScan 函数
【发布时间】:2016-04-27 11:45:21
【问题描述】:

我正在尝试在服务上检测附近的低功耗蓝牙设备。 服务启动时调用startLeScan(),10秒后调用stopLeScan()。 尽管 startLeScan() 返回 true,并且我没有收到任何错误,但 LeScanCallback 上的 onLeScan 是不叫。 服务

    .
    .
    .
            // Device scan callback.
        private BluetoothAdapter.LeScanCallback mLeScanCallback =
                new BluetoothAdapter.LeScanCallback() {

                    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
                        Log.i(TAG, "New LE Device: " + device.getName() + " @ " + rssi);
                        if (DEVICE_NAME.equals(device.getName()) && deviceAddress.equals(device.getAddress())) {
                            mDevice = device;
                            BleScan(false);
                            connectBluetoothDevice();
                        }
                    }
                };

    /**
     * Starts and stops Bluetooth LE scanning for BLE devices.
     *
     * @param enable true to start scanning, false to stop scanning.
     */
    public void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mScanning) {
                        BleScan(false);
                    }
                }
            }, SCAN_PERIOD);
            BleScan(true);
        } else {
            BleScan(false);
        }

    }

    /**
     * @param scan true to scan, and false to stop scanning for Bluetooth LE devices.
     */
    private void BleScan(boolean scan) {
        if (scan) {
            mScanning = true;
            boolean temp = mBluetoothAdapter.startLeScan(mLeScanCallback);
            Log.i(TAG, "Started LE scan: " + temp);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

我尝试使用 BluetoothLeScanner startScan()stopScan(),并改用 ScanCallback,但是它没有帮助:

    ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            Log.i(TAG, "New LE Device: " + result.getDevice().getName() + " @ " + result.getRssi());
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.i(TAG, "Scan Faild!!");
        }
    };


    /**
     * Starts and stops Bluetooth LE scanning for BLE devices.
     *
     * @param enable true to start scanning, false to stop scanning.
     */
    public void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mScanning) {
                        BleScan(false);
                    }
                }
            }, SCAN_PERIOD);
            BleScan(true);
        } else {
            BleScan(false);
        }

    }

    /**
     * @param scan true to scan, and false to stop scanning for Bluetooth LE devices.
     */
    private void BleScan(boolean scan) {
        if (scan) {
            mScanning = true;
            mBluetoothLeScanner.startScan(mScanCallback);
            Log.i(TAG, "Started LE scan");
        } else {
            mScanning = false;
            mBluetoothLeScanner.stopScan(mScanCallback);
        }
    }

有人说需要打开 GPS,所以我打开了 GPS。 我尝试重新启动android设备。 另一个 BLE 检测应用可以看到 BLE 设备。

为什么不调用扫描回调?

编辑

我在运行 Android 6.0.1 的 nexus 5 设备上运行此应用。

我尝试添加位置权限,但没有帮助:

( uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" )

【问题讨论】:

  • 您能告诉我们,您使用的是哪个手机和安卓版本吗? Android 6 需要位置权限才能获得扫描结果。
  • 我在运行 Android 6.0.1 的 nexus 5 设备上运行此应用程序。我尝试添加位置权限,但没有帮助。
  • @AmitaiFensterheim 您的目标是什么 SDK?如果您的目标是 >= 23,则还必须在运行时明确请求许可。
  • 只是添加到 SJoshis 评论看看developer.android.com/training/permissions/requesting.html,这将解释如何请求适当的权限
  • 谢谢!有用!为什么蓝牙 LE 需要位置权限?我应该使用哪个 BLE 扫描功能,startLeScan 还是 startScan

标签: android android-service bluetooth-lowenergy


【解决方案1】:

我也有类似的问题。 相同的代码,一台平板电脑无法使用,而另一台则可以。 我没有解决办法,但是当我重置我的平板电脑并重新安装 apk 时,它又可以正常工作了。

我找到了这个onLeScan from BluetoothAdapter.LeScanCallback not called in Android Marshmallow 当我打开 GPS 时,它的工作...

【讨论】:

    【解决方案2】:

    尝试在应用设置(设置->应用->你的应用->规则->位置数据)中开启对位置数据的访问

    【讨论】:

      【解决方案3】:

      关注 Google 文档:

      1.将这些行添加到Manifest

      <uses-permission android:name="android.permission.BLUETOOTH"/>
      <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
      
      <!-- Required only if your app isn't using the Device Companion Manager. -->
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      

      2.设置ble前检查权限ACCESS_FINE_LOCATION

      private void checkPermission() {
          String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
          if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) {
              requestPermissions(permissions, REQUEST_ENABLE_LOCATION);
          }
      }
      
      1. 如果一切都不起作用,请在设置 > 应用管理 > 您的应用 > 权限中检查您的应用权限。允许位置访问。完成。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多