E/BluetoothGatt:android.os.DeadObjectException

Android 当我连接成功之后,关闭蓝牙再打开蓝牙就出现这种情况。可能是蓝牙重新打开后,服务还未绑定完成,就去调用connect连接,导致的。某个对象已经不存在引起的异常。


E/BluetoothGatt: android.os.DeadObjectException
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(BinderProxy.java:532)
        at android.bluetooth.IBluetoothGatt$Stub$Proxy.registerForNotification(IBluetoothGatt.java:2276)

img

网上的方法:在application标签里面添加一句android:hardwareAccelerated="false"(禁用硬件加速)对我来说没有用,应该怎么解决呢?

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
这个问题可能是因为你的应用程序没有正确地断开与蓝牙设备的连接导致的。尝试在你的代码中添加正确的断开连接代码,以确保在重新打开蓝牙之前已经断开了蓝牙连接。

另外,如果你使用的是Android 6.0或更高版本,则需要在运行时请求蓝牙权限。确保你的应用程序已经正确地请求了必要的权限。你可以使用以下代码请求蓝牙权限:

private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_PERMISSION_COARSE_LOCATION = 1;

// ...

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

// Requests permission to access the device's location. Required for scanning
// for Bluetooth devices on Android 6.0 and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
            REQUEST_PERMISSION_COARSE_LOCATION);
}

如果这些方法不起作用,你可以尝试在蓝牙连接之前添加一个延迟来确保服务已经绑定完成。例如,使用以下代码将连接延迟3秒:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Connect to Bluetooth device
    }
}, 3000);

最后,你也可以尝试更新你的Android操作系统和蓝牙驱动程序,以确保它们是最新版本,可能会解决一些与蓝牙连接相关的问题。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢