Android Ble蓝牙出现以下错误:E/BluetoothGatt:android.os.DeadObjectException

造成以下错误的操作主要是:Ble蓝牙先连接成功,连接成功之后断开蓝牙,蓝牙重新打开后,服务还未绑定完成,就去调用connect连接,导致的。大概意思就是某个对象已经不存在引起的异常。

 网上的解决方法方法:在application标签里面添加一句(对我来说没有用)

android:hardwareAccelerated="false"(禁用硬件加速)

我的解决方法:

首先检测到蓝牙断开连接的状态:

else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
                    // 连接断开
                    Log.e(TAG,"onConnectionStateChange fail-->(连接状态更改)" + status);
}

然后是断开服务,其次是关闭服务

//这两个方法都没有参数,使用连接蓝牙设备获取到的BluetoothGatt对象直接调用就可以
	mBluetoothGatt.disconnect();
	mBluetoothGatt.close();

连接前先进行蓝牙扫描之后可以解决重连问题。 根据测试后达到了断线重连的效果,连接后需及时关闭扫描避免资源的浪费。

  if(mBluetoothGatt != null ){
                        mBluetoothGatt.disconnect(); //先去断开之前未正常断开的连接,解决连接133的问题
                        mBluetoothGatt.close(); //释放gatt服务
                        mBluetoothGatt = null;
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Intent intent = new Intent();
                    BluetoothDevice bluetoothDevice = intent.getParcelableExtra(EXTRAS_DEVICE);
                    //连接前先进行扫描
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                        mBluetoothGatt = bluetoothDevice.connectGatt(TestService2.this,
                                true, gattCallback, TRANSPORT_LE);
                    } else {//安卓6.0及以上的方案
                        mBluetoothGatt = bluetoothDevice.connectGatt(TestService2.this,
                                true, gattCallback);
                    }

完整代码:

     /**
         * 断开或连接 状态发生变化时调用
         * */
        @SuppressLint({"MissingPermission", "InlinedApi"})
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                //连接成功
                if (newState == BluetoothGatt.STATE_CONNECTED) {
                    Log.e(TAG, "连接成功=====================");
                    //发现服务
                    gatt.discoverServices();
                }else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
                    // 连接断开
                    Log.e(TAG,"onConnectionStateChange fail-->(连接状态更改)" + status);
                    mBluetoothGatt.close();
                    mBluetoothGatt.disconnect();

                    if(mBluetoothGatt != null ){
                        mBluetoothGatt.disconnect(); //先去断开之前未正常断开的连接,解决连接133的问题
                        mBluetoothGatt.close(); //释放gatt服务
                        mBluetoothGatt = null;
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Intent intent = new Intent();
                    BluetoothDevice bluetoothDevice = intent.getParcelableExtra(EXTRAS_DEVICE);
                    //连接前先进行扫描
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                        mBluetoothGatt = bluetoothDevice.connectGatt(TestService2.this,
                                true, gattCallback, TRANSPORT_LE);
                    } else {//安卓6.0及以上的方案
                        mBluetoothGatt = bluetoothDevice.connectGatt(TestService2.this,
                                true, gattCallback);
                    }
                }
            } else {
                //连接失败
                Log.e(TAG, "失败===================" + status);
                mBluetoothGatt.close();
                mBluetoothGatt.disconnect();
                isConnecting = false;
            }
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纽轱辘小铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值