From dd87cd3bb611b7449edfa17d59d9b962784750ee Mon Sep 17 00:00:00 2001 From: Ivan Podogov Date: Fri, 30 Dec 2016 14:43:29 +0000 Subject: [PATCH] HID Device role API fixes This change makes HIDD API more like the other ones, i.e. supporting multiple devices, and implements missing methods. While the underlying implementation may still only support a single device at a time, the "device" parameter can still be useful for checking if the application is trying to send the data to a correct device. Test: make Change-Id: I55fe04c0762a96fcddd6c6678e790361d648111a --- .../bluetooth/BluetoothHidDeviceCallback.java | 38 +++--- .../android/bluetooth/BluetoothInputHost.java | 111 ++++++++++++------ .../IBluetoothHidDeviceCallback.aidl | 10 +- .../bluetooth/IBluetoothInputHost.aidl | 15 ++- 4 files changed, 108 insertions(+), 66 deletions(-) diff --git a/core/java/android/bluetooth/BluetoothHidDeviceCallback.java b/core/java/android/bluetooth/BluetoothHidDeviceCallback.java index 0f0e050a1f409..f519776276fcb 100644 --- a/core/java/android/bluetooth/BluetoothHidDeviceCallback.java +++ b/core/java/android/bluetooth/BluetoothHidDeviceCallback.java @@ -45,9 +45,9 @@ public abstract class BluetoothHidDeviceCallback { * false otherwise. */ public void onAppStatusChanged(BluetoothDevice pluggedDevice, - BluetoothHidDeviceAppConfiguration config, boolean registered) { - Log.d(TAG, "onAppStatusChanged: pluggedDevice=" + (pluggedDevice == null ? - null : pluggedDevice.toString()) + " registered=" + registered); + BluetoothHidDeviceAppConfiguration config, boolean registered) { + Log.d(TAG, "onAppStatusChanged: pluggedDevice=" + pluggedDevice + " registered=" + + registered); } /** @@ -60,13 +60,13 @@ public abstract class BluetoothHidDeviceCallback { * @param state Connection state as defined in {@link BluetoothProfile}. */ public void onConnectionStateChanged(BluetoothDevice device, int state) { - Log.d(TAG, "onConnectionStateChanged: device=" + device.toString() + " state=" + state); + Log.d(TAG, "onConnectionStateChanged: device=" + device + " state=" + state); } /** * Callback called when GET_REPORT is received from remote host. Should be * replied by application using - * {@link BluetoothHidDevice#replyReport(byte, byte, byte[])}. + * {@link BluetoothHidDevice#replyReport(BluetoothDevice, byte, byte, byte[])}. * * @param type Requested Report Type. * @param id Requested Report Id, can be 0 if no Report Id are defined in @@ -74,21 +74,22 @@ public abstract class BluetoothHidDeviceCallback { * @param bufferSize Requested buffer size, application shall respond with * at least given number of bytes. */ - public void onGetReport(byte type, byte id, int bufferSize) { - Log.d(TAG, "onGetReport: type=" + type + " id=" + id + " bufferSize=" + bufferSize); + public void onGetReport(BluetoothDevice device, byte type, byte id, int bufferSize) { + Log.d(TAG, "onGetReport: device=" + device + " type=" + type + " id=" + id + " bufferSize=" + + bufferSize); } /** * Callback called when SET_REPORT is received from remote host. In case * received data are invalid, application shall respond with - * {@link BluetoothHidDevice#reportError()}. + * {@link BluetoothHidDevice#reportError(BluetoothDevice)}. * * @param type Report Type. * @param id Report Id. * @param data Report data. */ - public void onSetReport(byte type, byte id, byte[] data) { - Log.d(TAG, "onSetReport: type=" + type + " id=" + id); + public void onSetReport(BluetoothDevice device, byte type, byte id, byte[] data) { + Log.d(TAG, "onSetReport: device=" + device + " type=" + type + " id=" + id); } /** @@ -99,8 +100,8 @@ public abstract class BluetoothHidDeviceCallback { * * @param protocol Protocol Mode. */ - public void onSetProtocol(byte protocol) { - Log.d(TAG, "onSetProtocol: protocol=" + protocol); + public void onSetProtocol(BluetoothDevice device, byte protocol) { + Log.d(TAG, "onSetProtocol: device=" + device + " protocol=" + protocol); } /** @@ -111,16 +112,17 @@ public abstract class BluetoothHidDeviceCallback { * @param reportId Report Id. * @param data Report data. */ - public void onIntrData(byte reportId, byte[] data) { - Log.d(TAG, "onIntrData: reportId=" + reportId); + public void onIntrData(BluetoothDevice device, byte reportId, byte[] data) { + Log.d(TAG, "onIntrData: device=" + device + " reportId=" + reportId); } /** * Callback called when Virtual Cable is removed. This can be either due to - * {@link BluetoothHidDevice#unplug()} or request from remote side. After - * this callback is received connection will be disconnected automatically. + * {@link BluetoothHidDevice#unplug(BluetoothDevice)} or request from remote + * side. After this callback is received connection will be disconnected + * automatically. */ - public void onVirtualCableUnplug() { - Log.d(TAG, "onVirtualCableUnplug"); + public void onVirtualCableUnplug(BluetoothDevice device) { + Log.d(TAG, "onVirtualCableUnplug: device=" + device); } } diff --git a/core/java/android/bluetooth/BluetoothInputHost.java b/core/java/android/bluetooth/BluetoothInputHost.java index 129fe7ecdc39e..68d105f1155d8 100644 --- a/core/java/android/bluetooth/BluetoothInputHost.java +++ b/core/java/android/bluetooth/BluetoothInputHost.java @@ -27,6 +27,7 @@ import android.os.RemoteException; import android.util.Log; import java.util.Arrays; +import java.util.ArrayList; import java.util.List; /** @@ -137,28 +138,28 @@ public final class BluetoothInputHost implements BluetoothProfile { } @Override - public void onGetReport(byte type, byte id, int bufferSize) { - mCallback.onGetReport(type, id, bufferSize); + public void onGetReport(BluetoothDevice device, byte type, byte id, int bufferSize) { + mCallback.onGetReport(device, type, id, bufferSize); } @Override - public void onSetReport(byte type, byte id, byte[] data) { - mCallback.onSetReport(type, id, data); + public void onSetReport(BluetoothDevice device, byte type, byte id, byte[] data) { + mCallback.onSetReport(device, type, id, data); } @Override - public void onSetProtocol(byte protocol) { - mCallback.onSetProtocol(protocol); + public void onSetProtocol(BluetoothDevice device, byte protocol) { + mCallback.onSetProtocol(device, protocol); } @Override - public void onIntrData(byte reportId, byte[] data) { - mCallback.onIntrData(reportId, data); + public void onIntrData(BluetoothDevice device, byte reportId, byte[] data) { + mCallback.onIntrData(device, reportId, data); } @Override - public void onVirtualCableUnplug() { - mCallback.onVirtualCableUnplug(); + public void onVirtualCableUnplug(BluetoothDevice device) { + mCallback.onVirtualCableUnplug(device); } } @@ -276,21 +277,59 @@ public final class BluetoothInputHost implements BluetoothProfile { mServiceListener = null; } - @Override + /** + * {@inheritDoc} + */ public List getConnectedDevices() { Log.v(TAG, "getConnectedDevices()"); - return null; + + if (mService != null) { + try { + return mService.getConnectedDevices(); + } catch (RemoteException e) { + Log.e(TAG, e.toString()); + } + } else { + Log.w(TAG, "Proxy not attached to service"); + } + + return new ArrayList(); } - @Override + /** + * {@inheritDoc} + */ public List getDevicesMatchingConnectionStates(int[] states) { Log.v(TAG, "getDevicesMatchingConnectionStates(): states=" + Arrays.toString(states)); - return null; + + if (mService != null) { + try { + return mService.getDevicesMatchingConnectionStates(states); + } catch (RemoteException e) { + Log.e(TAG, e.toString()); + } + } else { + Log.w(TAG, "Proxy not attached to service"); + } + + return new ArrayList(); } - @Override + /** + * {@inheritDoc} + */ public int getConnectionState(BluetoothDevice device) { - Log.v(TAG, "getConnectionState(): device=" + device.getAddress()); + Log.v(TAG, "getConnectionState(): device=" + device); + + if (mService != null) { + try { + return mService.getConnectionState(device); + } catch (RemoteException e) { + Log.e(TAG, e.toString()); + } + } else { + Log.w(TAG, "Proxy not attached to service"); + } return STATE_DISCONNECTED; } @@ -379,14 +418,12 @@ public final class BluetoothInputHost implements BluetoothProfile { * @param data Report data, not including Report Id. * @return */ - public boolean sendReport(int id, byte[] data) { - Log.v(TAG, "sendReport(): id=" + id); - + public boolean sendReport(BluetoothDevice device, int id, byte[] data) { boolean result = false; if (mService != null) { try { - result = mService.sendReport(id, data); + result = mService.sendReport(device, id, data); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -399,21 +436,21 @@ public final class BluetoothInputHost implements BluetoothProfile { /** * Sends report to remote host as reply for GET_REPORT request from - * {@link BluetoothHidDeviceCallback#onGetReport(byte, byte, int)}. + * {@link BluetoothHidDeviceCallback#onGetReport(BluetoothDevice, byte, byte, int)}. * * @param type Report Type, as in request. * @param id Report Id, as in request. * @param data Report data, not including Report Id. * @return */ - public boolean replyReport(byte type, byte id, byte[] data) { - Log.v(TAG, "replyReport(): type=" + type + " id=" + id); + public boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) { + Log.v(TAG, "replyReport(): device=" + device + " type=" + type + " id=" + id); boolean result = false; if (mService != null) { try { - result = mService.replyReport(type, id, data); + result = mService.replyReport(device, type, id, data); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -426,19 +463,19 @@ public final class BluetoothInputHost implements BluetoothProfile { /** * Sends error handshake message as reply for invalid SET_REPORT request - * from {@link BluetoothHidDeviceCallback#onSetReport(byte, byte, byte[])}. + * from {@link BluetoothHidDeviceCallback#onSetReport(BluetoothDevice, byte, byte, byte[])}. * * @param error Error to be sent for SET_REPORT via HANDSHAKE. * @return */ - public boolean reportError(byte error) { - Log.v(TAG, "reportError(): error = " + error); + public boolean reportError(BluetoothDevice device, byte error) { + Log.v(TAG, "reportError(): device=" + device + " error=" + error); boolean result = false; if (mService != null) { try { - result = mService.reportError(error); + result = mService.reportError(device, error); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -454,14 +491,14 @@ public final class BluetoothInputHost implements BluetoothProfile { * * @return */ - public boolean unplug() { - Log.v(TAG, "unplug()"); + public boolean unplug(BluetoothDevice device) { + Log.v(TAG, "unplug(): device=" + device); boolean result = false; if (mService != null) { try { - result = mService.unplug(); + result = mService.unplug(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -478,14 +515,14 @@ public final class BluetoothInputHost implements BluetoothProfile { * * @return */ - public boolean connect() { - Log.v(TAG, "connect()"); + public boolean connect(BluetoothDevice device) { + Log.v(TAG, "connect(): device=" + device); boolean result = false; if (mService != null) { try { - result = mService.connect(); + result = mService.connect(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -501,14 +538,14 @@ public final class BluetoothInputHost implements BluetoothProfile { * * @return */ - public boolean disconnect() { - Log.v(TAG, "disconnect()"); + public boolean disconnect(BluetoothDevice device) { + Log.v(TAG, "disconnect(): device=" + device); boolean result = false; if (mService != null) { try { - result = mService.disconnect(); + result = mService.disconnect(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } diff --git a/core/java/android/bluetooth/IBluetoothHidDeviceCallback.aidl b/core/java/android/bluetooth/IBluetoothHidDeviceCallback.aidl index 125287696585d..a737198ad954a 100644 --- a/core/java/android/bluetooth/IBluetoothHidDeviceCallback.aidl +++ b/core/java/android/bluetooth/IBluetoothHidDeviceCallback.aidl @@ -23,9 +23,9 @@ import android.bluetooth.BluetoothHidDeviceAppConfiguration; interface IBluetoothHidDeviceCallback { void onAppStatusChanged(in BluetoothDevice device, in BluetoothHidDeviceAppConfiguration config, boolean registered); void onConnectionStateChanged(in BluetoothDevice device, in int state); - void onGetReport(in byte type, in byte id, in int bufferSize); - void onSetReport(in byte type, in byte id, in byte[] data); - void onSetProtocol(in byte protocol); - void onIntrData(in byte reportId, in byte[] data); - void onVirtualCableUnplug(); + void onGetReport(in BluetoothDevice device, in byte type, in byte id, in int bufferSize); + void onSetReport(in BluetoothDevice device, in byte type, in byte id, in byte[] data); + void onSetProtocol(in BluetoothDevice device, in byte protocol); + void onIntrData(in BluetoothDevice device, in byte reportId, in byte[] data); + void onVirtualCableUnplug(in BluetoothDevice device); } diff --git a/core/java/android/bluetooth/IBluetoothInputHost.aidl b/core/java/android/bluetooth/IBluetoothInputHost.aidl index b2c421c47c586..6c4993f750e64 100644 --- a/core/java/android/bluetooth/IBluetoothInputHost.aidl +++ b/core/java/android/bluetooth/IBluetoothInputHost.aidl @@ -28,10 +28,13 @@ interface IBluetoothInputHost { in BluetoothHidDeviceAppSdpSettings sdp, in BluetoothHidDeviceAppQosSettings inQos, in BluetoothHidDeviceAppQosSettings outQos, in IBluetoothHidDeviceCallback callback); boolean unregisterApp(in BluetoothHidDeviceAppConfiguration config); - boolean sendReport(in int id, in byte[] data); - boolean replyReport(in byte type, in byte id, in byte[] data); - boolean reportError(byte error); - boolean unplug(); - boolean connect(); - boolean disconnect(); + boolean sendReport(in BluetoothDevice device, in int id, in byte[] data); + boolean replyReport(in BluetoothDevice device, in byte type, in byte id, in byte[] data); + boolean reportError(in BluetoothDevice device, byte error); + boolean unplug(in BluetoothDevice device); + boolean connect(in BluetoothDevice device); + boolean disconnect(in BluetoothDevice device); + List getConnectedDevices(); + List getDevicesMatchingConnectionStates(in int[] states); + int getConnectionState(in BluetoothDevice device); }