9
9
import android .bluetooth .BluetoothGattDescriptor ;
10
10
import android .bluetooth .BluetoothGattService ;
11
11
import android .bluetooth .BluetoothProfile ;
12
+ import android .bluetooth .BluetoothStatusCodes ;
12
13
import android .content .BroadcastReceiver ;
13
14
import android .content .Context ;
14
15
import android .content .Intent ;
@@ -140,6 +141,9 @@ public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
140
141
public void run () {
141
142
// this calls onCharacteristicRead after completed
142
143
gatt .readCharacteristic (manufacturerCharacteristic );
144
+ if (gattRequestQueue .size () > 0 ) {
145
+ gattRequestQueue .remove (0 ).run ();
146
+ }
143
147
}
144
148
});
145
149
}
@@ -151,6 +155,9 @@ public void run() {
151
155
public void run () {
152
156
// this calls onCharacteristicRead after completed
153
157
gatt .readCharacteristic (modelCharacteristic );
158
+ if (gattRequestQueue .size () > 0 ) {
159
+ gattRequestQueue .remove (0 ).run ();
160
+ }
154
161
}
155
162
});
156
163
}
@@ -160,7 +167,9 @@ public void run() {
160
167
gattRequestQueue .add (new Runnable () {
161
168
@ Override
162
169
public void run () {
163
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
170
+ // if the app is running on Meta/Oculus, don't set the mtu
171
+ boolean isOculusDevices = "miramar" .equals (Build .DEVICE ) || "hollywood" .equals (Build .DEVICE ) || "eureka" .equals (Build .DEVICE );
172
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE || isOculusDevices ) {
164
173
// Android 14: the default MTU size set to 517
165
174
// https://developer.android.com/about/versions/14/behavior-changes-all#mtu-set-to-517
166
175
final int mtu = 517 ;
@@ -179,6 +188,7 @@ public void run() {
179
188
} else {
180
189
// request maximum MTU size
181
190
// this calls onMtuChanged after completed
191
+ // NOTE: Some devices already have MTU set to 517, so the `onMtuChanged` method is not called.
182
192
boolean result = gatt .requestMtu (517 ); // GATT_MAX_MTU_SIZE defined at `stack/include/gatt_api.h`
183
193
Log .d (Constants .TAG , "Central requestMtu address: " + gatt .getDevice ().getAddress () + ", succeed: " + result );
184
194
}
@@ -328,13 +338,25 @@ public void run() {
328
338
}
329
339
330
340
@ Override
331
- public void onCharacteristicChanged (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic ) {
332
- super .onCharacteristicChanged (gatt , characteristic );
341
+ public void onCharacteristicChanged (@ NonNull BluetoothGatt gatt , @ NonNull BluetoothGattCharacteristic characteristic , @ NonNull byte [] value ) {
342
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
343
+ Set <MidiInputDevice > midiInputDevices = midiInputDevicesMap .get (gatt .getDevice ().getAddress ());
344
+ if (midiInputDevices != null ) {
345
+ for (MidiInputDevice midiInputDevice : midiInputDevices ) {
346
+ ((InternalMidiInputDevice ) midiInputDevice ).incomingData (value );
347
+ }
348
+ }
349
+ }
350
+ }
333
351
334
- Set <MidiInputDevice > midiInputDevices = midiInputDevicesMap .get (gatt .getDevice ().getAddress ());
335
- if (midiInputDevices != null ) {
336
- for (MidiInputDevice midiInputDevice : midiInputDevices ) {
337
- ((InternalMidiInputDevice )midiInputDevice ).incomingData (characteristic .getValue ());
352
+ @ Override
353
+ public void onCharacteristicChanged (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic ) {
354
+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .TIRAMISU ) {
355
+ Set <MidiInputDevice > midiInputDevices = midiInputDevicesMap .get (gatt .getDevice ().getAddress ());
356
+ if (midiInputDevices != null ) {
357
+ for (MidiInputDevice midiInputDevice : midiInputDevices ) {
358
+ ((InternalMidiInputDevice ) midiInputDevice ).incomingData (characteristic .getValue ());
359
+ }
338
360
}
339
361
}
340
362
}
@@ -705,8 +727,12 @@ public void configureAsCentralDevice() throws SecurityException {
705
727
List <BluetoothGattDescriptor > descriptors = midiInputCharacteristic .getDescriptors ();
706
728
for (BluetoothGattDescriptor descriptor : descriptors ) {
707
729
if (BleUuidUtils .matches (BleUuidUtils .fromShortValue (0x2902 ), descriptor .getUuid ())) {
708
- descriptor .setValue (BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
709
- bluetoothGatt .writeDescriptor (descriptor );
730
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
731
+ bluetoothGatt .writeDescriptor (descriptor , BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
732
+ } else {
733
+ descriptor .setValue (BluetoothGattDescriptor .ENABLE_NOTIFICATION_VALUE );
734
+ bluetoothGatt .writeDescriptor (descriptor );
735
+ }
710
736
}
711
737
}
712
738
@@ -806,17 +832,19 @@ public void configureAsCentralDevice() {
806
832
}
807
833
808
834
@ Override
809
- public void transferData (@ NonNull byte [] writeBuffer ) throws SecurityException {
835
+ public boolean transferData (@ NonNull byte [] writeBuffer ) throws SecurityException {
810
836
try {
811
837
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
812
- bluetoothGatt .writeCharacteristic (midiOutputCharacteristic , writeBuffer , BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE );
838
+ int result = bluetoothGatt .writeCharacteristic (midiOutputCharacteristic , writeBuffer , BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE );
839
+ return result == BluetoothStatusCodes .SUCCESS ;
813
840
} else {
814
841
midiOutputCharacteristic .setValue (writeBuffer );
815
- bluetoothGatt .writeCharacteristic (midiOutputCharacteristic );
842
+ return bluetoothGatt .writeCharacteristic (midiOutputCharacteristic );
816
843
}
817
844
} catch (Throwable ignored ) {
818
845
// android.os.DeadObjectException will be thrown
819
846
// ignore it
847
+ return false ;
820
848
}
821
849
}
822
850
0 commit comments