Merge "BluetoothMidi: Remove unsafe code" into tm-dev

This commit is contained in:
Robert Wu
2022-03-23 16:06:50 +00:00
committed by Android (Google) Code Review

View File

@@ -150,6 +150,7 @@ public final class BluetoothMidiDevice {
@Override @Override
public void onCharacteristicRead(BluetoothGatt gatt, public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, BluetoothGattCharacteristic characteristic,
byte[] value,
int status) { int status) {
Log.d(TAG, "onCharacteristicRead " + status); Log.d(TAG, "onCharacteristicRead " + status);
@@ -164,8 +165,8 @@ public final class BluetoothMidiDevice {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor( BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
CLIENT_CHARACTERISTIC_CONFIG); CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor != null) { if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); int result = mBluetoothGatt.writeDescriptor(descriptor,
boolean result = mBluetoothGatt.writeDescriptor(descriptor); BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Log.d(TAG, "writeDescriptor returned " + result); Log.d(TAG, "writeDescriptor returned " + result);
} else { } else {
Log.e(TAG, "No CLIENT_CHARACTERISTIC_CONFIG for device " + mBluetoothDevice); Log.e(TAG, "No CLIENT_CHARACTERISTIC_CONFIG for device " + mBluetoothDevice);
@@ -184,12 +185,13 @@ public final class BluetoothMidiDevice {
@Override @Override
public void onCharacteristicChanged(BluetoothGatt gatt, public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) { BluetoothGattCharacteristic characteristic,
byte[] value) {
if (DEBUG) { if (DEBUG) {
logByteArray("Received ", characteristic.getValue(), 0, logByteArray("Received BLE packet", value, 0,
characteristic.getValue().length); value.length);
} }
mPacketDecoder.decodePacket(characteristic.getValue(), mOutputReceiver); mPacketDecoder.decodePacket(value, mOutputReceiver);
} }
@Override @Override
@@ -227,17 +229,14 @@ public final class BluetoothMidiDevice {
mCachedBuffer = new byte[count]; mCachedBuffer = new byte[count];
} }
System.arraycopy(buffer, 0, mCachedBuffer, 0, count); System.arraycopy(buffer, 0, mCachedBuffer, 0, count);
if (!mCharacteristic.setValue(mCachedBuffer)) {
Log.w(TAG, "could not set characteristic value");
return false;
}
if (DEBUG) { if (DEBUG) {
logByteArray("Sent ", mCharacteristic.getValue(), 0, logByteArray("Sent ", mCharacteristic.getValue(), 0,
mCharacteristic.getValue().length); 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"); Log.w(TAG, "could not write characteristic to Bluetooth GATT");
return false; return false;
} }