Android找不到任何BLE设备 [英] Android can't find any BLE devices

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

问题描述

这是他的问题.我为iOS开发了一个应用程序,用于控制BLE LED设备(以及其他一些东西).一切正常且流畅.现在我想为Android开发同一个应用程序,但是我在扫描BLE设备时已经失败了.我已经尝试了一些教程和示例代码,但是无论如何我都找不到任何设备.我在Moto G4 Play上工作.蓝牙可以使用,我可以在设置中配对设备,但无法与我尝试过的任何示例代码/教程一起使用. 例如这一个: https://github.com/kaviles/BLE_Tutorials

Here's he problem. I've developed an app for iOS to control an BLE LED device (and a few other things). Everything works fine and smooth. Now I wanted develop the same app for android and I already fail at the scanning of BLE devices. I have tried a few tutorials and sample codes but whatever I do I can't find any devices. I work on a Moto G4 Play. The bluetooth works and I can pair devices in the settings but it won't work with any sample code/tutorial I've tried. For example this one: https://github.com/kaviles/BLE_Tutorials

我按原样构建了此应用,但找不到任何东西.

I build this app as it is and it can't find anything.

因此,我从Playstore下载了BLE扫描仪,该扫描仪可以很好地工作并找到所有设备.

So I downloaded a BLE Scanner from the Playstore and that works fine to and finds all devices.

我知道没有任何示例代码很难说,但是我已经尝试了很多,而且我不确定我是否会错过一些基本的东西.

I know it's hard to say without any sample code but I've tried so many and I'm not sure if I miss something completely basic.

推荐答案

如注释中所述,如果targetSdk为23+或其他,则必须相应地设置权限.位置服务必须已打开.

As discussed in the comments, you have to set up the permissions accordingly if your targetSdk is 23+ or other. The location services must be on.

API 23+的清单权限:

Manifest permissions for API 23+:

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

要检查蓝牙权限:

    public boolean hasBlePermissions() {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
            return false;
        } else {
            return true;
        }
    }

通过:请求运行时权限

public void requestBlePermissions(final Activity activity, int requestCode) {
    ActivityCompat.requestPermissions(activity,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
            requestCode);
}

然后检查OnRequestPermissionResult的授予结果:

public boolean checkGrantResults(String[] permissions, int[] grantResults) {
    int granted = 0;

    if (grantResults.length > 0) {
        for(int i = 0; i < permissions.length ; i++) {
            String permission = permissions[i];
            if (permission.equals(Manifest.permission.ACCESS_FINE_LOCATION) ||
                    permission.equals(Manifest.permission.ACCESS_COARSE_LOCATION)) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    granted++;
                }
            }
        }
    } else { // if cancelled
        return false;
    }

    return granted == 2;
}

检查位置服务:

public boolean areLocationServicesEnabled(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    try {
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

这篇关于Android找不到任何BLE设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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