Android 6.0 上的低功耗蓝牙 startScan 找不到设备 [英] Bluetooth Low Energy startScan on Android 6.0 does not find devices

查看:65
本文介绍了Android 6.0 上的低功耗蓝牙 startScan 找不到设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nexus 5 开发具有低功耗蓝牙的应用程序.它适用于 Lollipop,但现在不适用于 Marshmallow.我在清单和活动的运行时设置了 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION 权限.

I'm developing an application with Bluetooth Low Energy using Nexus 5. It worked on Lollipop and now it is not working on Marshmallow. I set the ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions in the manifest and on runTime in the Activity.

这是扫描过滤器的列表:

This is the list of ScanFilters:

mScanFilterTest = new ScanFilter.Builder().build();
mScanFilter = new ArrayList<ScanFilter>();
mScanFilter.add(mScanFilterTest);

这些是设置:

mScanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER).setReportDelay(0)
                .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).build();

这些是我的回调:

 mBLEScan = new ScanCallback() {
     @Override
     public void onScanResult(int callbackType, ScanResult result) {
         super.onScanResult(callbackType, result);
         Log.i(TAG, "******************************************");
         Log.i(TAG, "The scan result " + result);
         Log.i(TAG, "------------------------------------------");
         }
     };

这是我的电话:

mBluetoothLeScanner.startScan(mScanFilter, mScanSettings, mBLEScan);

它开始扫描但没有找到任何设备.请帮帮我!!!!

It starts the scan but does not find any device. Please help me!!!!

推荐答案

我遇到了同样的问题.要修复它,您必须在手机设置中启用位置"(GPS)以及在运行时在应用程序中请求位置权限.两者都需要完成才能使扫描正常工作.

I struggled with the same issue. To fix it you have to enable "Location" (GPS) in the settings of the phone as well as request location permission in the app at runtime. Both need to be done for scanning to work properly.

要请求位置权限,请在对话框或类似内容中输入以下内容:

To request the location permission put the following in a dialog or the likes:

yourActivity.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, yourPermissionRequestCode);

并实施:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
    if(requestCode == yourPermissionRequestCode)
    {
        ... //Do something based on grantResults
    }
}

yourActivity 中并处理用户选择的任何内容.您还需要执行以下操作才能开启设备的位置服务:

in yourActivity and handle whatever the user selects. You also need to do the following to turn on your device's location services:

Intent enableLocationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
yourActivity.startActivityForResult(enableLocationIntent, yourServiceRequestCode);

您可以通过执行以下操作来检查用户是否打开了位置服务:

You can check if the user turned on the location services by implementing:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == yourServiceRequestCode)
    {
        ...//Do whatever you need to
    }
}

yourActivity 中.您还可以通过以下方式手动开启定位 (GPS) 服务:

in yourActivity. You can also manually turn on location (GPS) services by doing:

进入手机设置->选择位置"->然后开启

它在电话设置中应该是这样的:

It should look like this in the Phone settings:

或者像这样在快速设置下拉菜单中:

Or like this in the quick-settings drop down:

一旦用户启用了权限并启动了定位服务,那么您应该开始扫描.我注意到,如果您在启用权限/打开定位服务时已经在扫描,它会仍然没有在你的 onScanResults

Once the user has enabled the permission and started location services then you should start scanning. I've noticed that if you are already scanning while you enable the permission/turn on the location service it will still not put anything in your onScanResults

我不确定这是否是 iBeacons/蓝牙广告的错误或功能"(注意:广告是销售产品而不是技术蓝牙广告)以允许公司查看您的位置并将您定向到他们的位置想要.

I'm not sure if this is a bug or a "feature" for iBeacons/Bluetooth advertising (NOTE: advertising as in selling products not the technical Bluetooth advertising) to allow companies to see your location and direct you to where they want.

希望这能解决您的问题!

Hope this fixes your problem!

编辑我想补充一点:你只需要这个来扫描.连接到 BLE 设备后,您可以关闭手机上的定位服务,您仍将连接到您的设备.但是,您随后无法发现或连接到任何新设备,并且所有广告设备都将从 onScanResults

EDIT I meant to add: you only need this for SCANNING. Once you are connected to the BLE device you can shut off the location service on your phone and you will still be connected to your devices. However, you cannot then discover or connect to any new devices and all advertising devices will drop from the onScanResults

这篇关于Android 6.0 上的低功耗蓝牙 startScan 找不到设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆