首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Android6.0的BluetoothLeScanner.startScan没有发现设备

使用Android6.0的BluetoothLeScanner.startScan没有发现设备
EN

Stack Overflow用户
提问于 2015-10-08 10:56:22
回答 6查看 22.9K关注 0票数 27

我正在尝试使用函数BluatoothLeScanner.startScan,而不是不推荐的BluetoothAdapter.startLeScan。昨天我把我的Nexus 5升级到了Android6.0,从那以后,我的应用程序就不再工作了。我首先添加了所需的首选项( ACCESS_COARSE_LOCATION ),如下所示,https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id。然后,我添加了如下所述的权限:https://developer.android.com/training/permissions/requesting.html。但到了最后,它似乎不起作用了,它没有把可再生的设备送回去。

这是我的密码:

清单

代码语言:javascript
复制
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.stm.sensitronapp">
          <uses-sdk android:maxSdkVersion="23"/>
          <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

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

DeviceScanActivity

代码语言:javascript
复制
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
...

if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED){
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_COARSE_LOCATION)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_COARSE);
        }
    }

// Device scan callback.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mScanCallback = new ScanCallback() {
                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                    super.onScanResult(callbackType, result);
                    mLeDeviceListAdapter.addDevice(result.getDevice());
                    mLeDeviceListAdapter.notifyDataSetChanged();
                }
            };
        }
    } 
}
final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
        mSwipeRefreshLayout.setRefreshing(true);
        mLeDeviceListAdapter.clear();
        mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
        if(ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED) {
                mBluetoothLeScanner.startScan(mScanCallback);
            }
        }

编辑:为了解决这个问题,我只打开了GPS。在这边请中以编程的方式实现它很容易。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-01-29 03:51:17

如果已授予权限,请尝试:打开GPS。

票数 27
EN

Stack Overflow用户

发布于 2015-10-10 16:42:49

您的应用程序是否提示在启动时获得位置许可?如果不是,则在其他地方处理代码,以便提示代码。

此外,您还可以检查这个以测试您的应用程序是否运行良好:

打开设置>应用程序> YourApplication >权限并启用位置,然后尝试扫描结果。

只有在清单上提供了ACCESS_COARSE_LOCATION时,才会在权限下列出位置。

票数 10
EN

Stack Overflow用户

发布于 2016-01-21 13:22:16

使用上面提供的解决方案是可行的,但是副作用是您必须为不需要它的东西打开位置服务。一个丑陋和令人不满意的工作是指定您的清单中的目标版本

android:targetSdkVersion="21“

它允许扫描我的Nexus 7,即使安装的版本是6.0.1。我不知道针对比安装版本更低版本的副作用是什么,但至少扫描工作。可能是无GPS设备的唯一解决方案(如果存在这种设备)。

谷歌应该为此受难。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33013818

复制
相关文章

相似问题

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