尝试检测BLE设备时未调用onLeScan函数

时间:2016-04-27 11:45:21

标签: android android-service bluetooth-lowenergy

我正在尝试检测服务上附近的蓝牙低功耗设备。 启动服务后,将调用 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" )

3 个答案:

答案 0 :(得分:0)

我有类似的问题。 相同的代码,一个平板电脑不起作用,但其他平板 我没有解决方案但是当我重置我的平板电脑并重新安装apk时,它可以再次正常工作。

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

答案 1 :(得分:0)

尝试在应用设置(设置->应用->您的应用->规则->位置数据)中打开对位置数据的访问权限

答案 2 :(得分:0)

关注Google文档:

1。将这些行添加到清单

<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. 如果所有方法都不起作用,请在“设置”>“应用程序管理”>“您的应用程序”>“权限”中检查您的应用程序权限。允许位置访问。完成。