Skip to content

Commit

Permalink
fix the param-check of writeByBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ficat committed Nov 30, 2018
1 parent 8f5bf5a commit e03e06a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions easyble/src/main/java/com/ficat/easyble/gatt/BleGattImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte[]> byteList = getBatchData(writedData, lengthPerPackage);
if (byteList.size() > 0) {
BleWriteCallback writeCallback = new BleWriteCallback() {
Expand All @@ -556,12 +567,6 @@ public void onFail(int failCode, String info, BleDevice device) {

private List<byte[]> getBatchData(byte[] data, int lengthPerPackage) {
List<byte[]> 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++) {
Expand Down

0 comments on commit e03e06a

Please sign in to comment.