Merge "Bluetooth: Thread-safe binder invocation"

am: 94f1fd0da2

Change-Id: I8f9c76996f02c70b8d2ea21d4eb567eaa9da246a
This commit is contained in:
Jack He
2017-09-06 00:30:09 +00:00
committed by android-build-merger
14 changed files with 680 additions and 571 deletions

View File

@@ -125,7 +125,7 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
private Context mContext; private Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private IBluetoothA2dpSink mService; private volatile IBluetoothA2dpSink mService;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
@@ -240,15 +240,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
*/ */
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")"); if (DBG) log("connect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothA2dpSink service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.connect(device); return service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -279,15 +280,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")"); if (DBG) log("disconnect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothA2dpSink service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -297,15 +299,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()"); if (VDBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothA2dpSink service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -315,15 +318,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()"); if (VDBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothA2dpSink service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -333,16 +337,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getState(" + device + ")"); if (VDBG) log("getState(" + device + ")");
if (mService != null && isEnabled() final IBluetoothA2dpSink service = mService;
&& isValidDevice(device)) { if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -359,16 +363,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
*/ */
public BluetoothAudioConfig getAudioConfig(BluetoothDevice device) { public BluetoothAudioConfig getAudioConfig(BluetoothDevice device) {
if (VDBG) log("getAudioConfig(" + device + ")"); if (VDBG) log("getAudioConfig(" + device + ")");
if (mService != null && isEnabled() final IBluetoothA2dpSink service = mService;
&& isValidDevice(device)) { if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getAudioConfig(device); return service.getAudioConfig(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return null; return null;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return null; return null;
} }
@@ -389,20 +393,20 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")"); if (DBG) log("setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() final IBluetoothA2dpSink service = mService;
&& isValidDevice(device)) { if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -421,16 +425,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
*/ */
public int getPriority(BluetoothDevice device) { public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")"); if (VDBG) log("getPriority(" + device + ")");
if (mService != null && isEnabled() final IBluetoothA2dpSink service = mService;
&& isValidDevice(device)) { if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.PRIORITY_OFF; return BluetoothProfile.PRIORITY_OFF;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.PRIORITY_OFF; return BluetoothProfile.PRIORITY_OFF;
} }
@@ -442,16 +446,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
* @param device BluetoothDevice device * @param device BluetoothDevice device
*/ */
public boolean isA2dpPlaying(BluetoothDevice device) { public boolean isA2dpPlaying(BluetoothDevice device) {
if (mService != null && isEnabled() final IBluetoothA2dpSink service = mService;
&& isValidDevice(device)) { if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.isA2dpPlaying(device); return service.isA2dpPlaying(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -485,7 +489,6 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) Log.d(TAG, "Proxy object connected"); if (DBG) Log.d(TAG, "Proxy object connected");
mService = IBluetoothA2dpSink.Stub.asInterface(Binder.allowBlocking(service)); mService = IBluetoothA2dpSink.Stub.asInterface(Binder.allowBlocking(service));
if (mServiceListener != null) { if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK, mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK,
BluetoothA2dpSink.this); BluetoothA2dpSink.this);
@@ -502,15 +505,11 @@ public final class BluetoothA2dpSink implements BluetoothProfile {
}; };
private boolean isEnabled() { private boolean isEnabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; return mAdapter.getState() == BluetoothAdapter.STATE_ON;
return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
private static void log(String msg) { private static void log(String msg) {

View File

@@ -81,7 +81,7 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
private Context mContext; private Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private IBluetoothAvrcpController mService; private volatile IBluetoothAvrcpController mService;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
@@ -179,15 +179,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()"); if (VDBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothAvrcpController service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -197,15 +198,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()"); if (VDBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothAvrcpController service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -215,16 +217,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getState(" + device + ")"); if (VDBG) log("getState(" + device + ")");
if (mService != null && isEnabled() final IBluetoothAvrcpController service = mService;
&& isValidDevice(device)) { if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -236,9 +238,10 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
public BluetoothAvrcpPlayerSettings getPlayerSettings(BluetoothDevice device) { public BluetoothAvrcpPlayerSettings getPlayerSettings(BluetoothDevice device) {
if (DBG) Log.d(TAG, "getPlayerSettings"); if (DBG) Log.d(TAG, "getPlayerSettings");
BluetoothAvrcpPlayerSettings settings = null; BluetoothAvrcpPlayerSettings settings = null;
if (mService != null && isEnabled()) { final IBluetoothAvrcpController service = mService;
if (service != null && isEnabled()) {
try { try {
settings = mService.getPlayerSettings(device); settings = service.getPlayerSettings(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Error talking to BT service in getMetadata() " + e); Log.e(TAG, "Error talking to BT service in getMetadata() " + e);
return null; return null;
@@ -253,15 +256,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
*/ */
public boolean setPlayerApplicationSetting(BluetoothAvrcpPlayerSettings plAppSetting) { public boolean setPlayerApplicationSetting(BluetoothAvrcpPlayerSettings plAppSetting) {
if (DBG) Log.d(TAG, "setPlayerApplicationSetting"); if (DBG) Log.d(TAG, "setPlayerApplicationSetting");
if (mService != null && isEnabled()) { final IBluetoothAvrcpController service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.setPlayerApplicationSetting(plAppSetting); return service.setPlayerApplicationSetting(plAppSetting);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Error talking to BT service in setPlayerApplicationSetting() " + e); Log.e(TAG, "Error talking to BT service in setPlayerApplicationSetting() " + e);
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -272,23 +276,23 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
public void sendGroupNavigationCmd(BluetoothDevice device, int keyCode, int keyState) { public void sendGroupNavigationCmd(BluetoothDevice device, int keyCode, int keyState) {
Log.d(TAG, "sendGroupNavigationCmd dev = " + device + " key " + keyCode + " State = " Log.d(TAG, "sendGroupNavigationCmd dev = " + device + " key " + keyCode + " State = "
+ keyState); + keyState);
if (mService != null && isEnabled()) { final IBluetoothAvrcpController service = mService;
if (service != null && isEnabled()) {
try { try {
mService.sendGroupNavigationCmd(device, keyCode, keyState); service.sendGroupNavigationCmd(device, keyCode, keyState);
return; return;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Error talking to BT service in sendGroupNavigationCmd()", e); Log.e(TAG, "Error talking to BT service in sendGroupNavigationCmd()", e);
return; return;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
} }
private final ServiceConnection mConnection = new ServiceConnection() { private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) Log.d(TAG, "Proxy object connected"); if (DBG) Log.d(TAG, "Proxy object connected");
mService = IBluetoothAvrcpController.Stub.asInterface(Binder.allowBlocking(service)); mService = IBluetoothAvrcpController.Stub.asInterface(Binder.allowBlocking(service));
if (mServiceListener != null) { if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothProfile.AVRCP_CONTROLLER, mServiceListener.onServiceConnected(BluetoothProfile.AVRCP_CONTROLLER,
BluetoothAvrcpController.this); BluetoothAvrcpController.this);
@@ -305,15 +309,11 @@ public final class BluetoothAvrcpController implements BluetoothProfile {
}; };
private boolean isEnabled() { private boolean isEnabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; return mAdapter.getState() == BluetoothAdapter.STATE_ON;
return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
private static void log(String msg) { private static void log(String msg) {

View File

@@ -712,7 +712,7 @@ public final class BluetoothDevice implements Parcelable {
* getService() called. * getService() called.
* TODO: Unify implementation of sService amongst BluetoothFoo API's * TODO: Unify implementation of sService amongst BluetoothFoo API's
*/ */
private static IBluetooth sService; private static volatile IBluetooth sService;
private final String mAddress; private final String mAddress;
@@ -839,12 +839,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public String getName() { public String getName() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot get Remote Device name"); Log.e(TAG, "BT not enabled. Cannot get Remote Device name");
return null; return null;
} }
try { try {
return sService.getRemoteName(this); return service.getRemoteName(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -859,12 +860,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public int getType() { public int getType() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot get Remote Device type"); Log.e(TAG, "BT not enabled. Cannot get Remote Device type");
return DEVICE_TYPE_UNKNOWN; return DEVICE_TYPE_UNKNOWN;
} }
try { try {
return sService.getRemoteType(this); return service.getRemoteType(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -879,12 +881,13 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public String getAlias() { public String getAlias() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias"); Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias");
return null; return null;
} }
try { try {
return sService.getRemoteAlias(this); return service.getRemoteAlias(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -902,12 +905,13 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean setAlias(String alias) { public boolean setAlias(String alias) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); Log.e(TAG, "BT not enabled. Cannot set Remote Device name");
return false; return false;
} }
try { try {
return sService.setRemoteAlias(this, alias); return service.setRemoteAlias(this, alias);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -942,12 +946,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public int getBatteryLevel() { public int getBatteryLevel() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level"); Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level");
return BATTERY_LEVEL_UNKNOWN; return BATTERY_LEVEL_UNKNOWN;
} }
try { try {
return sService.getBatteryLevel(this); return service.getBatteryLevel(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -966,7 +971,8 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN)
public boolean createBond() { public boolean createBond() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");
return false; return false;
} }
@@ -974,7 +980,7 @@ public final class BluetoothDevice implements Parcelable {
Log.i(TAG, "createBond() for device " + getAddress() Log.i(TAG, "createBond() for device " + getAddress()
+ " called by pid: " + Process.myPid() + " called by pid: " + Process.myPid()
+ " tid: " + Process.myTid()); + " tid: " + Process.myTid());
return sService.createBond(this, TRANSPORT_AUTO); return service.createBond(this, TRANSPORT_AUTO);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -998,7 +1004,8 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean createBond(int transport) { public boolean createBond(int transport) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device");
return false; return false;
} }
@@ -1009,7 +1016,7 @@ public final class BluetoothDevice implements Parcelable {
Log.i(TAG, "createBond() for device " + getAddress() Log.i(TAG, "createBond() for device " + getAddress()
+ " called by pid: " + Process.myPid() + " called by pid: " + Process.myPid()
+ " tid: " + Process.myTid()); + " tid: " + Process.myTid());
return sService.createBond(this, transport); return service.createBond(this, transport);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1035,8 +1042,13 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean createBondOutOfBand(int transport, OobData oobData) { public boolean createBondOutOfBand(int transport, OobData oobData) {
final IBluetooth service = sService;
if (service == null) {
Log.w(TAG, "BT not enabled, createBondOutOfBand failed");
return false;
}
try { try {
return sService.createBondOutOfBand(this, transport, oobData); return service.createBondOutOfBand(this, transport, oobData);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1045,8 +1057,13 @@ public final class BluetoothDevice implements Parcelable {
/** @hide */ /** @hide */
public boolean isBondingInitiatedLocally() { public boolean isBondingInitiatedLocally() {
final IBluetooth service = sService;
if (service == null) {
Log.w(TAG, "BT not enabled, isBondingInitiatedLocally failed");
return false;
}
try { try {
return sService.isBondingInitiatedLocally(this); return service.isBondingInitiatedLocally(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1082,7 +1099,8 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean cancelBondProcess() { public boolean cancelBondProcess() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond"); Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond");
return false; return false;
} }
@@ -1090,7 +1108,7 @@ public final class BluetoothDevice implements Parcelable {
Log.i(TAG, "cancelBondProcess() for device " + getAddress() Log.i(TAG, "cancelBondProcess() for device " + getAddress()
+ " called by pid: " + Process.myPid() + " called by pid: " + Process.myPid()
+ " tid: " + Process.myTid()); + " tid: " + Process.myTid());
return sService.cancelBondProcess(this); return service.cancelBondProcess(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1108,7 +1126,8 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean removeBond() { public boolean removeBond() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond"); Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond");
return false; return false;
} }
@@ -1116,7 +1135,7 @@ public final class BluetoothDevice implements Parcelable {
Log.i(TAG, "removeBond() for device " + getAddress() Log.i(TAG, "removeBond() for device " + getAddress()
+ " called by pid: " + Process.myPid() + " called by pid: " + Process.myPid()
+ " tid: " + Process.myTid()); + " tid: " + Process.myTid());
return sService.removeBond(this); return service.removeBond(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1134,19 +1153,15 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public int getBondState() { public int getBondState() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot get bond state"); Log.e(TAG, "BT not enabled. Cannot get bond state");
return BOND_NONE; return BOND_NONE;
} }
try { try {
return sService.getBondState(this); return service.getBondState(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} catch (NullPointerException npe) {
// Handle case where bluetooth service proxy
// is already null.
Log.e(TAG, "NullPointerException for getBondState() of device ("
+ getAddress() + ")", npe);
} }
return BOND_NONE; return BOND_NONE;
} }
@@ -1160,12 +1175,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@SystemApi @SystemApi
public boolean isConnected() { public boolean isConnected() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
// BT is not enabled, we cannot be connected. // BT is not enabled, we cannot be connected.
return false; return false;
} }
try { try {
return sService.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED; return service.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
return false; return false;
@@ -1182,12 +1198,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@SystemApi @SystemApi
public boolean isEncrypted() { public boolean isEncrypted() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
// BT is not enabled, we cannot be connected. // BT is not enabled, we cannot be connected.
return false; return false;
} }
try { try {
return sService.getConnectionState(this) > CONNECTION_STATE_CONNECTED; return service.getConnectionState(this) > CONNECTION_STATE_CONNECTED;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
return false; return false;
@@ -1201,12 +1218,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public BluetoothClass getBluetoothClass() { public BluetoothClass getBluetoothClass() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class"); Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class");
return null; return null;
} }
try { try {
int classInt = sService.getRemoteClass(this); int classInt = service.getRemoteClass(this);
if (classInt == BluetoothClass.ERROR) return null; if (classInt == BluetoothClass.ERROR) return null;
return new BluetoothClass(classInt); return new BluetoothClass(classInt);
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -1227,12 +1245,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public ParcelUuid[] getUuids() { public ParcelUuid[] getUuids() {
if (sService == null || !isBluetoothEnabled()) { final IBluetooth service = sService;
if (service == null || !isBluetoothEnabled()) {
Log.e(TAG, "BT not enabled. Cannot get remote device Uuids"); Log.e(TAG, "BT not enabled. Cannot get remote device Uuids");
return null; return null;
} }
try { try {
return sService.getRemoteUuids(this); return service.getRemoteUuids(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1254,7 +1273,7 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
public boolean fetchUuidsWithSdp() { public boolean fetchUuidsWithSdp() {
IBluetooth service = sService; final IBluetooth service = sService;
if (service == null || !isBluetoothEnabled()) { if (service == null || !isBluetoothEnabled()) {
Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp"); Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp");
return false; return false;
@@ -1289,12 +1308,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
/** @hide */ /** @hide */
public boolean sdpSearch(ParcelUuid uuid) { public boolean sdpSearch(ParcelUuid uuid) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot query remote device sdp records"); Log.e(TAG, "BT not enabled. Cannot query remote device sdp records");
return false; return false;
} }
try { try {
return sService.sdpSearch(this, uuid); return service.sdpSearch(this, uuid);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1308,12 +1328,13 @@ public final class BluetoothDevice implements Parcelable {
* @return true pin has been set false for error * @return true pin has been set false for error
*/ */
public boolean setPin(byte[] pin) { public boolean setPin(byte[] pin) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot set Remote Device pin"); Log.e(TAG, "BT not enabled. Cannot set Remote Device pin");
return false; return false;
} }
try { try {
return sService.setPin(this, true, pin.length, pin); return service.setPin(this, true, pin.length, pin);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1337,12 +1358,13 @@ public final class BluetoothDevice implements Parcelable {
*/ */
@RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED)
public boolean setPairingConfirmation(boolean confirm) { public boolean setPairingConfirmation(boolean confirm) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot set pairing confirmation"); Log.e(TAG, "BT not enabled. Cannot set pairing confirmation");
return false; return false;
} }
try { try {
return sService.setPairingConfirmation(this, confirm); return service.setPairingConfirmation(this, confirm);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1361,12 +1383,13 @@ public final class BluetoothDevice implements Parcelable {
/** @hide */ /** @hide */
public boolean cancelPairingUserInput() { public boolean cancelPairingUserInput() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
Log.e(TAG, "BT not enabled. Cannot create pairing user input"); Log.e(TAG, "BT not enabled. Cannot create pairing user input");
return false; return false;
} }
try { try {
return sService.cancelBondProcess(this); return service.cancelBondProcess(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1400,11 +1423,12 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public int getPhonebookAccessPermission() { public int getPhonebookAccessPermission() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
return ACCESS_UNKNOWN; return ACCESS_UNKNOWN;
} }
try { try {
return sService.getPhonebookAccessPermission(this); return service.getPhonebookAccessPermission(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1421,11 +1445,12 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean setPhonebookAccessPermission(int value) { public boolean setPhonebookAccessPermission(int value) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
return false; return false;
} }
try { try {
return sService.setPhonebookAccessPermission(this, value); return service.setPhonebookAccessPermission(this, value);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1440,11 +1465,12 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public int getMessageAccessPermission() { public int getMessageAccessPermission() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
return ACCESS_UNKNOWN; return ACCESS_UNKNOWN;
} }
try { try {
return sService.getMessageAccessPermission(this); return service.getMessageAccessPermission(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1461,11 +1487,12 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean setMessageAccessPermission(int value) { public boolean setMessageAccessPermission(int value) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
return false; return false;
} }
try { try {
return sService.setMessageAccessPermission(this, value); return service.setMessageAccessPermission(this, value);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1480,11 +1507,12 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public int getSimAccessPermission() { public int getSimAccessPermission() {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
return ACCESS_UNKNOWN; return ACCESS_UNKNOWN;
} }
try { try {
return sService.getSimAccessPermission(this); return service.getSimAccessPermission(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }
@@ -1501,11 +1529,12 @@ public final class BluetoothDevice implements Parcelable {
* @hide * @hide
*/ */
public boolean setSimAccessPermission(int value) { public boolean setSimAccessPermission(int value) {
if (sService == null) { final IBluetooth service = sService;
if (service == null) {
return false; return false;
} }
try { try {
return sService.setSimAccessPermission(this, value); return service.setSimAccessPermission(this, value);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); Log.e(TAG, "", e);
} }

View File

@@ -306,7 +306,7 @@ public final class BluetoothHeadset implements BluetoothProfile {
private Context mContext; private Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private IBluetoothHeadset mService; private volatile IBluetoothHeadset mService;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
@@ -418,15 +418,16 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")"); if (DBG) log("connect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.connect(device); return service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -457,15 +458,16 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")"); if (DBG) log("disconnect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -475,15 +477,16 @@ public final class BluetoothHeadset implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()"); if (VDBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -493,15 +496,16 @@ public final class BluetoothHeadset implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()"); if (VDBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -511,15 +515,16 @@ public final class BluetoothHeadset implements BluetoothProfile {
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getConnectionState(" + device + ")"); if (VDBG) log("getConnectionState(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -540,19 +545,20 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")"); if (DBG) log("setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -571,15 +577,16 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public int getPriority(BluetoothDevice device) { public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")"); if (VDBG) log("getPriority(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF; return PRIORITY_OFF;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return PRIORITY_OFF; return PRIORITY_OFF;
} }
@@ -605,14 +612,15 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean startVoiceRecognition(BluetoothDevice device) { public boolean startVoiceRecognition(BluetoothDevice device) {
if (DBG) log("startVoiceRecognition()"); if (DBG) log("startVoiceRecognition()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.startVoiceRecognition(device); return service.startVoiceRecognition(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -627,14 +635,15 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean stopVoiceRecognition(BluetoothDevice device) { public boolean stopVoiceRecognition(BluetoothDevice device) {
if (DBG) log("stopVoiceRecognition()"); if (DBG) log("stopVoiceRecognition()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.stopVoiceRecognition(device); return service.stopVoiceRecognition(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -648,14 +657,15 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean isAudioConnected(BluetoothDevice device) { public boolean isAudioConnected(BluetoothDevice device) {
if (VDBG) log("isAudioConnected()"); if (VDBG) log("isAudioConnected()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.isAudioConnected(device); return service.isAudioConnected(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -674,14 +684,15 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public int getBatteryUsageHint(BluetoothDevice device) { public int getBatteryUsageHint(BluetoothDevice device) {
if (VDBG) log("getBatteryUsageHint()"); if (VDBG) log("getBatteryUsageHint()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getBatteryUsageHint(device); return service.getBatteryUsageHint(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return -1; return -1;
} }
@@ -704,9 +715,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean acceptIncomingConnect(BluetoothDevice device) { public boolean acceptIncomingConnect(BluetoothDevice device) {
if (DBG) log("acceptIncomingConnect"); if (DBG) log("acceptIncomingConnect");
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.acceptIncomingConnect(device); return service.acceptIncomingConnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -724,9 +736,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean rejectIncomingConnect(BluetoothDevice device) { public boolean rejectIncomingConnect(BluetoothDevice device) {
if (DBG) log("rejectIncomingConnect"); if (DBG) log("rejectIncomingConnect");
if (mService != null) { final IBluetoothHeadset service = mService;
if (service != null) {
try { try {
return mService.rejectIncomingConnect(device); return service.rejectIncomingConnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -745,9 +758,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public int getAudioState(BluetoothDevice device) { public int getAudioState(BluetoothDevice device) {
if (VDBG) log("getAudioState"); if (VDBG) log("getAudioState");
if (mService != null && !isDisabled()) { final IBluetoothHeadset service = mService;
if (service != null && !isDisabled()) {
try { try {
return mService.getAudioState(device); return service.getAudioState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -770,9 +784,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public void setAudioRouteAllowed(boolean allowed) { public void setAudioRouteAllowed(boolean allowed) {
if (VDBG) log("setAudioRouteAllowed"); if (VDBG) log("setAudioRouteAllowed");
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
mService.setAudioRouteAllowed(allowed); service.setAudioRouteAllowed(allowed);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -790,9 +805,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean getAudioRouteAllowed() { public boolean getAudioRouteAllowed() {
if (VDBG) log("getAudioRouteAllowed"); if (VDBG) log("getAudioRouteAllowed");
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getAudioRouteAllowed(); return service.getAudioRouteAllowed();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -812,9 +828,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public void setForceScoAudio(boolean forced) { public void setForceScoAudio(boolean forced) {
if (VDBG) log("setForceScoAudio " + String.valueOf(forced)); if (VDBG) log("setForceScoAudio " + String.valueOf(forced));
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
mService.setForceScoAudio(forced); service.setForceScoAudio(forced);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -834,14 +851,15 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean isAudioOn() { public boolean isAudioOn() {
if (VDBG) log("isAudioOn()"); if (VDBG) log("isAudioOn()");
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.isAudioOn(); return service.isAudioOn();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -855,9 +873,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
* @hide * @hide
*/ */
public boolean connectAudio() { public boolean connectAudio() {
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.connectAudio(); return service.connectAudio();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -877,9 +896,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
* @hide * @hide
*/ */
public boolean disconnectAudio() { public boolean disconnectAudio() {
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.disconnectAudio(); return service.disconnectAudio();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -903,9 +923,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean startScoUsingVirtualVoiceCall(BluetoothDevice device) { public boolean startScoUsingVirtualVoiceCall(BluetoothDevice device) {
if (DBG) log("startScoUsingVirtualVoiceCall()"); if (DBG) log("startScoUsingVirtualVoiceCall()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.startScoUsingVirtualVoiceCall(device); return service.startScoUsingVirtualVoiceCall(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -926,9 +947,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public boolean stopScoUsingVirtualVoiceCall(BluetoothDevice device) { public boolean stopScoUsingVirtualVoiceCall(BluetoothDevice device) {
if (DBG) log("stopScoUsingVirtualVoiceCall()"); if (DBG) log("stopScoUsingVirtualVoiceCall()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.stopScoUsingVirtualVoiceCall(device); return service.stopScoUsingVirtualVoiceCall(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -949,9 +971,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public void phoneStateChanged(int numActive, int numHeld, int callState, String number, public void phoneStateChanged(int numActive, int numHeld, int callState, String number,
int type) { int type) {
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
mService.phoneStateChanged(numActive, numHeld, callState, number, type); service.phoneStateChanged(numActive, numHeld, callState, number, type);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -968,9 +991,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
*/ */
public void clccResponse(int index, int direction, int status, int mode, boolean mpty, public void clccResponse(int index, int direction, int status, int mode, boolean mpty,
String number, int type) { String number, int type) {
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
mService.clccResponse(index, direction, status, mode, mpty, number, type); service.clccResponse(index, direction, status, mode, mpty, number, type);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -1006,14 +1030,15 @@ public final class BluetoothHeadset implements BluetoothProfile {
if (command == null) { if (command == null) {
throw new IllegalArgumentException("command is null"); throw new IllegalArgumentException("command is null");
} }
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.sendVendorSpecificResultCode(device, command, arg); return service.sendVendorSpecificResultCode(device, command, arg);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return false; return false;
@@ -1027,9 +1052,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
* @hide * @hide
*/ */
public boolean enableWBS() { public boolean enableWBS() {
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.enableWBS(); return service.enableWBS();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -1048,9 +1074,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
* @hide * @hide
*/ */
public boolean disableWBS() { public boolean disableWBS() {
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.disableWBS(); return service.disableWBS();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -1083,9 +1110,10 @@ public final class BluetoothHeadset implements BluetoothProfile {
* @hide * @hide
*/ */
public void bindResponse(int indId, boolean indStatus) { public void bindResponse(int indId, boolean indStatus) {
if (mService != null && isEnabled()) { final IBluetoothHeadset service = mService;
if (service != null && isEnabled()) {
try { try {
mService.bindResponse(indId, indStatus); service.bindResponse(indId, indStatus);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -1115,20 +1143,15 @@ public final class BluetoothHeadset implements BluetoothProfile {
}; };
private boolean isEnabled() { private boolean isEnabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; return mAdapter.getState() == BluetoothAdapter.STATE_ON;
return false;
} }
private boolean isDisabled() { private boolean isDisabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_OFF) return true; return mAdapter.getState() == BluetoothAdapter.STATE_OFF;
return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
private static void log(String msg) { private static void log(String msg) {

View File

@@ -76,8 +76,8 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
* Intent sent whenever audio state changes. * Intent sent whenever audio state changes.
* *
* <p>It includes two mandatory extras: * <p>It includes two mandatory extras:
* {@link BluetoothProfile.EXTRA_STATE}, * {@link BluetoothProfile#EXTRA_STATE},
* {@link BluetoothProfile.EXTRA_PREVIOUS_STATE}, * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE},
* with possible values: * with possible values:
* {@link #STATE_AUDIO_CONNECTING}, * {@link #STATE_AUDIO_CONNECTING},
* {@link #STATE_AUDIO_CONNECTED}, * {@link #STATE_AUDIO_CONNECTED},
@@ -367,7 +367,7 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
private Context mContext; private Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private IBluetoothHeadsetClient mService; private volatile IBluetoothHeadsetClient mService;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
@@ -478,15 +478,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")"); if (DBG) log("connect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.connect(device); return service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -499,15 +500,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")"); if (DBG) log("disconnect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -519,15 +521,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()"); if (VDBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -541,15 +544,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()"); if (VDBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -562,15 +566,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getConnectionState(" + device + ")"); if (VDBG) log("getConnectionState(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -581,19 +586,20 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")"); if (DBG) log("setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -602,15 +608,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public int getPriority(BluetoothDevice device) { public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")"); if (VDBG) log("getPriority(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF; return PRIORITY_OFF;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return PRIORITY_OFF; return PRIORITY_OFF;
} }
@@ -627,14 +634,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean startVoiceRecognition(BluetoothDevice device) { public boolean startVoiceRecognition(BluetoothDevice device) {
if (DBG) log("startVoiceRecognition()"); if (DBG) log("startVoiceRecognition()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.startVoiceRecognition(device); return service.startVoiceRecognition(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -651,14 +659,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean stopVoiceRecognition(BluetoothDevice device) { public boolean stopVoiceRecognition(BluetoothDevice device) {
if (DBG) log("stopVoiceRecognition()"); if (DBG) log("stopVoiceRecognition()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.stopVoiceRecognition(device); return service.stopVoiceRecognition(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -670,14 +679,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public List<BluetoothHeadsetClientCall> getCurrentCalls(BluetoothDevice device) { public List<BluetoothHeadsetClientCall> getCurrentCalls(BluetoothDevice device) {
if (DBG) log("getCurrentCalls()"); if (DBG) log("getCurrentCalls()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getCurrentCalls(device); return service.getCurrentCalls(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return null; return null;
} }
@@ -689,14 +699,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public Bundle getCurrentAgEvents(BluetoothDevice device) { public Bundle getCurrentAgEvents(BluetoothDevice device) {
if (DBG) log("getCurrentCalls()"); if (DBG) log("getCurrentCalls()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getCurrentAgEvents(device); return service.getCurrentAgEvents(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return null; return null;
} }
@@ -711,14 +722,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean acceptCall(BluetoothDevice device, int flag) { public boolean acceptCall(BluetoothDevice device, int flag) {
if (DBG) log("acceptCall()"); if (DBG) log("acceptCall()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.acceptCall(device, flag); return service.acceptCall(device, flag);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -731,14 +743,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean holdCall(BluetoothDevice device) { public boolean holdCall(BluetoothDevice device) {
if (DBG) log("holdCall()"); if (DBG) log("holdCall()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.holdCall(device); return service.holdCall(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -755,14 +768,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean rejectCall(BluetoothDevice device) { public boolean rejectCall(BluetoothDevice device) {
if (DBG) log("rejectCall()"); if (DBG) log("rejectCall()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.rejectCall(device); return service.rejectCall(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -784,14 +798,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) { public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) {
if (DBG) log("terminateCall()"); if (DBG) log("terminateCall()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.terminateCall(device, call); return service.terminateCall(device, call);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -811,14 +826,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean enterPrivateMode(BluetoothDevice device, int index) { public boolean enterPrivateMode(BluetoothDevice device, int index) {
if (DBG) log("enterPrivateMode()"); if (DBG) log("enterPrivateMode()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.enterPrivateMode(device, index); return service.enterPrivateMode(device, index);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -837,14 +853,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean explicitCallTransfer(BluetoothDevice device) { public boolean explicitCallTransfer(BluetoothDevice device) {
if (DBG) log("explicitCallTransfer()"); if (DBG) log("explicitCallTransfer()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.explicitCallTransfer(device); return service.explicitCallTransfer(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -859,14 +876,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) { public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) {
if (DBG) log("dial()"); if (DBG) log("dial()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.dial(device, number); return service.dial(device, number);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return null; return null;
} }
@@ -882,14 +900,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean sendDTMF(BluetoothDevice device, byte code) { public boolean sendDTMF(BluetoothDevice device, byte code) {
if (DBG) log("sendDTMF()"); if (DBG) log("sendDTMF()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.sendDTMF(device, code); return service.sendDTMF(device, code);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -907,14 +926,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean getLastVoiceTagNumber(BluetoothDevice device) { public boolean getLastVoiceTagNumber(BluetoothDevice device) {
if (DBG) log("getLastVoiceTagNumber()"); if (DBG) log("getLastVoiceTagNumber()");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getLastVoiceTagNumber(device); return service.getLastVoiceTagNumber(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -925,9 +945,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public int getAudioState(BluetoothDevice device) { public int getAudioState(BluetoothDevice device) {
if (VDBG) log("getAudioState"); if (VDBG) log("getAudioState");
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getAudioState(device); return service.getAudioState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -947,9 +968,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) { public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) {
if (VDBG) log("setAudioRouteAllowed"); if (VDBG) log("setAudioRouteAllowed");
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
mService.setAudioRouteAllowed(device, allowed); service.setAudioRouteAllowed(device, allowed);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -968,9 +990,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
*/ */
public boolean getAudioRouteAllowed(BluetoothDevice device) { public boolean getAudioRouteAllowed(BluetoothDevice device) {
if (VDBG) log("getAudioRouteAllowed"); if (VDBG) log("getAudioRouteAllowed");
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getAudioRouteAllowed(device); return service.getAudioRouteAllowed(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -991,9 +1014,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
* otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent; * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
*/ */
public boolean connectAudio(BluetoothDevice device) { public boolean connectAudio(BluetoothDevice device) {
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.connectAudio(device); return service.connectAudio(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -1014,9 +1038,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
* otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent; * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
*/ */
public boolean disconnectAudio(BluetoothDevice device) { public boolean disconnectAudio(BluetoothDevice device) {
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.disconnectAudio(device); return service.disconnectAudio(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -1034,9 +1059,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
* @return bundle of AG features; null if no service or AG not connected * @return bundle of AG features; null if no service or AG not connected
*/ */
public Bundle getCurrentAgFeatures(BluetoothDevice device) { public Bundle getCurrentAgFeatures(BluetoothDevice device) {
if (mService != null && isEnabled()) { final IBluetoothHeadsetClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getCurrentAgFeatures(device); return service.getCurrentAgFeatures(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -1048,7 +1074,7 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
} }
private ServiceConnection mConnection = new ServiceConnection() { private final ServiceConnection mConnection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) Log.d(TAG, "Proxy object connected"); if (DBG) Log.d(TAG, "Proxy object connected");
@@ -1071,15 +1097,11 @@ public final class BluetoothHeadsetClient implements BluetoothProfile {
}; };
private boolean isEnabled() { private boolean isEnabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; return mAdapter.getState() == BluetoothAdapter.STATE_ON;
return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
private static void log(String msg) { private static void log(String msg) {

View File

@@ -176,9 +176,10 @@ public final class BluetoothHealth implements BluetoothProfile {
BluetoothHealthAppConfiguration config = BluetoothHealthAppConfiguration config =
new BluetoothHealthAppConfiguration(name, dataType, role, channelType); new BluetoothHealthAppConfiguration(name, dataType, role, channelType);
if (mService != null) { final IBluetoothHealth service = mService;
if (service != null) {
try { try {
result = mService.registerAppConfiguration(config, wrapper); result = service.registerAppConfiguration(config, wrapper);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -200,9 +201,10 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
public boolean unregisterAppConfiguration(BluetoothHealthAppConfiguration config) { public boolean unregisterAppConfiguration(BluetoothHealthAppConfiguration config) {
boolean result = false; boolean result = false;
if (mService != null && isEnabled() && config != null) { final IBluetoothHealth service = mService;
if (service != null && isEnabled() && config != null) {
try { try {
result = mService.unregisterAppConfiguration(config); result = service.unregisterAppConfiguration(config);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -228,9 +230,10 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
public boolean connectChannelToSource(BluetoothDevice device, public boolean connectChannelToSource(BluetoothDevice device,
BluetoothHealthAppConfiguration config) { BluetoothHealthAppConfiguration config) {
if (mService != null && isEnabled() && isValidDevice(device) && config != null) { final IBluetoothHealth service = mService;
if (service != null && isEnabled() && isValidDevice(device) && config != null) {
try { try {
return mService.connectChannelToSource(device, config); return service.connectChannelToSource(device, config);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -256,9 +259,10 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
public boolean connectChannelToSink(BluetoothDevice device, public boolean connectChannelToSink(BluetoothDevice device,
BluetoothHealthAppConfiguration config, int channelType) { BluetoothHealthAppConfiguration config, int channelType) {
if (mService != null && isEnabled() && isValidDevice(device) && config != null) { final IBluetoothHealth service = mService;
if (service != null && isEnabled() && isValidDevice(device) && config != null) {
try { try {
return mService.connectChannelToSink(device, config, channelType); return service.connectChannelToSink(device, config, channelType);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -284,9 +288,10 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
public boolean disconnectChannel(BluetoothDevice device, public boolean disconnectChannel(BluetoothDevice device,
BluetoothHealthAppConfiguration config, int channelId) { BluetoothHealthAppConfiguration config, int channelId) {
if (mService != null && isEnabled() && isValidDevice(device) && config != null) { final IBluetoothHealth service = mService;
if (service != null && isEnabled() && isValidDevice(device) && config != null) {
try { try {
return mService.disconnectChannel(device, config, channelId); return service.disconnectChannel(device, config, channelId);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -312,9 +317,10 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
public ParcelFileDescriptor getMainChannelFd(BluetoothDevice device, public ParcelFileDescriptor getMainChannelFd(BluetoothDevice device,
BluetoothHealthAppConfiguration config) { BluetoothHealthAppConfiguration config) {
if (mService != null && isEnabled() && isValidDevice(device) && config != null) { final IBluetoothHealth service = mService;
if (service != null && isEnabled() && isValidDevice(device) && config != null) {
try { try {
return mService.getMainChannelFd(device, config); return service.getMainChannelFd(device, config);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -341,9 +347,10 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothHealth service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getHealthDeviceConnectionState(device); return service.getHealthDeviceConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -370,15 +377,16 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (mService != null && isEnabled()) { final IBluetoothHealth service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedHealthDevices(); return service.getConnectedHealthDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -401,15 +409,16 @@ public final class BluetoothHealth implements BluetoothProfile {
*/ */
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (mService != null && isEnabled()) { final IBluetoothHealth service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getHealthDevicesMatchingConnectionStates(states); return service.getHealthDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -455,7 +464,7 @@ public final class BluetoothHealth implements BluetoothProfile {
private Context mContext; private Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private IBluetoothHealth mService; private volatile IBluetoothHealth mService;
BluetoothAdapter mAdapter; BluetoothAdapter mAdapter;
/** /**
@@ -540,11 +549,8 @@ public final class BluetoothHealth implements BluetoothProfile {
return false; return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
private boolean checkAppParam(String name, int role, int channelType, private boolean checkAppParam(String name, int role, int channelType,

View File

@@ -222,7 +222,7 @@ public final class BluetoothInputDevice implements BluetoothProfile {
private Context mContext; private Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
private IBluetoothInputDevice mService; private volatile IBluetoothInputDevice mService;
private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
new IBluetoothStateChangeCallback.Stub() { new IBluetoothStateChangeCallback.Stub() {
@@ -331,15 +331,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")"); if (DBG) log("connect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.connect(device); return service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -370,15 +371,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")"); if (DBG) log("disconnect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -388,15 +390,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()"); if (VDBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -406,15 +409,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()"); if (VDBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -424,15 +428,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getState(" + device + ")"); if (VDBG) log("getState(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -453,19 +458,20 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")"); if (DBG) log("setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -484,15 +490,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public int getPriority(BluetoothDevice device) { public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")"); if (VDBG) log("getPriority(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.PRIORITY_OFF; return BluetoothProfile.PRIORITY_OFF;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.PRIORITY_OFF; return BluetoothProfile.PRIORITY_OFF;
} }
@@ -517,18 +524,13 @@ public final class BluetoothInputDevice implements BluetoothProfile {
}; };
private boolean isEnabled() { private boolean isEnabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; return mAdapter.getState() == BluetoothAdapter.STATE_ON;
return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
/** /**
* Initiate virtual unplug for a HID input device. * Initiate virtual unplug for a HID input device.
* *
@@ -540,16 +542,17 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean virtualUnplug(BluetoothDevice device) { public boolean virtualUnplug(BluetoothDevice device) {
if (DBG) log("virtualUnplug(" + device + ")"); if (DBG) log("virtualUnplug(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.virtualUnplug(device); return service.virtualUnplug(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -565,15 +568,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean getProtocolMode(BluetoothDevice device) { public boolean getProtocolMode(BluetoothDevice device) {
if (VDBG) log("getProtocolMode(" + device + ")"); if (VDBG) log("getProtocolMode(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getProtocolMode(device); return service.getProtocolMode(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -588,15 +592,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean setProtocolMode(BluetoothDevice device, int protocolMode) { public boolean setProtocolMode(BluetoothDevice device, int protocolMode) {
if (DBG) log("setProtocolMode(" + device + ")"); if (DBG) log("setProtocolMode(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.setProtocolMode(device, protocolMode); return service.setProtocolMode(device, protocolMode);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -615,19 +620,19 @@ public final class BluetoothInputDevice implements BluetoothProfile {
public boolean getReport(BluetoothDevice device, byte reportType, byte reportId, public boolean getReport(BluetoothDevice device, byte reportType, byte reportId,
int bufferSize) { int bufferSize) {
if (VDBG) { if (VDBG) {
log( log("getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId
"getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId + "bufferSize=" + bufferSize);
+ "bufferSize=" + bufferSize);
} }
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getReport(device, reportType, reportId, bufferSize); return service.getReport(device, reportType, reportId, bufferSize);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -644,15 +649,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean setReport(BluetoothDevice device, byte reportType, String report) { public boolean setReport(BluetoothDevice device, byte reportType, String report) {
if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report); if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report);
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.setReport(device, reportType, report); return service.setReport(device, reportType, report);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -668,15 +674,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean sendData(BluetoothDevice device, String report) { public boolean sendData(BluetoothDevice device, String report) {
if (DBG) log("sendData(" + device + "), report=" + report); if (DBG) log("sendData(" + device + "), report=" + report);
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.sendData(device, report); return service.sendData(device, report);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -691,15 +698,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean getIdleTime(BluetoothDevice device) { public boolean getIdleTime(BluetoothDevice device) {
if (DBG) log("getIdletime(" + device + ")"); if (DBG) log("getIdletime(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getIdleTime(device); return service.getIdleTime(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -715,15 +723,16 @@ public final class BluetoothInputDevice implements BluetoothProfile {
*/ */
public boolean setIdleTime(BluetoothDevice device, byte idleTime) { public boolean setIdleTime(BluetoothDevice device, byte idleTime) {
if (DBG) log("setIdletime(" + device + "), idleTime=" + idleTime); if (DBG) log("setIdletime(" + device + "), idleTime=" + idleTime);
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothInputDevice service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.setIdleTime(device, idleTime); return service.setIdleTime(device, idleTime);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }

View File

@@ -113,7 +113,7 @@ public final class BluetoothInputHost implements BluetoothProfile {
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private IBluetoothInputHost mService; private volatile IBluetoothInputHost mService;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
@@ -202,24 +202,18 @@ public final class BluetoothInputHost implements BluetoothProfile {
} }
}; };
private ServiceConnection mConnection = new ServiceConnection() { private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
Log.d(TAG, "onServiceConnected()"); Log.d(TAG, "onServiceConnected()");
mService = IBluetoothInputHost.Stub.asInterface(service); mService = IBluetoothInputHost.Stub.asInterface(service);
if (mServiceListener != null) { if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothProfile.INPUT_HOST, mServiceListener.onServiceConnected(BluetoothProfile.INPUT_HOST,
BluetoothInputHost.this); BluetoothInputHost.this);
} }
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
Log.d(TAG, "onServiceDisconnected()"); Log.d(TAG, "onServiceDisconnected()");
mService = null; mService = null;
if (mServiceListener != null) { if (mServiceListener != null) {
mServiceListener.onServiceDisconnected(BluetoothProfile.INPUT_HOST); mServiceListener.onServiceDisconnected(BluetoothProfile.INPUT_HOST);
} }
@@ -291,9 +285,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
Log.v(TAG, "getConnectedDevices()"); Log.v(TAG, "getConnectedDevices()");
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -311,9 +306,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
Log.v(TAG, "getDevicesMatchingConnectionStates(): states=" + Arrays.toString(states)); Log.v(TAG, "getDevicesMatchingConnectionStates(): states=" + Arrays.toString(states));
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -331,9 +327,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
Log.v(TAG, "getConnectionState(): device=" + device); Log.v(TAG, "getConnectionState(): device=" + device);
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -370,13 +367,14 @@ public final class BluetoothInputHost implements BluetoothProfile {
return false; return false;
} }
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
BluetoothHidDeviceAppConfiguration config = BluetoothHidDeviceAppConfiguration config =
new BluetoothHidDeviceAppConfiguration(); new BluetoothHidDeviceAppConfiguration();
BluetoothHidDeviceCallbackWrapper cbw = BluetoothHidDeviceCallbackWrapper cbw =
new BluetoothHidDeviceCallbackWrapper(callback); new BluetoothHidDeviceCallbackWrapper(callback);
result = mService.registerApp(config, sdp, inQos, outQos, cbw); result = service.registerApp(config, sdp, inQos, outQos, cbw);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -403,9 +401,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
boolean result = false; boolean result = false;
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
result = mService.unregisterApp(config); result = service.unregisterApp(config);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -427,9 +426,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
public boolean sendReport(BluetoothDevice device, int id, byte[] data) { public boolean sendReport(BluetoothDevice device, int id, byte[] data) {
boolean result = false; boolean result = false;
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
result = mService.sendReport(device, id, data); result = service.sendReport(device, id, data);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -454,9 +454,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
boolean result = false; boolean result = false;
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
result = mService.replyReport(device, type, id, data); result = service.replyReport(device, type, id, data);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -479,9 +480,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
boolean result = false; boolean result = false;
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
result = mService.reportError(device, error); result = service.reportError(device, error);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -502,9 +504,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
boolean result = false; boolean result = false;
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
result = mService.unplug(device); result = service.unplug(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -526,9 +529,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
boolean result = false; boolean result = false;
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
result = mService.connect(device); result = service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -549,9 +553,10 @@ public final class BluetoothInputHost implements BluetoothProfile {
boolean result = false; boolean result = false;
if (mService != null) { final IBluetoothInputHost service = mService;
if (service != null) {
try { try {
result = mService.disconnect(device); result = service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }

View File

@@ -43,7 +43,7 @@ public final class BluetoothMap implements BluetoothProfile {
public static final String ACTION_CONNECTION_STATE_CHANGED = public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED"; "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED";
private IBluetoothMap mService; private volatile IBluetoothMap mService;
private final Context mContext; private final Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
@@ -161,9 +161,10 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public int getState() { public int getState() {
if (VDBG) log("getState()"); if (VDBG) log("getState()");
if (mService != null) { final IBluetoothMap service = mService;
if (service != null) {
try { try {
return mService.getState(); return service.getState();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -182,9 +183,10 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public BluetoothDevice getClient() { public BluetoothDevice getClient() {
if (VDBG) log("getClient()"); if (VDBG) log("getClient()");
if (mService != null) { final IBluetoothMap service = mService;
if (service != null) {
try { try {
return mService.getClient(); return service.getClient();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -202,9 +204,10 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public boolean isConnected(BluetoothDevice device) { public boolean isConnected(BluetoothDevice device) {
if (VDBG) log("isConnected(" + device + ")"); if (VDBG) log("isConnected(" + device + ")");
if (mService != null) { final IBluetoothMap service = mService;
if (service != null) {
try { try {
return mService.isConnected(device); return service.isConnected(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -232,15 +235,16 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")"); if (DBG) log("disconnect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -272,15 +276,16 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (DBG) log("getConnectedDevices()"); if (DBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothMap service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -291,15 +296,16 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (DBG) log("getDevicesMatchingStates()"); if (DBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothMap service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -310,15 +316,16 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (DBG) log("getConnectionState(" + device + ")"); if (DBG) log("getConnectionState(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -335,19 +342,20 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")"); if (DBG) log("setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -363,15 +371,16 @@ public final class BluetoothMap implements BluetoothProfile {
*/ */
public int getPriority(BluetoothDevice device) { public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")"); if (VDBG) log("getPriority(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF; return PRIORITY_OFF;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return PRIORITY_OFF; return PRIORITY_OFF;
} }
@@ -403,13 +412,8 @@ public final class BluetoothMap implements BluetoothProfile {
log("Bluetooth is Not enabled"); log("Bluetooth is Not enabled");
return false; return false;
} }
private static boolean isValidDevice(BluetoothDevice device) {
private boolean isValidDevice(BluetoothDevice device) { return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (device == null) return false;
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
} }

View File

@@ -59,7 +59,7 @@ public final class BluetoothMapClient implements BluetoothProfile {
public static final String EXTRA_SENDER_CONTACT_NAME = public static final String EXTRA_SENDER_CONTACT_NAME =
"android.bluetooth.mapmce.profile.extra.SENDER_CONTACT_NAME"; "android.bluetooth.mapmce.profile.extra.SENDER_CONTACT_NAME";
private IBluetoothMapClient mService; private volatile IBluetoothMapClient mService;
private final Context mContext; private final Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
@@ -176,9 +176,10 @@ public final class BluetoothMapClient implements BluetoothProfile {
*/ */
public boolean isConnected(BluetoothDevice device) { public boolean isConnected(BluetoothDevice device) {
if (VDBG) Log.d(TAG, "isConnected(" + device + ")"); if (VDBG) Log.d(TAG, "isConnected(" + device + ")");
if (mService != null) { final IBluetoothMapClient service = mService;
if (service != null) {
try { try {
return mService.isConnected(device); return service.isConnected(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -195,9 +196,10 @@ public final class BluetoothMapClient implements BluetoothProfile {
*/ */
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (DBG) Log.d(TAG, "connect(" + device + ")" + "for MAPS MCE"); if (DBG) Log.d(TAG, "connect(" + device + ")" + "for MAPS MCE");
if (mService != null) { final IBluetoothMapClient service = mService;
if (service != null) {
try { try {
return mService.connect(device); return service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -216,14 +218,15 @@ public final class BluetoothMapClient implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) Log.d(TAG, "disconnect(" + device + ")"); if (DBG) Log.d(TAG, "disconnect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -235,15 +238,16 @@ public final class BluetoothMapClient implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (DBG) Log.d(TAG, "getConnectedDevices()"); if (DBG) Log.d(TAG, "getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<>(); return new ArrayList<>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<>(); return new ArrayList<>();
} }
@@ -255,15 +259,16 @@ public final class BluetoothMapClient implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (DBG) Log.d(TAG, "getDevicesMatchingStates()"); if (DBG) Log.d(TAG, "getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<>(); return new ArrayList<>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<>(); return new ArrayList<>();
} }
@@ -275,15 +280,16 @@ public final class BluetoothMapClient implements BluetoothProfile {
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (DBG) Log.d(TAG, "getConnectionState(" + device + ")"); if (DBG) Log.d(TAG, "getConnectionState(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -298,19 +304,20 @@ public final class BluetoothMapClient implements BluetoothProfile {
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) Log.d(TAG, "setPriority(" + device + ", " + priority + ")"); if (DBG) Log.d(TAG, "setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -326,15 +333,16 @@ public final class BluetoothMapClient implements BluetoothProfile {
*/ */
public int getPriority(BluetoothDevice device) { public int getPriority(BluetoothDevice device) {
if (VDBG) Log.d(TAG, "getPriority(" + device + ")"); if (VDBG) Log.d(TAG, "getPriority(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF; return PRIORITY_OFF;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return PRIORITY_OFF; return PRIORITY_OFF;
} }
@@ -353,9 +361,10 @@ public final class BluetoothMapClient implements BluetoothProfile {
public boolean sendMessage(BluetoothDevice device, Uri[] contacts, String message, public boolean sendMessage(BluetoothDevice device, Uri[] contacts, String message,
PendingIntent sentIntent, PendingIntent deliveredIntent) { PendingIntent sentIntent, PendingIntent deliveredIntent) {
if (DBG) Log.d(TAG, "sendMessage(" + device + ", " + contacts + ", " + message); if (DBG) Log.d(TAG, "sendMessage(" + device + ", " + contacts + ", " + message);
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.sendMessage(device, contacts, message, sentIntent, deliveredIntent); return service.sendMessage(device, contacts, message, sentIntent, deliveredIntent);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
@@ -372,9 +381,10 @@ public final class BluetoothMapClient implements BluetoothProfile {
*/ */
public boolean getUnreadMessages(BluetoothDevice device) { public boolean getUnreadMessages(BluetoothDevice device) {
if (DBG) Log.d(TAG, "getUnreadMessages(" + device + ")"); if (DBG) Log.d(TAG, "getUnreadMessages(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothMapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getUnreadMessages(device); return service.getUnreadMessages(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
@@ -409,12 +419,8 @@ public final class BluetoothMapClient implements BluetoothProfile {
return false; return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
} }

View File

@@ -123,7 +123,7 @@ public final class BluetoothPan implements BluetoothProfile {
private Context mContext; private Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
private IBluetoothPan mPanService; private volatile IBluetoothPan mPanService;
/** /**
* Create a BluetoothPan proxy object for interacting with the local * Create a BluetoothPan proxy object for interacting with the local
@@ -238,15 +238,16 @@ public final class BluetoothPan implements BluetoothProfile {
*/ */
public boolean connect(BluetoothDevice device) { public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")"); if (DBG) log("connect(" + device + ")");
if (mPanService != null && isEnabled() && isValidDevice(device)) { final IBluetoothPan service = mPanService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mPanService.connect(device); return service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -277,15 +278,16 @@ public final class BluetoothPan implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")"); if (DBG) log("disconnect(" + device + ")");
if (mPanService != null && isEnabled() && isValidDevice(device)) { final IBluetoothPan service = mPanService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mPanService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -295,15 +297,16 @@ public final class BluetoothPan implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()"); if (VDBG) log("getConnectedDevices()");
if (mPanService != null && isEnabled()) { final IBluetoothPan service = mPanService;
if (service != null && isEnabled()) {
try { try {
return mPanService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -313,15 +316,16 @@ public final class BluetoothPan implements BluetoothProfile {
@Override @Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()"); if (VDBG) log("getDevicesMatchingStates()");
if (mPanService != null && isEnabled()) { final IBluetoothPan service = mPanService;
if (service != null && isEnabled()) {
try { try {
return mPanService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -331,25 +335,25 @@ public final class BluetoothPan implements BluetoothProfile {
@Override @Override
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getState(" + device + ")"); if (VDBG) log("getState(" + device + ")");
if (mPanService != null && isEnabled() final IBluetoothPan service = mPanService;
&& isValidDevice(device)) { if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mPanService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mPanService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
public void setBluetoothTethering(boolean value) { public void setBluetoothTethering(boolean value) {
if (DBG) log("setBluetoothTethering(" + value + ")"); if (DBG) log("setBluetoothTethering(" + value + ")");
final IBluetoothPan service = mPanService;
if (mPanService != null && isEnabled()) { if (service != null && isEnabled()) {
try { try {
mPanService.setBluetoothTethering(value); service.setBluetoothTethering(value);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
} }
@@ -358,10 +362,10 @@ public final class BluetoothPan implements BluetoothProfile {
public boolean isTetheringOn() { public boolean isTetheringOn() {
if (VDBG) log("isTetheringOn()"); if (VDBG) log("isTetheringOn()");
final IBluetoothPan service = mPanService;
if (mPanService != null && isEnabled()) { if (service != null && isEnabled()) {
try { try {
return mPanService.isTetheringOn(); return service.isTetheringOn();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
} }
@@ -373,7 +377,6 @@ public final class BluetoothPan implements BluetoothProfile {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) Log.d(TAG, "BluetoothPAN Proxy object connected"); if (DBG) Log.d(TAG, "BluetoothPAN Proxy object connected");
mPanService = IBluetoothPan.Stub.asInterface(Binder.allowBlocking(service)); mPanService = IBluetoothPan.Stub.asInterface(Binder.allowBlocking(service));
if (mServiceListener != null) { if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothProfile.PAN, mServiceListener.onServiceConnected(BluetoothProfile.PAN,
BluetoothPan.this); BluetoothPan.this);
@@ -390,15 +393,11 @@ public final class BluetoothPan implements BluetoothProfile {
}; };
private boolean isEnabled() { private boolean isEnabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; return mAdapter.getState() == BluetoothAdapter.STATE_ON;
return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false; return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
} }
private static void log(String msg) { private static void log(String msg) {

View File

@@ -68,7 +68,7 @@ public class BluetoothPbap {
public static final String PBAP_STATE_CHANGED_ACTION = public static final String PBAP_STATE_CHANGED_ACTION =
"android.bluetooth.pbap.intent.action.PBAP_STATE_CHANGED"; "android.bluetooth.pbap.intent.action.PBAP_STATE_CHANGED";
private IBluetoothPbap mService; private volatile IBluetoothPbap mService;
private final Context mContext; private final Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
@@ -214,9 +214,10 @@ public class BluetoothPbap {
*/ */
public int getState() { public int getState() {
if (VDBG) log("getState()"); if (VDBG) log("getState()");
if (mService != null) { final IBluetoothPbap service = mService;
if (service != null) {
try { try {
return mService.getState(); return service.getState();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -235,9 +236,10 @@ public class BluetoothPbap {
*/ */
public BluetoothDevice getClient() { public BluetoothDevice getClient() {
if (VDBG) log("getClient()"); if (VDBG) log("getClient()");
if (mService != null) { final IBluetoothPbap service = mService;
if (service != null) {
try { try {
return mService.getClient(); return service.getClient();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -255,9 +257,10 @@ public class BluetoothPbap {
*/ */
public boolean isConnected(BluetoothDevice device) { public boolean isConnected(BluetoothDevice device) {
if (VDBG) log("isConnected(" + device + ")"); if (VDBG) log("isConnected(" + device + ")");
if (mService != null) { final IBluetoothPbap service = mService;
if (service != null) {
try { try {
return mService.isConnected(device); return service.isConnected(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -275,9 +278,10 @@ public class BluetoothPbap {
*/ */
public boolean disconnect() { public boolean disconnect() {
if (DBG) log("disconnect()"); if (DBG) log("disconnect()");
if (mService != null) { final IBluetoothPbap service = mService;
if (service != null) {
try { try {
mService.disconnect(); service.disconnect();
return true; return true;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());

View File

@@ -42,7 +42,7 @@ public final class BluetoothPbapClient implements BluetoothProfile {
public static final String ACTION_CONNECTION_STATE_CHANGED = public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.pbap.profile.action.CONNECTION_STATE_CHANGED"; "android.bluetooth.pbap.profile.action.CONNECTION_STATE_CHANGED";
private IBluetoothPbapClient mService; private volatile IBluetoothPbapClient mService;
private final Context mContext; private final Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
@@ -173,15 +173,16 @@ public final class BluetoothPbapClient implements BluetoothProfile {
if (DBG) { if (DBG) {
log("connect(" + device + ") for PBAP Client."); log("connect(" + device + ") for PBAP Client.");
} }
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothPbapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.connect(device); return service.connect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return false; return false;
@@ -197,16 +198,17 @@ public final class BluetoothPbapClient implements BluetoothProfile {
if (DBG) { if (DBG) {
log("disconnect(" + device + ")" + new Exception()); log("disconnect(" + device + ")" + new Exception());
} }
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothPbapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
mService.disconnect(device); service.disconnect(device);
return true; return true;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return false; return false;
@@ -223,15 +225,16 @@ public final class BluetoothPbapClient implements BluetoothProfile {
if (DBG) { if (DBG) {
log("getConnectedDevices()"); log("getConnectedDevices()");
} }
if (mService != null && isEnabled()) { final IBluetoothPbapClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
@@ -247,15 +250,16 @@ public final class BluetoothPbapClient implements BluetoothProfile {
if (DBG) { if (DBG) {
log("getDevicesMatchingStates()"); log("getDevicesMatchingStates()");
} }
if (mService != null && isEnabled()) { final IBluetoothPbapClient service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
@@ -271,15 +275,16 @@ public final class BluetoothPbapClient implements BluetoothProfile {
if (DBG) { if (DBG) {
log("getConnectionState(" + device + ")"); log("getConnectionState(" + device + ")");
} }
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothPbapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
@@ -321,14 +326,8 @@ public final class BluetoothPbapClient implements BluetoothProfile {
return false; return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) { return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
return false;
}
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) {
return true;
}
return false;
} }
/** /**
@@ -339,26 +338,27 @@ public final class BluetoothPbapClient implements BluetoothProfile {
* {@link #PRIORITY_OFF}, * {@link #PRIORITY_OFF},
* *
* @param device Paired bluetooth device * @param device Paired bluetooth device
* @param priority * @param priority Priority of this profile
* @return true if priority is set, false on error * @return true if priority is set, false on error
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) { if (DBG) {
log("setPriority(" + device + ", " + priority + ")"); log("setPriority(" + device + ", " + priority + ")");
} }
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothPbapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return false; return false;
@@ -378,15 +378,16 @@ public final class BluetoothPbapClient implements BluetoothProfile {
if (VDBG) { if (VDBG) {
log("getPriority(" + device + ")"); log("getPriority(" + device + ")");
} }
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothPbapClient service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF; return PRIORITY_OFF;
} }
} }
if (mService == null) { if (service == null) {
Log.w(TAG, "Proxy not attached to service"); Log.w(TAG, "Proxy not attached to service");
} }
return PRIORITY_OFF; return PRIORITY_OFF;

View File

@@ -68,7 +68,7 @@ public final class BluetoothSap implements BluetoothProfile {
public static final String ACTION_CONNECTION_STATE_CHANGED = public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.sap.profile.action.CONNECTION_STATE_CHANGED"; "android.bluetooth.sap.profile.action.CONNECTION_STATE_CHANGED";
private IBluetoothSap mService; private volatile IBluetoothSap mService;
private final Context mContext; private final Context mContext;
private ServiceListener mServiceListener; private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter; private BluetoothAdapter mAdapter;
@@ -202,9 +202,10 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public int getState() { public int getState() {
if (VDBG) log("getState()"); if (VDBG) log("getState()");
if (mService != null) { final IBluetoothSap service = mService;
if (service != null) {
try { try {
return mService.getState(); return service.getState();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -224,9 +225,10 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public BluetoothDevice getClient() { public BluetoothDevice getClient() {
if (VDBG) log("getClient()"); if (VDBG) log("getClient()");
if (mService != null) { final IBluetoothSap service = mService;
if (service != null) {
try { try {
return mService.getClient(); return service.getClient();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -246,9 +248,10 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public boolean isConnected(BluetoothDevice device) { public boolean isConnected(BluetoothDevice device) {
if (VDBG) log("isConnected(" + device + ")"); if (VDBG) log("isConnected(" + device + ")");
if (mService != null) { final IBluetoothSap service = mService;
if (service != null) {
try { try {
return mService.isConnected(device); return service.isConnected(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@@ -279,15 +282,16 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public boolean disconnect(BluetoothDevice device) { public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")"); if (DBG) log("disconnect(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothSap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.disconnect(device); return service.disconnect(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -299,15 +303,16 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public List<BluetoothDevice> getConnectedDevices() { public List<BluetoothDevice> getConnectedDevices() {
if (DBG) log("getConnectedDevices()"); if (DBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) { final IBluetoothSap service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getConnectedDevices(); return service.getConnectedDevices();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -319,15 +324,16 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (DBG) log("getDevicesMatchingStates()"); if (DBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) { final IBluetoothSap service = mService;
if (service != null && isEnabled()) {
try { try {
return mService.getDevicesMatchingConnectionStates(states); return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>(); return new ArrayList<BluetoothDevice>();
} }
@@ -339,15 +345,16 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public int getConnectionState(BluetoothDevice device) { public int getConnectionState(BluetoothDevice device) {
if (DBG) log("getConnectionState(" + device + ")"); if (DBG) log("getConnectionState(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothSap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getConnectionState(device); return service.getConnectionState(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED; return BluetoothProfile.STATE_DISCONNECTED;
} }
@@ -363,19 +370,20 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public boolean setPriority(BluetoothDevice device, int priority) { public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")"); if (DBG) log("setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothSap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF if (priority != BluetoothProfile.PRIORITY_OFF
&& priority != BluetoothProfile.PRIORITY_ON) { && priority != BluetoothProfile.PRIORITY_ON) {
return false; return false;
} }
try { try {
return mService.setPriority(device, priority); return service.setPriority(device, priority);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false; return false;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return false; return false;
} }
@@ -388,19 +396,20 @@ public final class BluetoothSap implements BluetoothProfile {
*/ */
public int getPriority(BluetoothDevice device) { public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")"); if (VDBG) log("getPriority(" + device + ")");
if (mService != null && isEnabled() && isValidDevice(device)) { final IBluetoothSap service = mService;
if (service != null && isEnabled() && isValidDevice(device)) {
try { try {
return mService.getPriority(device); return service.getPriority(device);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable())); Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF; return PRIORITY_OFF;
} }
} }
if (mService == null) Log.w(TAG, "Proxy not attached to service"); if (service == null) Log.w(TAG, "Proxy not attached to service");
return PRIORITY_OFF; return PRIORITY_OFF;
} }
private ServiceConnection mConnection = new ServiceConnection() { private final ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) log("Proxy object connected"); if (DBG) log("Proxy object connected");
mService = IBluetoothSap.Stub.asInterface(Binder.allowBlocking(service)); mService = IBluetoothSap.Stub.asInterface(Binder.allowBlocking(service));
@@ -432,15 +441,8 @@ public final class BluetoothSap implements BluetoothProfile {
return false; return false;
} }
private boolean isValidDevice(BluetoothDevice device) { private static boolean isValidDevice(BluetoothDevice device) {
if (device == null) { return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
return false;
}
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) {
return true;
}
return false;
} }
} }