Merge "Disable BLE-MIDI connection logic while researching UUIDs connection message." into tm-dev

This commit is contained in:
TreeHugger Robot
2022-04-30 03:25:17 +00:00
committed by Android (Google) Code Review
2 changed files with 77 additions and 12 deletions

View File

@@ -152,8 +152,12 @@ public final class BluetoothMidiDevice {
BluetoothGattCharacteristic characteristic,
byte[] value,
int status) {
Log.d(TAG, "onCharacteristicRead " + status);
Log.d(TAG, "onCharacteristicRead status:" + status);
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for (StackTraceElement element : elements) {
Log.i(TAG, " " + element);
}
// switch to receiving notifications after initial characteristic read
mBluetoothGatt.setCharacteristicNotification(characteristic, true);

View File

@@ -23,7 +23,7 @@ import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
// import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
@@ -174,6 +174,7 @@ public class MidiService extends IMidiManager.Stub {
};
private final class Client implements IBinder.DeathRecipient {
private static final String TAG = "MidiService.Client";
// Binder token for this client
private final IBinder mToken;
// This client's UID
@@ -215,7 +216,9 @@ public class MidiService extends IMidiManager.Stub {
}
public void addDeviceConnection(Device device, IMidiDeviceOpenCallback callback) {
Log.d(TAG, "addDeviceConnection() device:" + device);
if (mDeviceConnections.size() >= MAX_CONNECTIONS_PER_CLIENT) {
Log.i(TAG, "too many MIDI connections for UID = " + mUid);
throw new SecurityException(
"too many MIDI connections for UID = " + mUid);
}
@@ -343,6 +346,7 @@ public class MidiService extends IMidiManager.Stub {
}
private final class Device implements IBinder.DeathRecipient {
private static final String TAG = "MidiService.Device";
private IMidiDeviceServer mServer;
private MidiDeviceInfo mDeviceInfo;
private final BluetoothDevice mBluetoothDevice;
@@ -378,6 +382,7 @@ public class MidiService extends IMidiManager.Stub {
}
private void setDeviceServer(IMidiDeviceServer server) {
Log.i(TAG, "setDeviceServer()");
if (server != null) {
if (mServer != null) {
Log.e(TAG, "mServer already set in setDeviceServer");
@@ -459,21 +464,28 @@ public class MidiService extends IMidiManager.Stub {
}
public void addDeviceConnection(DeviceConnection connection) {
Log.d(TAG, "addDeviceConnection() [A] connection:" + connection);
synchronized (mDeviceConnections) {
Log.d(TAG, " mServer:" + mServer);
if (mServer != null) {
Log.i(TAG, "++++ A");
mDeviceConnections.add(connection);
connection.notifyClient(mServer);
} else if (mServiceConnection == null &&
(mServiceInfo != null || mBluetoothDevice != null)) {
Log.i(TAG, "++++ B");
mDeviceConnections.add(connection);
mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "++++ onServiceConnected() mBluetoothDevice:"
+ mBluetoothDevice);
IMidiDeviceServer server = null;
if (mBluetoothDevice != null) {
IBluetoothMidiService mBluetoothMidiService =
IBluetoothMidiService.Stub.asInterface(service);
Log.i(TAG, "++++ mBluetoothMidiService:" + mBluetoothMidiService);
if (mBluetoothMidiService != null) {
try {
// We need to explicitly add the device in a separate method
@@ -592,6 +604,7 @@ public class MidiService extends IMidiManager.Stub {
// Represents a connection between a client and a device
private final class DeviceConnection {
private static final String TAG = "MidiService.DeviceConnection";
private final IBinder mToken = new Binder();
private final Device mDevice;
private final Client mClient;
@@ -616,6 +629,8 @@ public class MidiService extends IMidiManager.Stub {
}
public void notifyClient(IMidiDeviceServer deviceServer) {
Log.d(TAG, "notifyClient");
if (mCallback != null) {
try {
mCallback.onDeviceOpened(deviceServer, (deviceServer == null ? null : mToken));
@@ -628,7 +643,9 @@ public class MidiService extends IMidiManager.Stub {
@Override
public String toString() {
return "DeviceConnection Device ID: " + mDevice.getDeviceInfo().getId();
// return "DeviceConnection Device ID: " + mDevice.getDeviceInfo().getId();
return mDevice != null && mDevice.getDeviceInfo() != null
? ("" + mDevice.getDeviceInfo().getId()) : "null";
}
}
@@ -669,9 +686,10 @@ public class MidiService extends IMidiManager.Stub {
}
private void dumpUuids(BluetoothDevice btDevice) {
Log.d(TAG, "UUIDs for " + btDevice);
ParcelUuid[] uuidParcels = btDevice.getUuids();
Log.d(TAG, "dumpUuids(" + btDevice + ") numParcels:"
+ (uuidParcels != null ? uuidParcels.length : 0));
if (uuidParcels == null) {
Log.d(TAG, "No UUID Parcels");
return;
@@ -707,7 +725,8 @@ public class MidiService extends IMidiManager.Stub {
}
switch (action) {
case BluetoothDevice.ACTION_ACL_CONNECTED: {
case BluetoothDevice.ACTION_ACL_CONNECTED:
{
Log.d(TAG, "ACTION_ACL_CONNECTED");
dumpIntentExtras(intent);
// BLE-MIDI controllers are by definition BLE, so if this device
@@ -734,7 +753,8 @@ public class MidiService extends IMidiManager.Stub {
}
break;
case BluetoothDevice.ACTION_ACL_DISCONNECTED: {
case BluetoothDevice.ACTION_ACL_DISCONNECTED:
{
Log.d(TAG, "ACTION_ACL_DISCONNECTED");
BluetoothDevice btDevice =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
@@ -745,6 +765,35 @@ public class MidiService extends IMidiManager.Stub {
}
}
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
// {
// Log.d(TAG, "ACTION_BOND_STATE_CHANGED");
// int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
// Log.d(TAG, " bondState:" + bondState);
// BluetoothDevice btDevice =
// intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Log.d(TAG, " btDevice:" + btDevice);
// dumpUuids(btDevice);
// if (isBLEMIDIDevice(btDevice)) {
// Log.d(TAG, "BT MIDI DEVICE");
// openBluetoothDevice(btDevice);
// }
// }
// break;
case BluetoothDevice.ACTION_UUID:
{
Log.d(TAG, "ACTION_UUID");
BluetoothDevice btDevice =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
dumpUuids(btDevice);
if (isBLEMIDIDevice(btDevice)) {
Log.d(TAG, "BT MIDI DEVICE");
openBluetoothDevice(btDevice);
}
}
break;
}
}
};
@@ -753,11 +802,15 @@ public class MidiService extends IMidiManager.Stub {
mContext = context;
mPackageManager = context.getPackageManager();
// TEMPORARY - Disable BTL-MIDI
//FIXME - b/25689266
// Setup broadcast receivers
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
context.registerReceiver(mBleMidiReceiver, filter);
// IntentFilter filter = new IntentFilter();
// filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
// filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
// filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
// filter.addAction(BluetoothDevice.ACTION_UUID);
// context.registerReceiver(mBleMidiReceiver, filter);
mBluetoothServiceUid = -1;
@@ -771,6 +824,8 @@ public class MidiService extends IMidiManager.Stub {
mNonMidiUUIDs.add(BluetoothUuid.LE_AUDIO);
mNonMidiUUIDs.add(BluetoothUuid.HOGP);
mNonMidiUUIDs.add(BluetoothUuid.HEARING_AID);
// This one is coming up
// mNonMidiUUIDs.add(BluetoothUuid.BATTERY);
}
private void onUnlockUser() {
@@ -876,11 +931,13 @@ public class MidiService extends IMidiManager.Stub {
public void openDevice(IBinder token, MidiDeviceInfo deviceInfo,
IMidiDeviceOpenCallback callback) {
Client client = getClient(token);
Log.d(TAG, "openDevice() client:" + client);
if (client == null) return;
Device device;
synchronized (mDevicesByInfo) {
device = mDevicesByInfo.get(deviceInfo);
Log.d(TAG, " device:" + device);
if (device == null) {
throw new IllegalArgumentException("device does not exist: " + deviceInfo);
}
@@ -901,6 +958,7 @@ public class MidiService extends IMidiManager.Stub {
// clear calling identity so bindService does not fail
final long identity = Binder.clearCallingIdentity();
try {
Log.i(TAG, "addDeviceConnection() [B] device:" + device);
client.addDeviceConnection(device, callback);
} finally {
Binder.restoreCallingIdentity(identity);
@@ -916,6 +974,7 @@ public class MidiService extends IMidiManager.Stub {
@Override
public void onDeviceOpened(MidiDevice device) {
synchronized (mBleMidiDeviceMap) {
Log.i(TAG, "onDeviceOpened() device:" + device);
mBleMidiDeviceMap.put(bluetoothDevice, device);
}
}
@@ -949,6 +1008,7 @@ public class MidiService extends IMidiManager.Stub {
// Bluetooth devices are created on demand
Device device;
Log.i(TAG, "alloc device...");
synchronized (mDevicesByInfo) {
device = mBluetoothDevices.get(bluetoothDevice);
if (device == null) {
@@ -956,10 +1016,11 @@ public class MidiService extends IMidiManager.Stub {
mBluetoothDevices.put(bluetoothDevice, device);
}
}
Log.i(TAG, "device: " + device);
// clear calling identity so bindService does not fail
final long identity = Binder.clearCallingIdentity();
try {
Log.i(TAG, "addDeviceConnection() [C] device:" + device);
client.addDeviceConnection(device, callback);
} finally {
Binder.restoreCallingIdentity(identity);