蓝牙低耗能startScan在Android 6.0没有找到设备 [英] Bluetooth Low Energy startScan on Android 6.0 does not find devices

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

问题描述

我正在开发具有低功耗蓝牙应用程序中使用的Nexus 5,它的工作的棒棒糖,现在它是不工作的棉花糖。 我将在清单和运行活动中的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.

这是ScanFilters列表:

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_COURSE_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:

进入手机设置 - &GT;选择位置 - &GT;然后打开它

这应该是这样的,在手机设置:

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

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

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