From 6d9c9dcb5b81c0fa7fc1a2efd12a8f3c0b278a08 Mon Sep 17 00:00:00 2001 From: Robert Wu Date: Thu, 10 Mar 2022 23:09:47 +0000 Subject: [PATCH] BluetoothMidi: Remove unsafe code In ag/16192242, several functions in Bluetooth relating to characteristics were marked as unsafe and new ones were added. This CL is meant to conform with these standards and potentially remove a long standing bug. Bug: 146385555 Test: Added Identity Reply/Request code to MIDIMatrix and tested with Yamaha MD-BT01 for Android T->T, T->R, R->T Change-Id: I07311296509d4d042fc92aec327f40993d2e6f54 --- .../BluetoothMidiDevice.java | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java b/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java index 4c3b68958c0f..0e6249047980 100644 --- a/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java +++ b/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java @@ -150,6 +150,7 @@ public final class BluetoothMidiDevice { @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, + byte[] value, int status) { Log.d(TAG, "onCharacteristicRead " + status); @@ -164,8 +165,8 @@ public final class BluetoothMidiDevice { BluetoothGattDescriptor descriptor = characteristic.getDescriptor( CLIENT_CHARACTERISTIC_CONFIG); if (descriptor != null) { - descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); - boolean result = mBluetoothGatt.writeDescriptor(descriptor); + int result = mBluetoothGatt.writeDescriptor(descriptor, + BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); Log.d(TAG, "writeDescriptor returned " + result); } else { Log.e(TAG, "No CLIENT_CHARACTERISTIC_CONFIG for device " + mBluetoothDevice); @@ -184,12 +185,13 @@ public final class BluetoothMidiDevice { @Override public void onCharacteristicChanged(BluetoothGatt gatt, - BluetoothGattCharacteristic characteristic) { + BluetoothGattCharacteristic characteristic, + byte[] value) { if (DEBUG) { - logByteArray("Received ", characteristic.getValue(), 0, - characteristic.getValue().length); + logByteArray("Received BLE packet", value, 0, + value.length); } - mPacketDecoder.decodePacket(characteristic.getValue(), mOutputReceiver); + mPacketDecoder.decodePacket(value, mOutputReceiver); } @Override @@ -227,17 +229,14 @@ public final class BluetoothMidiDevice { mCachedBuffer = new byte[count]; } System.arraycopy(buffer, 0, mCachedBuffer, 0, count); - if (!mCharacteristic.setValue(mCachedBuffer)) { - Log.w(TAG, "could not set characteristic value"); - return false; - } if (DEBUG) { logByteArray("Sent ", mCharacteristic.getValue(), 0, mCharacteristic.getValue().length); } - if (!mBluetoothGatt.writeCharacteristic(mCharacteristic)) { + if (mBluetoothGatt.writeCharacteristic(mCharacteristic, mCachedBuffer, + mCharacteristic.getWriteType()) != BluetoothGatt.GATT_SUCCESS) { Log.w(TAG, "could not write characteristic to Bluetooth GATT"); return false; }