Merge "Merge "Disable BLE-MIDI connection logic while researching UUIDs connection message." into tm-dev am: 1f274466df am: b5f3438176" into tm-d1-dev-plus-aosp

This commit is contained in:
Automerger Merge Worker
2022-04-30 03:32:43 +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, BluetoothGattCharacteristic characteristic,
byte[] value, byte[] value,
int status) { 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 // switch to receiving notifications after initial characteristic read
mBluetoothGatt.setCharacteristicNotification(characteristic, true); mBluetoothGatt.setCharacteristicNotification(characteristic, true);

View File

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