我正在尝试学习如何通过我的应用程序启用和禁用蓝牙。所以我一直在为此编写一个小代码。当我在模拟器上运行该应用程序时,代码可以工作并启用和禁用蓝牙。当我在手机上运行它时,它崩溃了。
private void initBluetooth() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(context, "No Bluetooth found", Toast.LENGTH_SHORT).show();
finish();
}
}
private void enableDisableBluetooth() {
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
} else {
bluetoothAdapter.enable();
}
}
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
2022-05-24 12:45:12.536 32020-32020/com.example.bluetoothtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bluetoothtest, PID: 32020
java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10467, packageName = com.example.bluetoothtest, attributionTag = null, token = android.os.BinderProxy@ee66382, next = null }: disable
at android.os.Parcel.createExceptionOrNull(Parcel.java:2437)
at android.os.Parcel.createException(Parcel.java:2421)
at android.os.Parcel.readException(Parcel.java:2404)
at android.os.Parcel.readException(Parcel.java:2346)
at android.bluetooth.IBluetoothManager$Stub$Proxy.disable(IBluetoothManager.java:1046)
at android.bluetooth.BluetoothAdapter.disable(BluetoothAdapter.java:2270)
at com.example.bluetoothtest.MainActivity.enableBluetooth(MainActivity.java:108)
at com.example.bluetoothtest.MainActivity.onOptionsItemSelected(MainActivity.java:55)
at android.app.Activity.onMenuItemSelected(Activity.java:4401)
at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:352)
at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:264)
at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
at androidx.appcompat.app.AppCompatDelegateImpl.onMenuItemSelected(AppCompatDelegateImpl.java:1185)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:975)
at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
at android.view.View.performClick(View.java:7792)
at android.widget.TextView.performClick(TextView.java:16045)
at android.view.View.performClickInternal(View.java:7769)
at android.view.View.access$3800(View.java:910)
at android.view.View$PerformClick.run(View.java:30184)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8641)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.BluetoothManagerService.checkPermissionForDataDelivery(BluetoothManagerService.java:5019)
at com.android.server.BluetoothManagerService.checkConnectPermissionForDataDelivery(BluetoothManagerService.java:5037)
at com.android.server.BluetoothManagerService.checkBluetoothPermissions(BluetoothManagerService.java:1503)
at com.android.server.BluetoothManagerService.disable(BluetoothManagerService.java:1930)
at android.bluetooth.IBluetoothManager$Stub.onTransact(IBluetoothManager.java:473)
2022-05-24 12:45:12.584 32020-32020/com.example.bluetoothtest I/Process: Sending signal. PID: 32020 SIG: 9
随着 Android 12 的新更新,蓝牙权限被分为多个权限。在您的清单中,您声明了 BLUETOOTH_CONNECT,因此首先您需要在您的 Activity 或片段中请求 BLUETOOTH_CONNECT 权限。
BLUETOOTH_ADVERTISE、BLUETOOTH_CONNECT 和 BLUETOOTH_SCAN 权限是运行时权限。因此,您必须在应用程序中明确请求用户批准,然后才能查找蓝牙设备。
Android 12及以上版本需要指定运行时的权限,所以需要添加类似;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, enable_bt);
}
}