安卓 6 蓝牙 [英] Android 6 bluetooth

查看:15
本文介绍了安卓 6 蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我升级到 Android 6 并且我使用蓝牙的应用程序无法使用这个新的 API 版本.Play 商店上的应用程序也有同样的问题:Bluetooth spp tools pro(查看蓝牙是否正常工作的好应用程序),它没有发现设备.

I upgraded to Android 6 and my applications who use Bluetooth doesn't work with this new API version. It's the same problem with application on Play Store: Bluetooth spp tools pro (good application to view if bluetooth works) which doesn't discovery of devices.

问题似乎出在蓝牙发现中:

The problem seems to be in Bluetooth discovery:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery()
Log.i("BLUETOOTH", String.valueOf(mBluetoothAdapter.isDiscovering())); // Return false

我的应用程序在 Android 4/5 上运行良好,我遵循:http://developer.android.com/guide/topics/connectivity/bluetooth.html

My applications work well with Android 4/5 and I followed : http://developer.android.com/guide/topics/connectivity/bluetooth.html

推荐答案

针对 Android 6.0,仅在清单中包含权限是不够的.您必须明确地询问用户每个被视为危险"的权限.BluetoothDevice.ACTION_FOUND 需要 BLUETOOTH 和 ACCESS_COARSE_LOCATION 权限http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_FOUND

Staring with Android 6.0 it is not enough to include permissions on manifest. You have to ask the user explicitly about each permission that is considered "dangerous". BluetoothDevice.ACTION_FOUND requires BLUETOOTH and ACCESS_COARSE_LOCATION permissions http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#ACTION_FOUND

ACCESS_COARSE_LOCATIONhttp://developer.android.com/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION是一个危险"的权限,因此您必须在进行实际发现之前使用 requestPermission 请求它.

The ACCESS_COARSE_LOCATION http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION is a "dangerous" permission and therefore you have to ask for it using requestPermission before doing actual discovery.

  public void doDiscovery() {
    int hasPermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
    if (hasPermission == PackageManager.PERMISSION_GRANTED) {
        continueDoDiscovery();
        return;
    }

    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{
                    android.Manifest.permission.ACCESS_COARSE_LOCATION},
            REQUEST_COARSE_LOCATION_PERMISSIONS);
}

然后你会在 onRequestPermissionsResult 上得到用户的回答

then on you will get the user answer on onRequestPermissionsResult

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
       case REQUEST_COARSE_LOCATION_PERMISSIONS: {
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                continueDoDiscovery();
            } else {
                Toast.makeText(this,
                        getResources().getString(R.string.permission_failure),
                        Toast.LENGTH_LONG).show();
                cancelOperation();
            }
            return;
        }
    }
}

要使用以前版本的 android,您应该使用兼容性库并使用 ActivityCompat 进行调用

To work with previous versions of android you should use compatibility libraries and make the calls using ActivityCompat

这篇关于安卓 6 蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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