蓝牙LE扫描有时找不到设备 [英] Bluetooth LE Scanning Sometimes Doesn't Find Devices

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

问题描述

我正在扫描Bluetooth LE设备并作为外围设备运行(在Moto G 2nd Gen上运行Android 6.0)

I am scanning for Bluetooth LE devices and running as a Peripheral (running Android 6.0 on a Moto G 2nd Gen)

我遇到的问题是,有时(似乎随机但经常),它将找不到我的任何其他外围设备,而其他时候,它仍然可以正常工作.

The problem I am having is that sometimes (randomly it seems but often) it will not find any of my other peripheral devices, the other times it works fine.

我有一台运行类似代码的iOS设备(扫描外围设备并充当外围设备),当Android扫描找不到iOS设备时,我的iOS发现充当外围设备的Android设备就很好了.因此,这似乎只是事物扫描方面的问题.

I have a companion iOS device running similar code (both scanning for peripherals and acting as a peripheral), and when the Android scanning can't find the iOS device, my iOS finds the Android device acting as a peripheral just fine. So it seems only to be a problem with the scanning side of things.

不仅找不到我的同伴iOS设备,而且找不到任何蓝牙设备.当它正常工作时,它会找到我的同伴iOS设备以及许多其他设备.

It's not only just not finding my companion iOS device, but doesn't find any Bluetooth devices. When it works, it finds my companion iOS device as well as a bunch of other devices.

我在有和没有ScanFilters的情况下都尝试过,并且遇到相同的问题. 我正在使用最低SDK为23的SDK 26进行构建.

I have tried it with and without ScanFilters, and get the same issue. I am building against SDK 26 with a minimum SDK of 23.

我正在设置所需的权限,因为有时它会起作用.

I am setting the permissions that are needed, as it sometimes works.

以下相关代码:

private void startScanning() {
    mHandler = new Handler(mContext.getMainLooper());

    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            ScanSettings settings = new ScanSettings.Builder()
                                        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                                        .setReportDelay(0)
                                        .build();

            mBluetoothLeScanner.startScan(null, settings, mScanCallback);
        }
    }, 1000);
}

private ScanCallback mScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);

        if( result == null || result.getDevice() == null )
            return;

        Log.e("myTest", "Found Device");

        BluetoothDevice device = result.getDevice();
        final String deviceAddress = device.getAddress();

        List<ParcelUuid> parcel = result.getScanRecord().getServiceUuids();

        if (parcel != null) {
            String parcelUUID = parcel.toString().substring(1,37);

            if (parcelUUID.equalsIgnoreCase(mContext.getString(R.string.service_uuid))) {
                final BluetoothDevice bleDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress);

                if (!seenPeripherals.contains(deviceAddress)) {
                    stopScanning();

                    mHandler = new Handler(mContext.getMainLooper());

                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("AppToApp", "Trying to connect to device " + deviceAddress);

                            mGatt = bleDevice.connectGatt(mContext, false, mGattCallback);
                        }
                    }, 1000);
                }
            }
        }   
    }
}

推荐答案

我遇到了同样的问题.这是因为Google策略已针对棉花糖API 23 和更高版本进行了更改,要使用BLE用户,需要打开 GPS .对于Google文档,请点击运行时权限" 链接.要解决此问题,您必须在电话的设置中启用位置",并在运行时间 的应用程序中请求位置权限.都需要完成这两项操作才能使扫描正常工作.

I face the same issue. This is because Google policy has been changed for Marshmallow API 23 and higher version, to use BLE user need to turn ON GPS. For Google Docs check this Permission @ Runtime link. To fix it you have to enable "Location" 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:

myActivity.requestPermissions(new String[]{Manifest.permission.ACCESS_COURSE_LOCATION}, yourPermissionRequestCode);

并实施:

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

myActivity中的

处理用户选择的任何内容.您还需要执行以下操作来打开设备的位置服务:

in myActivity 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);
myActivity.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
    }
}

中的

.您还可以通过以下操作手动打开位置服务:

in myActivity. You can also manually turn on location services by doing:

Enter phone settings -> Select "Apps" -> Select your app -> Select the "Permissions" option -> Switch the "Location" permission on.

一旦用户启用了权限并启动了位置服务,您就可以开始扫描外围设备了. 权限/开启位置服务后,它仍不会在您的onScanResults

这使公司可以看到您的位置并将您定向到他们想要的地方.

This allow companies to see your location and direct you to where they want.

在扫描设备时,一旦您连接到BLE设备,就可以关闭手机上的位置服务,并且您仍将保持与外围设备的连接.但是,一旦连接到固件(外围设备),您将无法再连接到任何新的外围设备,直到断开与配对设备的连接,并且所有其他扫描设备(移动设备)都无法在其扫描列表中看到外围设备名称(当用户如果外围设备已连接到任何其他移动设备,则该外围设备可搜索附近设备),因为一次只能将外围设备连接到一个设备. 对于BLE基本示例,您可以查看以下 Truiton Ble教程.我认为此代码段将解决您的问题.快乐的编码:)

while SCANNING for device, Once you are connected to the BLE device you can Turn Off the location service on your phone and you will still stay connected to your peripheral device. However, once you connected to a firmware (peripheral) you cannot then connect to any new peripheral until you disconnect the connection to the paired device, and all other scanning devices(mobile) cannot see the peripheral device name in their scanned list (when user search for near by peripheral device) when the peripheral is already connected to any other mobile device, because a peripheral can be connect to only 1 device at a time. For BLE basic sample you can check this Truiton Ble tutorial. This snippet will fix your Issue I think. Happy coding :)

这篇关于蓝牙LE扫描有时找不到设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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