造成以下错误的操作主要是: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;
            }
        }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐