【问题标题】:Android 6 bluetooth安卓6蓝牙
【发布时间】:2015-10-15 07:08:10
【问题描述】:

我升级到 Android 6,但我使用蓝牙的应用程序无法使用这个新的 API 版本。 Play Store 上的应用程序也存在同样的问题:Bluetooth spp tools pro(用于查看蓝牙是否正常工作的好应用程序)无法发现设备。

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

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

【问题讨论】:

  • 您在哪个设备上遇到此问题?哪个关系?我在 Nexus 5 上做

标签: android bluetooth


【解决方案1】:

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

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

  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 上获得用户答案

@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 进行调用

【讨论】:

【解决方案2】:

我花了一些时间调查这个问题。
在 Android 错误跟踪器上创建错误报告 here
问题是系统不会将BluetoothDevice.ACTION_FOUND 意图转发到已注册的BroadcastReceiver。 Logcat 显示这样的行:

10-16 07:34:09.147      786-802/? W/BroadcastQueue﹕ Permission Denial: receiving Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } to ProcessRecord{5ce2d92 21736:com.example.mvl.bluetoothtest/u0a74} (pid=21736, uid=10074) requires android.permission.ACCESS_COARSE_LOCATION due to sender com.android.bluetooth (uid 1002)    

对我来说,应用程序需要android.permission.ACCESS_COARSE_LOCATION 权限才能接收此意图。我个人不明白为什么我需要该权限才能获得蓝牙设备。
因此,如果您将此权限添加到您的 Manifest,那么它应该还有一个前提条件 - 您必须设置目标 SDK 并使用 SDK 不更高的版本进行编译,然后是 22。

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

【讨论】:

  • ACCESS_COARSE_LOCATION 是必需的,因为蓝牙,例如 Wi-Fi 和网络(严格来说不是 GPS 设备)也可以帮助您使用 BT 信标来确定您的位置。因此,授予位置权限也是预先请求的。
【解决方案3】:

查看GattService.java中的源代码,你会发现方法onScanResult中有一些代码cmets

// Do no report if location mode is OFF or the client has no location permission
// PEERS_MAC_ADDRESS permission holders always get results
if (hasScanResultPermission(client) && matchesFilters(client, result)) {
    try {
        ScanSettings settings = client.settings;
        if ((settings.getCallbackType() &
                                ScanSettings.CALLBACK_TYPE_ALL_MATCHES) != 0) {
            app.callback.onScanResult(result);
        }
    } catch (RemoteException e) {
        Log.e(TAG, "Exception: " + e);
        mClientMap.remove(client.clientIf);
        mScanManager.stopScan(client);
    }
}

这阐明了获取蓝牙 LE 广告报告所需的条件。

【讨论】:

    猜你喜欢
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2011-07-20
    • 2012-12-16
    • 2014-08-31
    • 2011-12-18
    相关资源
    最近更新 更多