From e03e06a47fcc0920e35b8f5f30289cdb90985e35 Mon Sep 17 00:00:00 2001 From: pw <329803553@qq.com> Date: Fri, 30 Nov 2018 20:54:21 +0800 Subject: [PATCH] fix the param-check of writeByBatch --- .../com/ficat/easyble/gatt/BleGattImpl.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java b/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java index 9426a4c..f8dbc40 100644 --- a/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java +++ b/easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java @@ -530,8 +530,19 @@ public void run() { @Override public void writeByBatch(BleDevice device, final String serviceUuid, final String writeUuid, - final byte[] writedData, final int lengthPerPackage, final BleWriteByBatchCallback callback) { + final byte[] writedData, int lengthPerPackage, final BleWriteByBatchCallback callback) { checkNotNull(callback, BleWriteByBatchCallback.class); + if (writedData == null || writedData.length == 0) { + callback.onFail(BleCallback.FAIL_OTHER, "writeData is null or no writing data", device); + return; + } + if (lengthPerPackage < 1 || lengthPerPackage > 509) { + callback.onFail(BleCallback.FAIL_OTHER, "lengthPerPackage is invalid, it must range from 1 to 509", device); + return; + } + if (lengthPerPackage > 20) { + Logger.w("lengthPerPackage is greater than the default length of per package, make sure MTU is greater than 23"); + } final List byteList = getBatchData(writedData, lengthPerPackage); if (byteList.size() > 0) { BleWriteCallback writeCallback = new BleWriteCallback() { @@ -556,12 +567,6 @@ public void onFail(int failCode, String info, BleDevice device) { private List getBatchData(byte[] data, int lengthPerPackage) { List batchDatas = new ArrayList<>(); - if (data == null || data.length == 0) { - return batchDatas; - } - if (lengthPerPackage < 0) { - lengthPerPackage = 20; - } int packageNummber = (data.length % lengthPerPackage == 0) ? data.length / lengthPerPackage : data.length / lengthPerPackage + 1; for (int i = 1; i <= packageNummber; i++) {