为什么我的BLE扫描仪没有发现任何设备,虽然我已经申请了位置请求?

问题描述 投票:1回答:1

我现在有点新,现在我正在尝试设置一个基本的BLE扫描仪应用程序,但该应用程序没有发现我的BLE模块(即时通讯使用Android开发页面上的基本代码)。

我试着在网上查找解决方案,发现问题是位置要求。然而,即使我申请此请求后,我仍然无法找到我的BLE模块

this is the location request 
@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_COARSE_LOCATION: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    scanLeDevice(true);
                } else {
                    // Alert the user that this application requires the location permission to perform the scan.
                }
            }
        }
    }

这是我的扫描码

    private void scanLeDevice(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                    invalidateOptionsMenu();
                }
            }, SCAN_PERIOD);

            mScanning = true;
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
        invalidateOptionsMenu();
    }
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mLeDeviceListAdapter.addDevice(device);
                    mLeDeviceListAdapter.notifyDataSetChanged();
                }
            });
        }
    };
android bluetooth-lowenergy
1个回答
0
投票

您是否还在AndroidManifest.xml中添加了权限?它看起来像

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

同时,您需要在Application中的某个位置设置您的单元格。

找到您自己的Android应用程序,然后授予它位置权限。

希望它有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.