Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Fixed review comments
Browse files Browse the repository at this point in the history
- fixed review comments (internal+google)
- corrected tabs/spaces
- Add connection id header for obex client operations
- added support for implementing ProfileService class

Change-Id: Idab8b4fa54a0f31bec4ffa263a69a9850a07f858

Bug:10692365
  • Loading branch information
kschulz-samsung authored and Zhihai Xu committed Sep 13, 2013
1 parent 11f2b42 commit 0d37605
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 77 deletions.
10 changes: 8 additions & 2 deletions core/java/android/bluetooth/BluetoothAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import android.os.ServiceManager;
import android.util.Log;
import android.util.Pair;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand All @@ -51,7 +50,7 @@
* devices, and start a scan for Bluetooth LE devices.
*
* <p>To get a {@link BluetoothAdapter} representing the local Bluetooth
* adapter, when running on JELLY_BEAN_MR1 and below, call the
* adapter, when running on JELLY_BEAN_MR1 and below, call the
* static {@link #getDefaultAdapter} method; when running on JELLY_BEAN_MR2 and
* higher, retrieve it through
* {@link android.content.Context#getSystemService} with
Expand Down Expand Up @@ -1229,6 +1228,9 @@ public boolean getProfileProxy(Context context, BluetoothProfile.ServiceListener
} else if (profile == BluetoothProfile.HEALTH) {
BluetoothHealth health = new BluetoothHealth(context, listener);
return true;
} else if (profile == BluetoothProfile.MAP) {
BluetoothMap map = new BluetoothMap(context, listener);
return true;
} else {
return false;
}
Expand Down Expand Up @@ -1277,6 +1279,10 @@ public void closeProfileProxy(int profile, BluetoothProfile proxy) {
BluetoothGattServer gattServer = (BluetoothGattServer)proxy;
gattServer.close();
break;
case BluetoothProfile.MAP:
BluetoothMap map = (BluetoothMap)proxy;
map.close();
break;
}
}

Expand Down
244 changes: 176 additions & 68 deletions core/java/android/bluetooth/BluetoothMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package android.bluetooth;

import java.util.List;
import java.util.ArrayList;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
Expand All @@ -30,25 +32,14 @@
* Profile.
*@hide
*/
public class BluetoothMap {
public final class BluetoothMap implements BluetoothProfile {

private static final String TAG = "BluetoothMap";
private static final boolean DBG = true;
private static final boolean VDBG = false;

/** int extra for MAP_STATE_CHANGED_ACTION */
public static final String MAP_STATE =
"android.bluetooth.map.intent.MAP_STATE";
/** int extra for MAP_STATE_CHANGED_ACTION */
public static final String MAP_PREVIOUS_STATE =
"android.bluetooth.map.intent.MAP_PREVIOUS_STATE";

/** Indicates the state of a Map connection state has changed.
* This intent will always contain MAP_STATE, MAP_PREVIOUS_STATE and
* BluetoothIntent.ADDRESS extras.
*/
public static final String MAP_STATE_CHANGED_ACTION =
"android.bluetooth.map.intent.action.MAP_STATE_CHANGED";
public static final String ACTION_CONNECTION_STATE_CHANGED =
"android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED";

private IBluetoothMap mService;
private final Context mContext;
Expand All @@ -57,41 +48,12 @@ public class BluetoothMap {

/** There was an error trying to obtain the state */
public static final int STATE_ERROR = -1;
/** No client currently connected */
public static final int STATE_DISCONNECTED = 0;
/** Connection attempt in progress */
public static final int STATE_CONNECTING = 1;
/** Client is currently connected */
public static final int STATE_CONNECTED = 2;

public static final int RESULT_FAILURE = 0;
public static final int RESULT_SUCCESS = 1;
/** Connection canceled before completion. */
public static final int RESULT_CANCELED = 2;

/**
* An interface for notifying Bluetooth PCE IPC clients when they have
* been connected to the BluetoothMap service.
*/
public interface ServiceListener {
/**
* Called to notify the client when this proxy object has been
* connected to the BluetoothMap service. Clients must wait for
* this callback before making IPC calls on the BluetoothMap
* service.
*/
public void onServiceConnected(BluetoothMap proxy);

/**
* Called to notify the client that this proxy object has been
* disconnected from the BluetoothMap service. Clients must not
* make IPC calls on the BluetoothMap service after this callback.
* This callback will currently only occur if the application hosting
* the BluetoothMap service, but may be called more often in future.
*/
public void onServiceDisconnected();
}

final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
new IBluetoothStateChangeCallback.Stub() {
public void onBluetoothStateChange(boolean up) {
Expand All @@ -111,11 +73,7 @@ public void onBluetoothStateChange(boolean up) {
try {
if (mService == null) {
if (VDBG) Log.d(TAG,"Binding service...");
if (!mContext.bindService(
new Intent(IBluetoothMap.class.getName()),
mConnection, 0)) {
Log.e(TAG, "Could not bind to Bluetooth MAP Service");
}
doBind();
}
} catch (Exception re) {
Log.e(TAG,"",re);
Expand All @@ -128,7 +86,8 @@ public void onBluetoothStateChange(boolean up) {
/**
* Create a BluetoothMap proxy object.
*/
public BluetoothMap(Context context, ServiceListener l) {
/*package*/ BluetoothMap(Context context, ServiceListener l) {
if (DBG) Log.d(TAG, "Create BluetoothMap proxy object");
mContext = context;
mServiceListener = l;
mAdapter = BluetoothAdapter.getDefaultAdapter();
Expand All @@ -140,9 +99,18 @@ public BluetoothMap(Context context, ServiceListener l) {
Log.e(TAG,"",e);
}
}
if (!context.bindService(new Intent(IBluetoothMap.class.getName()), mConnection, 0)) {
Log.e(TAG, "Could not bind to Bluetooth Map Service");
doBind();
}

boolean doBind() {
Intent intent = new Intent(IBluetoothMap.class.getName());
ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
intent.setComponent(comp);
if (comp == null || !mContext.bindService(intent, mConnection, 0)) {
Log.e(TAG, "Could not bind to Bluetooth MAP Service with " + intent);
return false;
}
return true;
}

protected void finalize() throws Throwable {
Expand Down Expand Up @@ -221,9 +189,9 @@ public BluetoothDevice getClient() {
}

/**
* Returns true if the specified Bluetooth device is connected (does not
* include connecting). Returns false if not connected, or if this proxy
* object is not currently connected to the Map service.
* Returns true if the specified Bluetooth device is connected.
* Returns false if not connected, or if this proxy object is not
* currently connected to the Map service.
*/
public boolean isConnected(BluetoothDevice device) {
if (VDBG) log("isConnected(" + device + ")");
Expand All @@ -239,21 +207,33 @@ public boolean isConnected(BluetoothDevice device) {
}

/**
* Disconnects the current Map Client. Currently this call blocks,
* it may soon be made asynchronous. Returns false if this proxy object is
* not currently connected to the Map service.
* Initiate connection. Initiation of outgoing connections is not
* supported for MAP server.
*/
public boolean disconnect() {
if (DBG) log("disconnect()");
if (mService != null) {
public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")" + "not supported for MAPS");
return false;
}

/**
* Initiate disconnect.
*
* @param device Remote Bluetooth Device
* @return false on error,
* true otherwise
*/
public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")");
if (mService != null && isEnabled() &&
isValidDevice(device)) {
try {
mService.disconnect();
return true;
} catch (RemoteException e) {Log.e(TAG, e.toString());}
} else {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
return mService.disconnect(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false;
}
}
if (mService == null) Log.w(TAG, "Proxy not attached to service");
return false;
}

Expand All @@ -277,24 +257,152 @@ public static boolean doesClassMatchSink(BluetoothClass btClass) {
}
}

/**
* Get the list of connected devices. Currently at most one.
*
* @return list of connected devices
*/
public List<BluetoothDevice> getConnectedDevices() {
if (DBG) log("getConnectedDevices()");
if (mService != null && isEnabled()) {
try {
return mService.getConnectedDevices();
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>();
}
}
if (mService == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>();
}

/**
* Get the list of devices matching specified states. Currently at most one.
*
* @return list of matching devices
*/
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (DBG) log("getDevicesMatchingStates()");
if (mService != null && isEnabled()) {
try {
return mService.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>();
}
}
if (mService == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>();
}

/**
* Get connection state of device
*
* @return device connection state
*/
public int getConnectionState(BluetoothDevice device) {
if (DBG) log("getConnectionState(" + device + ")");
if (mService != null && isEnabled() &&
isValidDevice(device)) {
try {
return mService.getConnectionState(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED;
}
}
if (mService == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED;
}

/**
* Set priority of the profile
*
* <p> The device should already be paired.
* Priority can be one of {@link #PRIORITY_ON} or
* {@link #PRIORITY_OFF},
*
* @param device Paired bluetooth device
* @param priority
* @return true if priority is set, false on error
*/
public boolean setPriority(BluetoothDevice device, int priority) {
if (DBG) log("setPriority(" + device + ", " + priority + ")");
if (mService != null && isEnabled() &&
isValidDevice(device)) {
if (priority != BluetoothProfile.PRIORITY_OFF &&
priority != BluetoothProfile.PRIORITY_ON) {
return false;
}
try {
return mService.setPriority(device, priority);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return false;
}
}
if (mService == null) Log.w(TAG, "Proxy not attached to service");
return false;
}

/**
* Get the priority of the profile.
*
* <p> The priority can be any of:
* {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
* {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
*
* @param device Bluetooth device
* @return priority of the device
*/
public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")");
if (mService != null && isEnabled() &&
isValidDevice(device)) {
try {
return mService.getPriority(device);
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return PRIORITY_OFF;
}
}
if (mService == null) Log.w(TAG, "Proxy not attached to service");
return PRIORITY_OFF;
}

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) log("Proxy object connected");
mService = IBluetoothMap.Stub.asInterface(service);
if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothMap.this);
mServiceListener.onServiceConnected(BluetoothProfile.MAP, BluetoothMap.this);
}
}
public void onServiceDisconnected(ComponentName className) {
if (DBG) log("Proxy object disconnected");
mService = null;
if (mServiceListener != null) {
mServiceListener.onServiceDisconnected();
mServiceListener.onServiceDisconnected(BluetoothProfile.MAP);
}
}
};

private static void log(String msg) {
Log.d(TAG, msg);
}

private boolean isEnabled() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null && adapter.getState() == BluetoothAdapter.STATE_ON) return true;
log("Bluetooth is Not enabled");
return false;
}
private boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false;

if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
}


}
Loading

0 comments on commit 0d37605

Please sign in to comment.