纽轱辘小铭 2023-03-28 12:09 采纳率: 88.9%
浏览 16
已结题

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"(禁用硬件加速)对我来说没有用,应该怎么解决呢?

  • 写回答

1条回答 默认 最新

  • Leodong. 2023-03-28 12:28
    关注

    该回答通过自己思路及引用到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操作系统和蓝牙驱动程序,以确保它们是最新版本,可能会解决一些与蓝牙连接相关的问题。


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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 已采纳回答 3月29日
  • 创建了问题 3月28日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。