FastBle icon indicating copy to clipboard operation
FastBle copied to clipboard

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

Open DM-Li-Lee opened this issue 3 years ago • 1 comments

如果我需要发送很大的数据,数据分包发送,这时候,我主动断开设备的连接,按我的理解,应该是不在发送数据的,但是由于在 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 avatar Oct 09 '21 13:10 DM-Li-Lee

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

@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);
                                }
                            }

DM-Li-Lee avatar Oct 09 '21 13:10 DM-Li-Lee