Merge "Bluetooth MIDI: Set MTU after service discovery" into tm-qpr-dev am: 9871f1fe9c

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/20114766

Change-Id: Ia789a51c4798e8b0dc8c9e583860fb5531811836
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
TreeHugger Robot
2022-10-19 17:56:08 +00:00
committed by Automerger Merge Worker

View File

@@ -100,16 +100,12 @@ public final class BluetoothMidiDevice {
@Override @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) { int newState) {
Log.d(TAG, "onConnectionStateChange() status: " + status + ", newState: " + newState);
String intentAction; String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) { if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.d(TAG, "Connected to GATT server."); Log.d(TAG, "Connected to GATT server.");
Log.d(TAG, "Attempting to start service discovery:" + Log.d(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices()); mBluetoothGatt.discoverServices());
if (!mBluetoothGatt.requestMtu(MAX_PACKET_SIZE)) {
Log.e(TAG, "request mtu failed");
mPacketEncoder.setMaxPacketSize(DEFAULT_PACKET_SIZE);
mPacketDecoder.setMaxPacketSize(DEFAULT_PACKET_SIZE);
}
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) { } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected from GATT server."); Log.i(TAG, "Disconnected from GATT server.");
close(); close();
@@ -118,6 +114,7 @@ public final class BluetoothMidiDevice {
@Override @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) { public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG, "onServicesDiscovered() status: " + status);
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(MIDI_SERVICE); BluetoothGattService service = gatt.getService(MIDI_SERVICE);
if (service != null) { if (service != null) {
@@ -137,6 +134,13 @@ public final class BluetoothMidiDevice {
// Specification says to read the characteristic first and then // Specification says to read the characteristic first and then
// switch to receiving notifications // switch to receiving notifications
mBluetoothGatt.readCharacteristic(characteristic); mBluetoothGatt.readCharacteristic(characteristic);
// Request higher MTU size
if (!gatt.requestMtu(MAX_PACKET_SIZE)) {
Log.e(TAG, "request mtu failed");
mPacketEncoder.setMaxPacketSize(DEFAULT_PACKET_SIZE);
mPacketDecoder.setMaxPacketSize(DEFAULT_PACKET_SIZE);
}
} }
openBluetoothDevice(mBluetoothDevice); openBluetoothDevice(mBluetoothDevice);
@@ -235,13 +239,13 @@ public final class BluetoothMidiDevice {
System.arraycopy(buffer, 0, mCachedBuffer, 0, count); System.arraycopy(buffer, 0, mCachedBuffer, 0, count);
if (DEBUG) { if (DEBUG) {
logByteArray("Sent ", mCharacteristic.getValue(), 0, logByteArray("Sent ", mCachedBuffer, 0, mCachedBuffer.length);
mCharacteristic.getValue().length);
} }
if (mBluetoothGatt.writeCharacteristic(mCharacteristic, mCachedBuffer, int result = mBluetoothGatt.writeCharacteristic(mCharacteristic, mCachedBuffer,
mCharacteristic.getWriteType()) != BluetoothGatt.GATT_SUCCESS) { mCharacteristic.getWriteType());
Log.w(TAG, "could not write characteristic to Bluetooth GATT"); if (result != BluetoothGatt.GATT_SUCCESS) {
Log.w(TAG, "could not write characteristic to Bluetooth GATT. result: " + result);
return false; return false;
} }
@@ -254,6 +258,10 @@ public final class BluetoothMidiDevice {
mBluetoothDevice = device; mBluetoothDevice = device;
mService = service; mService = service;
// Set a small default packet size in case there is an issue with configuring MTUs.
mPacketEncoder.setMaxPacketSize(DEFAULT_PACKET_SIZE);
mPacketDecoder.setMaxPacketSize(DEFAULT_PACKET_SIZE);
mBluetoothGatt = mBluetoothDevice.connectGatt(context, false, mGattCallback); mBluetoothGatt = mBluetoothDevice.connectGatt(context, false, mGattCallback);
mContext = context; mContext = context;