Jasonchenlijian / FastBle

Android Bluetooth Low Energy (BLE) Fast Development Framework. It uses simple ways to filter, scan, connect, read ,write, notify, readRssi, setMTU, and multiConnection.
Apache License 2.0
5.29k stars 1.22k forks source link

如果设置了sendNextWhenLastSuccess=true,主动断开连接后还会一直发送 #519

Open DM-Li-Lee opened 2 years ago

DM-Li-Lee commented 2 years ago

如果我需要发送很大的数据,数据分包发送,这时候,我主动断开设备的连接,按我的理解,应该是不在发送数据的,但是由于在 BleManager.getInstance().write(mBleDevice, UUID_SERVICE, UUID_WRITE, buffer, true, true, 30, callback); 设置了sendNextWhenLastSuccess=true,导致在SplitWriter 走到发送失败 onWriteFailure回调方法中,mHandler一直在继续发送数据

 @Override
                            public void onWriteFailure(BleException exception) {
                                if (mCallback != null) {
                                    mCallback.onWriteFailure(new OtherException("exception occur while writing: " + exception.getDescription()));
                                }
                                if (mSendNextWhenLastSuccess) {
                                    Message message = mHandler.obtainMessage(BleMsg.MSG_SPLIT_WRITE_NEXT);
                                    mHandler.sendMessageDelayed(message, mIntervalBetweenTwoPackage);
                                }
                            }
DM-Li-Lee commented 2 years ago

我按照自己的需求,在发送失败后,加了个判断,如果当前为连接状态才让它继续发

@Override
                            public void onWriteFailure(BleException exception) {
                                if (mCallback != null) {
                                    mCallback.onWriteFailure(new OtherException("exception occur while writing: " + exception.getDescription()));
                                }
                                int connectState = BleManager.getInstance().getConnectState(mBleBluetooth.getDevice());
                                if (mSendNextWhenLastSuccess
                                        && connectState == BluetoothProfile.STATE_CONNECTED) {
                                    Message message = mHandler.obtainMessage(BleMsg.MSG_SPLIT_WRITE_NEXT);
                                    mHandler.sendMessageDelayed(message, mIntervalBetweenTwoPackage);
                                }
                            }