将目标 SDK 从28更改为29会导致BLE扫描不起作用。

huangapple go评论79阅读模式
英文:

Changing target SDK from 28 to 29 results in BLE scan not working

问题

我正在开发一个需要连接到BLE设备的Android应用程序。
应用程序按预期工作,但是现在当我将目标SDK更改为29(之前是API 28)时,应用程序不再获取任何扫描结果。
在API级别28上,当应用程序启动时,我检查权限。如果没有被授予,我会请求权限,然后像往常一样开始扫描BLE设备。然后我会获得包括周围BLE设备的扫描结果。我通过打印日志消息确认这一点。
现在当我将目标SDK更改为29(这是Play Store更新应用程序所需的)时,应用程序停止获取扫描结果。日志中也没有任何消息。

在API 28上工作但在API 29上不工作的设备:搭载Android 10的三星S10。

我在清单中添加的权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<!-- 如果您的应用程序针对Android 9或更低版本,请改为声明ACCESS_COARSE_LOCATION。 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

我在运行时从用户那里请求的权限:

if(ContextCompat.checkSelfPermission(this,
    Manifest.permission.ACCESS_COARSE_LOCATION)
    != PackageManager.PERMISSION_GRANTED){
    // 这里是向用户请求权限的代码。
    ActivityCompat.requestPermissions(MainActivity.this,
        new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
        REQUEST_ENABLE_LOCATION);
}

我处理扫描结果的代码:

public void onScanResult(int callbackType, ScanResult result) {
    BluetoothDevice device = result.getDevice();
    String deviceName = device.getName();
    if (deviceName != null) {
        if (checkdevicename(deviceName)){

            Log.d(TAG, "Connect:" + deviceName);
            isConnected = true;
            mDevice = device;

            // 连接到设备,建立GATT连接。
            device.connectGatt(ma, false, mGattCallback);
            stopScan();

        } else {
            Log.d(TAG, "DEVICE NAME =====>" + deviceName);

        }
    }
}
英文:

I am working on Android app which need to be connected to BLE devices.
The app works as expected but now when I changes the target SDK to 29 (which was previously API 28) the app stops getting any scan results.
At API level 28 when the app starts I check the permission. If not granted I ask for it and then start scanning as usual for BLE devices. Then I get the scan results which include the BLE devices around me. I confirm that by printing log messages.
Now when I changed the target SDK to 29 (which is required by play store to update the app) the app stops getting scan results. I am not getting messages in the logs either.

Device which is working with API 28 and not with API 29: Samsung S10 with Android 10.

Permissions I added in the manifest

&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.BLUETOOTH_ADMIN&quot; /&gt;

&lt;!-- If your app targets Android 9 or lower, you can declare
     ACCESS_COARSE_LOCATION instead. --&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_BACKGROUND_LOCATION&quot; /&gt;

Permission I request from user at runtime:

if(ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED){
        //here code for asking permission from user.
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },
                REQUEST_ENABLE_LOCATION);
    }

The code where I interact with scan results:

public void onScanResult(int callbackType, ScanResult result) {
    BluetoothDevice device = result.getDevice();
    String deviceName = device.getName();
         if (deviceName != null) {
                    if (checkdevicename(deviceName)){

                        Log.d(TAG, &quot;Connect:&quot; + deviceName);
                        isConnected = true;
                        mDevice = device;

//                             // connecting to device. establishing gatt connection.
                        device.connectGatt(ma,false,mGattCallback);
                        stopScan();

                    } else {
                        Log.d(TAG,&quot;DEVICE NAME =====&gt;&quot;+deviceName);

                    }
//         super.onScanResult(callbackType, result);
}}

答案1

得分: 4

似乎在SDK 29中,隐私设置更加严格:https://developer.android.com/about/versions/10/privacy/changes,用于使用BLE(低功耗蓝牙)。
因此,基于这个知识,您可能需要更新您的权限,请参阅https://stackoverflow.com/questions/25884129/scanning-of-bluetooth-low-energy-fails。

简而言之(根据29版本的隐私更改),您需要使用ACCESS_FINE_LOCATION权限来使用某些蓝牙功能(以及WiFi和一些其他功能)。

一些电话、蓝牙、Wi-Fi API需要FINE位置权限。
如果您的应用目标是Android 10或更高版本,则必须具备ACCESS_FINE_LOCATION权限,以便使用Wi-Fi、Wi-Fi Aware或蓝牙API中的多种方法。

英文:

It seems the privacy settings got more strict in SDK 29: https://developer.android.com/about/versions/10/privacy/changes for using BLE (Bluetooth Low Energy).
So with that knowledge you might need to update your permissions, see https://stackoverflow.com/questions/25884129/scanning-of-bluetooth-low-energy-fails as well.

In a nutshell (according to the privacy version changes in 29) you need to use ACCESS_FINE_LOCATION permission to use certain Bluetooth functions (and also WiFi and some more).

> Some telephony, Bluetooth, Wi-Fi APIs require FINE location permission
If your app targets Android 10 or higher, it must have the ACCESS_FINE_LOCATION permission in order to use several methods within the Wi-Fi, Wi-Fi Aware, or Bluetooth APIs.

huangapple
  • 本文由 发表于 2020年9月17日 15:37:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定