首页 > 其他分享 >Android Ble蓝牙出现以下错误:E/BluetoothGatt:android.os.DeadObjectException

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

时间:2023-05-13 14:01:26浏览次数:37  
标签:bluetoothDevice 断开 DeadObjectException 蓝牙 BluetoothGatt os 连接 mBluetoothGatt

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

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

网上的解决方法方法:在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;
            }
        }


标签:bluetoothDevice,断开,DeadObjectException,蓝牙,BluetoothGatt,os,连接,mBluetoothGatt
From: https://blog.51cto.com/u_15887619/6273694

相关文章

  • Ubuntu系统下python模块graphviz运行报错:graphviz.backend.execute.ExecutableNotFoun
    代码中需要运行python模块graphviz,安装:pipinstallgraphviz 运行后报错:graphviz.backend.execute.ExecutableNotFound:failedtoexecutePosixPath('dot'),makesuretheGraphvizexecutablesareonyoursystems'PATH  报错的全部信息:Traceback(mostrecentc......
  • python中os.path模块常见使用 和 os创建文件夹
    1、下面列举一些常用的os.path模块函数:os.path模块是Python中用于处理文件路径的模块之一,它提供了一系列函数用于对文件路径进行操作,例如连接、拆分、标准化、比较等。os.path.join(path1[,path2[,...]]):将多个路径组合成一个完整路径。该函数根据操作系统的不同,自动使......
  • VCL 中的 Windows API 函数: BeginDeferWindowPos
    BeginDeferWindowPos和DeferWindowPos、EndDeferWindowPos是一组一起使用的函数,可对一组窗口的位置、大小、Z序等进行调整,在ExtCtrls单元有用到.下面先用常规方法实现对Panel1中的一组Button进行调整,然后再用上面三个函数重新实现.用BeginDeferWindowPos、Defer......
  • MacOS祖传bug
    问题:/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/stdlib.h:20:15:fatalerror:'stdlib.h'filenotfound#include_next<stdlib.h>^~~~~~~~~~1errorgenerated.error:command'/Librar......
  • PostgreSQL插件(1): pg_timeout及pg_timetable 及 若干FAQ(1)
           PostgreSQL插件(1):pg_timeout及pg_timetable及若干FAQ(1)PostgreSQL2023-05-1209:20发表于河北编者荐语:报考PG数据库专家上盘古云课堂以下文章来源于数据库杂记,作者SeanHe1、前言这次将简单介绍PG的两个插件,以及若干常见问题汇集,作......
  • 问题解决:TNS-12543: TNS:destination host unreachable
    环境:11.2.0.3ADG(db11g\db11gadg\db11gcas)在自己先前克隆后的环境互相tnsping报错。tnsping本机ok,tnsping其他机器均报错:[oracle@db11g~]$tnspingjingyuTNSPingUtilityforLinux:Version11.2.0.3.0-Productionon13-MAY-202308:09:11Copyright(c)1997,......
  • Nacos Client 源码分析(二)服务订阅与推送消息处理
    1.概述在上一篇文章《NacosClient源码分析(一)事件的发布与订阅》分析了NacosClient的发布订阅机制,但我们现在还不清楚NotifyCenter的publishEvent方法是怎么被调用的以及客户端向服务端订阅服务的具体流程。下面我们对继续分析Nacos的源码。2.服务订阅还是从NacosNamin......
  • Python爱彼迎Airbnb新用户体验数据XGBoost、随机森林预测
    全文链接:http://tecdat.cn/?p=32380原文出处:拓端数据部落公众号分析师:ZhuhuaHuang在多项用户数据中寻找与预测值相关的属性。查看各个特征的分布与特征之间的关联。分析用户数据,查看特定人群的使用习惯进行产品优化。最后选择合适的模型与参数来进行预测。解决方案任务/目标......
  • 阿里云 OSS 工具类
     importcom.aliyun.oss.OSS;importcom.aliyun.oss.OSSClientBuilder;importlombok.Data;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;importorg.springframework.web.multipart.Multi......
  • OSI与TCP/IP协议簇
    1、OSI七层模型通行-->协议-->协议分层分层思想:将复杂的问题简单化。同层使用相同的协议,下层为上层提供服务。1974年,ISO(国际标准组织)发布了OSI(OpenSystemInterconnection,开放式系统互联模型)参考模型:物理层、数据链路层、网络层、传输层、会话层、表示层、应用层。 OSI只......