Skip to content

Commit 3de9104

Browse files
Merge pull request #9 from douglasjunior/issue-5
Fixing "buffer dispatch" to prevent characters loss. Fixes #5
2 parents 6c6736b + 991af50 commit 3de9104

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

BluetoothLowEnergyLibrary/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ android {
2525

2626
dependencies {
2727
compile project(':BluetoothClassicLibrary')
28+
compile 'com.android.support:support-annotations:25.0.0'
2829
}

BluetoothLowEnergyLibrary/src/main/java/com/github/douglasjunior/bluetoothlowenergylibrary/BluetoothLeService.java

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ public class BluetoothLeService extends BluetoothService {
6565
private BluetoothGatt bluetoothGatt;
6666
private BluetoothGattCharacteristic characteristicRxTx;
6767

68-
private final byte[] buffer;
69-
private int i = 0;
70-
private byte[][] bytesBuffer;
71-
private int bufferIndex = 0;
68+
private final byte[] readBuffer;
69+
private int readBufferIndex = 0;
70+
private byte[][] writeBuffer;
71+
private int writeBufferIndex = 0;
7272

7373
private int maxTransferBytes = 20;
7474

7575
protected BluetoothLeService(BluetoothConfiguration config) {
7676
super(config);
7777
BluetoothManager btManager = (BluetoothManager) config.context.getSystemService(Context.BLUETOOTH_SERVICE);
7878
btAdapter = btManager.getAdapter();
79-
buffer = new byte[config.bufferSize];
79+
readBuffer = new byte[config.bufferSize];
8080
}
8181

8282
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {
@@ -252,31 +252,34 @@ public void run() {
252252
private void readData(byte[] data) {
253253
final byte byteDelimiter = (byte) mConfig.characterDelimiter;
254254
for (byte temp : data) {
255-
/*
256-
Verifica se está no momento de despachar o buffer
257-
*/
258-
if (temp == byteDelimiter || i >= buffer.length) {
259-
if (i > 0) {
260-
// Send the obtained bytes to the UI Activity
261-
if (onEventCallback != null) {
262-
final int finalI = i;
263-
runOnMainThread(new Runnable() {
264-
@Override
265-
public void run() {
266-
onEventCallback.onDataRead(buffer, finalI);
267-
}
268-
});
269-
}
270-
i = 0;
255+
256+
if (temp == byteDelimiter) {
257+
if (readBufferIndex > 0) {
258+
dispatchBuffer(readBuffer, readBufferIndex);
259+
readBufferIndex = 0;
271260
}
261+
continue;
272262
}
273-
/*
274-
Caso contrário, armazena o byte recebido
275-
*/
276-
else {
277-
buffer[i] = temp;
278-
i++;
263+
if (readBufferIndex == readBuffer.length - 1) {
264+
dispatchBuffer(readBuffer, readBufferIndex);
265+
readBufferIndex = 0;
279266
}
267+
readBuffer[readBufferIndex] = temp;
268+
readBufferIndex++;
269+
270+
}
271+
}
272+
273+
private void dispatchBuffer(byte[] buffer, int i) {
274+
final byte[] data = new byte[i];
275+
System.arraycopy(buffer, 0, data, 0, i);
276+
if (onEventCallback != null) {
277+
runOnMainThread(new Runnable() {
278+
@Override
279+
public void run() {
280+
onEventCallback.onDataRead(data, data.length);
281+
}
282+
});
280283
}
281284
}
282285

@@ -467,22 +470,22 @@ public void write(byte[] data) {
467470
Log.v(TAG, "write: " + data.length);
468471
if (bluetoothGatt != null && characteristicRxTx != null && mStatus == BluetoothStatus.CONNECTED) {
469472
if (data.length <= maxTransferBytes) {
470-
bufferIndex = 0;
471-
bytesBuffer = new byte[1][data.length];
472-
bytesBuffer[0] = data;
473+
writeBufferIndex = 0;
474+
writeBuffer = new byte[1][data.length];
475+
writeBuffer[0] = data;
473476
} else {
474-
bufferIndex = 0;
477+
writeBufferIndex = 0;
475478
int bufferSize = (data.length / maxTransferBytes) + 1;
476-
bytesBuffer = new byte[bufferSize][maxTransferBytes];
479+
writeBuffer = new byte[bufferSize][maxTransferBytes];
477480

478-
for (int i = 0; i < bytesBuffer.length; i++) {
481+
for (int i = 0; i < writeBuffer.length; i++) {
479482
int start = i * maxTransferBytes;
480483
int end = start + maxTransferBytes;
481484
if (start >= data.length)
482485
break;
483486
if (end > data.length)
484487
end = data.length;
485-
bytesBuffer[i] = Arrays.copyOfRange(data, start, end);
488+
writeBuffer[i] = Arrays.copyOfRange(data, start, end);
486489
}
487490
}
488491
writeCharacteristic();
@@ -494,19 +497,19 @@ public void write(byte[] data) {
494497
*
495498
*/
496499
private void writeCharacteristic() {
497-
Log.v(TAG, "writeCharacteristic " + bufferIndex);
498-
if (bufferIndex >= bytesBuffer.length)
500+
Log.v(TAG, "writeCharacteristic " + writeBufferIndex);
501+
if (writeBufferIndex >= writeBuffer.length)
499502
return;
500503

501-
byte[] bytes = bytesBuffer[bufferIndex];
504+
byte[] bytes = writeBuffer[writeBufferIndex];
502505

503506
boolean setValue = characteristicRxTx.setValue(bytes);
504507
Log.v(TAG, "setValue: " + setValue);
505508

506509
boolean writeCharacteristic = bluetoothGatt.writeCharacteristic(characteristicRxTx);
507510
Log.v(TAG, "writeCharacteristic: " + writeCharacteristic);
508511

509-
bufferIndex++;
512+
writeBufferIndex++;
510513
}
511514

512515
}

0 commit comments

Comments
 (0)