@@ -26,6 +26,8 @@ public class BleGattWrapper extends BluetoothGattCallback {
26
26
public static final UUID BT_KISS_CHARACTERISTIC_RX_UUID = UUID .fromString ("00000003-ba2a-46c9-ae49-01b0961f68bb" );
27
27
public static final UUID BT_NOTIFY_UUID = UUID .fromString ("00002902-0000-1000-8000-00805f9b34fb" );
28
28
29
+ private static final int DefaultMtuSize = 16 ;
30
+
29
31
private BluetoothGatt _gatt ;
30
32
private BluetoothGattCharacteristic _rxCharacteristic ;
31
33
private BluetoothGattCharacteristic _txCharacteristic ;
@@ -63,9 +65,7 @@ public int read(byte[] data) throws IOException {
63
65
synchronized (_readBuffer ) {
64
66
// nothing to read
65
67
if (_readBuffer .position () == 0 ) return 0 ;
66
-
67
68
_readBuffer .flip ();
68
-
69
69
int countRead = 0 ;
70
70
try {
71
71
for (int i = 0 ; i < data .length ; i ++) {
@@ -85,25 +85,32 @@ public int read(byte[] data) throws IOException {
85
85
86
86
public int write (byte [] data ) throws IOException {
87
87
if (!_isConnected ) throw new IOException ();
88
-
89
88
synchronized (_writeBuffer ) {
90
89
_writeBuffer .put (data );
91
- _writeBuffer .flip ();
90
+ writeCharacteristic (_txCharacteristic );
91
+ return data .length ;
92
+ }
93
+ }
92
94
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 ];
94
101
_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 );
104
106
}
105
- return data .length ;
106
107
}
108
+ _writeBuffer .compact ();
109
+ }
110
+
111
+ @ Override
112
+ public void onMtuChanged (BluetoothGatt gatt , int mtu , int status ) {
113
+ super .onMtuChanged (gatt , mtu , status );
107
114
}
108
115
109
116
@ Override
@@ -129,18 +136,12 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
129
136
@ Override
130
137
public void onCharacteristicWrite (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic , int status ) {
131
138
super .onCharacteristicWrite (gatt , characteristic , status );
132
-
133
- if (status == BluetoothGatt .GATT_SUCCESS && _writeBuffer .position () > 0 ) {
139
+ if (status == BluetoothGatt .GATT_SUCCESS ) {
134
140
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 );
143
142
}
143
+ } else {
144
+ Log .e (TAG , "GATT write failure " + status );
144
145
}
145
146
}
146
147
0 commit comments