From be09925566994dd7df2b4befd55892fff6bd5515 Mon Sep 17 00:00:00 2001 From: HET <553252628@qq.com> Date: Wed, 13 Dec 2017 19:51:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=86=E5=8C=85=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FAQ.md | 63 +++++++++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/FAQ.md b/FAQ.md index b6c0c49..cd7216c 100644 --- a/FAQ.md +++ b/FAQ.md @@ -30,58 +30,45 @@ 分包处理如下: ``` -//存储待发送的数据队列 -private Queue dataInfoQueue = new LinkedList<>(); - -private Handler handler = new Handler(){ - @Override - public void handleMessage(Message msg) { - super.handleMessage(msg); - } -}; - -private Runnable runnable = new Runnable() { - @Override - public void run() { - send(); - } -}; - //外部调用发送数据方法 -public void send(byte[] data) { +public void write(final BluetoothLeDevice bluetoothLeDevice, byte[] data) { if (dataInfoQueue != null) { dataInfoQueue.clear(); dataInfoQueue = splitPacketFor20Byte(data); - handler.post(runnable); + new Handler(Looper.getMainLooper()).post(new Runnable() { + @Override + public void run() { + send(bluetoothLeDevice); + } + }); } } -//实际发送数据过程 -private void send() { +//发送队列,提供一种简单的处理方式,实际项目场景需要根据需求优化 +private Queue dataInfoQueue = new LinkedList<>(); +private void send(final BluetoothLeDevice bluetoothLeDevice) { if (dataInfoQueue != null && !dataInfoQueue.isEmpty()) { - //检测到发送数据,直接发送 + DeviceMirror deviceMirror = mDeviceMirrorPool.getDeviceMirror(bluetoothLeDevice); + if (dataInfoQueue.peek() != null && deviceMirror != null) { + deviceMirror.writeData(dataInfoQueue.poll()); + } if (dataInfoQueue.peek() != null) { - ViseBluetooth.getInstance().writeCharacteristic(dataInfoQueue.poll(), new IBleCallback() { - - @Override - public void onSuccess(BluetoothGattCharacteristic bluetoothGattCharacteristic, int type) { - - } - + new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override - public void onFailure(BleException exception) { - + public void run() { + send(bluetoothLeDevice); } - }); - } - //检测还有数据,延时后继续发送,一般延时100毫秒左右 - if (dataInfoQueue.peek() != null) { - handler.postDelayed(runnable, 100); + }, 100); } } } -//数据分包处理 +/** + * 数据分包 + * + * @param data + * @return + */ private Queue splitPacketFor20Byte(byte[] data) { Queue dataInfoQueue = new LinkedList<>(); if (data != null) { @@ -99,7 +86,7 @@ private Queue splitPacketFor20Byte(byte[] data) { System.arraycopy(data, index, currentData, 0, 20); index += 20; } -            dataInfoQueue.offer(currentData); + dataInfoQueue.offer(currentData); } while (index < data.length); } return dataInfoQueue;