Skip to content

Commit 12193f5

Browse files
committed
Fix BLE transmit
1 parent 4e2b8bb commit 12193f5

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

codec2talkie/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId "com.radio.codec2talkie"
1111
minSdkVersion 23
1212
targetSdkVersion 30
13-
versionCode 148
14-
versionName "1.48"
13+
versionCode 149
14+
versionName "1.49"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}

codec2talkie/src/main/java/com/radio/codec2talkie/connect/BleGattWrapper.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class BleGattWrapper extends BluetoothGattCallback {
2626
public static final UUID BT_KISS_CHARACTERISTIC_RX_UUID = UUID.fromString("00000003-ba2a-46c9-ae49-01b0961f68bb");
2727
public static final UUID BT_NOTIFY_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
2828

29+
private static final int DefaultMtuSize = 16;
30+
2931
private BluetoothGatt _gatt;
3032
private BluetoothGattCharacteristic _rxCharacteristic;
3133
private BluetoothGattCharacteristic _txCharacteristic;
@@ -63,9 +65,7 @@ public int read(byte[] data) throws IOException {
6365
synchronized (_readBuffer) {
6466
// nothing to read
6567
if (_readBuffer.position() == 0) return 0;
66-
6768
_readBuffer.flip();
68-
6969
int countRead = 0;
7070
try {
7171
for (int i = 0; i < data.length; i++) {
@@ -85,25 +85,32 @@ public int read(byte[] data) throws IOException {
8585

8686
public int write(byte[] data) throws IOException {
8787
if (!_isConnected) throw new IOException();
88-
8988
synchronized (_writeBuffer) {
9089
_writeBuffer.put(data);
91-
_writeBuffer.flip();
90+
writeCharacteristic(_txCharacteristic);
91+
return data.length;
92+
}
93+
}
9294

93-
byte[] arr = new byte[_writeBuffer.limit()];
95+
private void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
96+
_writeBuffer.flip();
97+
int cntRemaining = _writeBuffer.remaining();
98+
if (cntRemaining > 0) {
99+
int cntWrite = Math.min(cntRemaining, DefaultMtuSize);
100+
byte[] arr = new byte[cntWrite];
94101
_writeBuffer.get(arr);
95-
_txCharacteristic.setValue(arr);
96-
97-
if (_gatt.writeCharacteristic(_txCharacteristic)) {
98-
// written successfully
99-
_writeBuffer.clear();
100-
} else {
101-
// redo
102-
_writeBuffer.position(_writeBuffer.limit());
103-
_writeBuffer.limit(_writeBuffer.capacity());
102+
characteristic.setValue(arr);
103+
if (!_gatt.writeCharacteristic(characteristic)) {
104+
Log.d(TAG, "GATT write delayed");
105+
_writeBuffer.position(_writeBuffer.position() - cntWrite);
104106
}
105-
return data.length;
106107
}
108+
_writeBuffer.compact();
109+
}
110+
111+
@Override
112+
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
113+
super.onMtuChanged(gatt, mtu, status);
107114
}
108115

109116
@Override
@@ -129,18 +136,12 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
129136
@Override
130137
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
131138
super.onCharacteristicWrite(gatt, characteristic, status);
132-
133-
if (status == BluetoothGatt.GATT_SUCCESS && _writeBuffer.position() > 0) {
139+
if (status == BluetoothGatt.GATT_SUCCESS) {
134140
synchronized (_writeBuffer) {
135-
_writeBuffer.flip();
136-
137-
byte[] arr = new byte[_writeBuffer.limit()];
138-
_writeBuffer.get(arr);
139-
characteristic.setValue(arr);
140-
141-
_gatt.writeCharacteristic(characteristic);
142-
_writeBuffer.clear();
141+
writeCharacteristic(_txCharacteristic);
143142
}
143+
} else {
144+
Log.e(TAG, "GATT write failure " + status);
144145
}
145146
}
146147

0 commit comments

Comments
 (0)