[英]Why is my bluetoothAdapter crashing the application
Im trying to learn how to just enable and disable Bluetooth through my application.我试图学习如何通过我的应用程序启用和禁用蓝牙。 So I have been writing a small code for this.所以我一直在为此编写一个小代码。 When I run the application on a simulator the code works and it enables and disables the Bluetooth.当我在模拟器上运行应用程序时,代码可以工作,它会启用和禁用蓝牙。 When I run it on my phone it crashing.当我在手机上运行它时,它崩溃了。
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
With the new update on Android 12 the bluetooth permissions are separated in multiple pemrissions.随着 Android 12 的新更新,蓝牙权限分为多个权限。 In your manifest you declared BLUETOOTH_CONNECT, so first you need to request BLUETOOTH_CONNECT permission in your activity or fragment.在您的清单中,您声明了 BLUETOOTH_CONNECT,因此首先您需要在您的活动或片段中请求 BLUETOOTH_CONNECT 权限。
The BLUETOOTH_ADVERTISE, BLUETOOTH_CONNECT, and BLUETOOTH_SCAN permissions are runtime permissions. BLUETOOTH_ADVERTISE、BLUETOOTH_CONNECT 和 BLUETOOTH_SCAN 权限是运行时权限。 Therefore, you must explicitly request user approval in your app before you can look for Bluetooth devices.因此,您必须在您的应用程序中明确请求用户批准,然后才能查找蓝牙设备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.