首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android扫描程序权限异常

Android扫描程序权限异常
EN

Stack Overflow用户
提问于 2018-06-12 18:22:43
回答 1查看 1.8K关注 0票数 1

我很难从BLE扫描仪得到扫描结果。我的AndroidManifest.xml中有适当的权限(AndroidManifest.xml),详见下文,但得到一个异常,表明我需要所拥有的权限。不足为奇的是,扫描程序的回调从未被调用。

代码语言:javascript
运行
复制
W/Binder: Caught a RuntimeException from the binder stub implementation.
          java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results.  I'm not sure why the exception is thrown but I certianly DON'T get scan call backs.


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


ScanCallback btScanCallback = new ScanCallback()
{
    @Override
    public void onScanResult(int callbackType, final ScanResult result)
    {
        BluetoothDevice btDevice = result.getDevice();
        Log.d(LOGTAG, "Found BLE device: " + btDevice.getName());


        // Remove the device from the scanner select view if its there already
        for( int i=0; i<btDeviceNameList.size(); i++)
        {
            String aDevice = btDeviceNameList.get(i);

            if(aDevice.equalsIgnoreCase(btDevice.getName()))
            {
                btDeviceNameList.remove(i);
                btDeviceList.remove(i);
                break;
            }
       }

        // Add the device to the scanner select view
        btDeviceList.add(btDevice);
        btDeviceNameList.add(btDevice.getName());

        btListAdapter.notifyDataSetChanged();
    }


    @Override
    public void onScanFailed(int errorCode)
    {
        Log.e(LOGTAG, "BT scan error: " + errorCode);
    }
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-14 09:02:35

我遇到过你所面临的同样的情况。如果您使用的是Android6.0或更高的。必须在运行时请求位置权限。在获得蓝牙适配器之后,让我们按照下面的代码行请求位置权限。当您的应用程序运行时,将显示一个对话框来询问您是否同意共享您的位置。

代码语言:javascript
运行
复制
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50823501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档