summaryrefslogtreecommitdiffstats
path: root/core/java/android/server/BluetoothDeviceService.java
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-02-10 15:44:00 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-02-10 15:44:00 -0800
commitd24b8183b93e781080b2c16c487e60d51c12da31 (patch)
treefbb89154858984eb8e41556da7e9433040d55cd4 /core/java/android/server/BluetoothDeviceService.java
parentf1e484acb594a726fb57ad0ae4cfe902c7f35858 (diff)
downloadframeworks_base-d24b8183b93e781080b2c16c487e60d51c12da31.zip
frameworks_base-d24b8183b93e781080b2c16c487e60d51c12da31.tar.gz
frameworks_base-d24b8183b93e781080b2c16c487e60d51c12da31.tar.bz2
auto import from //branches/cupcake/...@130745
Diffstat (limited to 'core/java/android/server/BluetoothDeviceService.java')
-rw-r--r--core/java/android/server/BluetoothDeviceService.java337
1 files changed, 63 insertions, 274 deletions
diff --git a/core/java/android/server/BluetoothDeviceService.java b/core/java/android/server/BluetoothDeviceService.java
index 3ce34c3..d149761 100644
--- a/core/java/android/server/BluetoothDeviceService.java
+++ b/core/java/android/server/BluetoothDeviceService.java
@@ -91,8 +91,6 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
mIsDiscovering = false;
mEventLoop = new BluetoothEventLoop(mContext, this);
registerForAirplaneMode();
-
- disableEsco(); // TODO: enable eSCO support once its fully supported
}
private native void initializeNativeDataNative();
@@ -130,10 +128,21 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
}
mEventLoop.stop();
disableNative();
+
+ // mark in progress bondings as cancelled
+ for (String address : mBondState.listInState(BluetoothDevice.BOND_BONDING)) {
+ mBondState.setBondState(address, BluetoothDevice.BOND_NOT_BONDED,
+ BluetoothDevice.UNBOND_REASON_AUTH_CANCELED);
+ }
+ // update mode
+ Intent intent = new Intent(BluetoothIntent.SCAN_MODE_CHANGED_ACTION);
+ intent.putExtra(BluetoothIntent.SCAN_MODE, BluetoothDevice.SCAN_MODE_NONE);
+ mContext.sendBroadcast(intent, BLUETOOTH_PERM);
+
mIsEnabled = false;
- Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.BLUETOOTH_ON, 0);
+ persistBluetoothOnSetting(false);
mIsDiscovering = false;
- Intent intent = new Intent(BluetoothIntent.DISABLED_ACTION);
+ intent = new Intent(BluetoothIntent.DISABLED_ACTION);
mContext.sendBroadcast(intent, BLUETOOTH_PERM);
return true;
}
@@ -202,18 +211,27 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
if (res) {
mIsEnabled = true;
- Settings.Secure.putInt(mContext.getContentResolver(),
- Settings.Secure.BLUETOOTH_ON, 1);
+ persistBluetoothOnSetting(true);
mIsDiscovering = false;
Intent intent = new Intent(BluetoothIntent.ENABLED_ACTION);
mBondState.loadBondState();
mContext.sendBroadcast(intent, BLUETOOTH_PERM);
mHandler.sendMessageDelayed(mHandler.obtainMessage(REGISTER_SDP_RECORDS), 3000);
+
+ // Update mode
+ mEventLoop.onModeChanged(getModeNative());
}
mEnableThread = null;
}
- };
+ }
+ private void persistBluetoothOnSetting(boolean bluetoothOn) {
+ long origCallerIdentityToken = Binder.clearCallingIdentity();
+ Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.BLUETOOTH_ON,
+ bluetoothOn ? 1 : 0);
+ Binder.restoreCallingIdentity(origCallerIdentityToken);
+ }
+
private native int enableNative();
private native int disableNative();
@@ -288,10 +306,10 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
return state.intValue();
}
- public synchronized String[] listBonds() {
+ private synchronized String[] listInState(int state) {
ArrayList<String> result = new ArrayList<String>(mState.size());
for (Map.Entry<String, Integer> e : mState.entrySet()) {
- if (e.getValue().intValue() == BluetoothDevice.BOND_BONDED) {
+ if (e.getValue().intValue() == state) {
result.add(e.getKey());
}
}
@@ -353,18 +371,6 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
}
private native boolean setNameNative(String name);
- public synchronized String getMajorClass() {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- return getMajorClassNative();
- }
- private native String getMajorClassNative();
-
- public synchronized String getMinorClass() {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- return getMinorClassNative();
- }
- private native String getMinorClassNative();
-
/**
* Returns the user-friendly name of a remote device. This value is
* retrned from our local cache, which is updated during device discovery.
@@ -386,24 +392,6 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
/* pacakge */ native String getAdapterPathNative();
- /**
- * Initiate a remote-device-discovery procedure. This procedure may be
- * canceled by calling {@link #stopDiscovery}. Remote-device discoveries
- * are returned as intents
- * <p>
- * Typically, when a remote device is found, your
- * android.bluetooth.DiscoveryEventNotifier#notifyRemoteDeviceFound
- * method will be invoked, and subsequently, your
- * android.bluetooth.RemoteDeviceEventNotifier#notifyRemoteNameUpdated
- * will tell you the user-friendly name of the remote device. However,
- * it is possible that the name update may fail for various reasons, so you
- * should display the device's Bluetooth address as soon as you get a
- * notifyRemoteDeviceFound event, and update the name when you get the
- * remote name.
- *
- * @return true if discovery has started,
- * false otherwise.
- */
public synchronized boolean startDiscovery(boolean resolveNames) {
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
"Need BLUETOOTH_ADMIN permission");
@@ -411,12 +399,6 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
}
private native boolean startDiscoveryNative(boolean resolveNames);
- /**
- * Cancel a remote-device discovery.
- *
- * Note: you may safely call this method even when discovery has not been
- * started.
- */
public synchronized boolean cancelDiscovery() {
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
"Need BLUETOOTH_ADMIN permission");
@@ -492,171 +474,23 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
}
private native boolean isConnectedNative(String address);
- /**
- * Detetermines whether this device is connectable (that is, whether remote
- * devices can connect to it.)
- * <p>
- * Note: A Bluetooth adapter has separate connectable and discoverable
- * states, and you could have any combination of those. Although
- * any combination is possible (such as discoverable but not
- * connectable), we restrict the possible combinations to one of
- * three possibilities: discoverable and connectable, connectable
- * but not discoverable, and neither connectable nor discoverable.
- *
- * @return true if this adapter is connectable
- * false otherwise
- *
- * @see #isDiscoverable
- * @see #getMode
- * @see #setMode
- */
- public synchronized boolean isConnectable() {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- return isConnectableNative();
- }
- private native boolean isConnectableNative();
-
- /**
- * Detetermines whether this device is discoverable.
- *
- * Note: a Bluetooth adapter has separate connectable and discoverable
- * states, and you could have any combination of those. Although
- * any combination is possible (such as discoverable but not
- * connectable), we restrict the possible combinations to one of
- * three possibilities: discoverable and connectable, connectable
- * but not discoverable, and neither connectable nor discoverable.
- *
- * @return true if this adapter is discoverable
- * false otherwise
- *
- * @see #isConnectable
- * @see #getMode
- * @see #setMode
- */
- public synchronized boolean isDiscoverable() {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- return isDiscoverableNative();
- }
- private native boolean isDiscoverableNative();
-
- /**
- * Determines which one of three modes this adapter is in: discoverable and
- * connectable, not discoverable but connectable, or neither.
- *
- * @return Mode enumeration containing the current mode.
- *
- * @see #setMode
- */
- public synchronized int getMode() {
+ public synchronized int getScanMode() {
mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- String mode = getModeNative();
- if (mode == null) {
- return BluetoothDevice.MODE_UNKNOWN;
- }
- if (mode.equalsIgnoreCase("off")) {
- return BluetoothDevice.MODE_OFF;
- }
- else if (mode.equalsIgnoreCase("connectable")) {
- return BluetoothDevice.MODE_CONNECTABLE;
- }
- else if (mode.equalsIgnoreCase("discoverable")) {
- return BluetoothDevice.MODE_DISCOVERABLE;
- }
- else {
- return BluetoothDevice.MODE_UNKNOWN;
- }
+ return bluezStringToScanMode(getModeNative());
}
private native String getModeNative();
- /**
- * Set the discoverability and connectability mode of this adapter. The
- * possibilities are discoverable and connectable (MODE_DISCOVERABLE),
- * connectable but not discoverable (MODE_CONNECTABLE), and neither
- * (MODE_OFF).
- *
- * Note: MODE_OFF does not mean that the adapter is physically off. It
- * may be neither discoverable nor connectable, but it could still
- * initiate outgoing connections, or could participate in a
- * connection initiated by a remote device before its mode was set
- * to MODE_OFF.
- *
- * @param mode the new mode
- * @see #getMode
- */
- public synchronized boolean setMode(int mode) {
+ public synchronized boolean setScanMode(int mode) {
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
"Need BLUETOOTH_ADMIN permission");
- switch (mode) {
- case BluetoothDevice.MODE_OFF:
- return setModeNative("off");
- case BluetoothDevice.MODE_CONNECTABLE:
- return setModeNative("connectable");
- case BluetoothDevice.MODE_DISCOVERABLE:
- return setModeNative("discoverable");
+ String bluezMode = scanModeToBluezString(mode);
+ if (bluezMode != null) {
+ return setModeNative(bluezMode);
}
return false;
}
private native boolean setModeNative(String mode);
- /**
- * Retrieves the alias of a remote device. The alias is a local feature,
- * and allows us to associate a name with a remote device that is different
- * from that remote device's user-friendly name. The remote device knows
- * nothing about this. The alias can be changed with
- * {@link #setRemoteAlias}, and it may be removed with
- * {@link #clearRemoteAlias}
- *
- * @param address Bluetooth address of remote device.
- *
- * @return The alias of the remote device.
- */
- public synchronized String getRemoteAlias(String address) {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- if (!BluetoothDevice.checkBluetoothAddress(address)) {
- return null;
- }
- return getRemoteAliasNative(address);
- }
- private native String getRemoteAliasNative(String address);
-
- /**
- * Changes the alias of a remote device. The alias is a local feature,
- * from that remote device's user-friendly name. The remote device knows
- * nothing about this. The alias can be retrieved with
- * {@link #getRemoteAlias}, and it may be removed with
- * {@link #clearRemoteAlias}.
- *
- * @param address Bluetooth address of remote device
- * @param alias Alias for the remote device
- */
- public synchronized boolean setRemoteAlias(String address, String alias) {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
- "Need BLUETOOTH_ADMIN permission");
- if (alias == null || !BluetoothDevice.checkBluetoothAddress(address)) {
- return false;
- }
- return setRemoteAliasNative(address, alias);
- }
- private native boolean setRemoteAliasNative(String address, String alias);
-
- /**
- * Removes the alias of a remote device. The alias is a local feature,
- * from that remote device's user-friendly name. The remote device knows
- * nothing about this. The alias can be retrieved with
- * {@link #getRemoteAlias}.
- *
- * @param address Bluetooth address of remote device
- */
- public synchronized boolean clearRemoteAlias(String address) {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
- "Need BLUETOOTH_ADMIN permission");
- if (!BluetoothDevice.checkBluetoothAddress(address)) {
- return false;
- }
- return clearRemoteAliasNative(address);
- }
- private native boolean clearRemoteAliasNative(String address);
-
public synchronized boolean disconnectRemoteDeviceAcl(String address) {
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
"Need BLUETOOTH_ADMIN permission");
@@ -699,7 +533,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
}
mBondState.setBondState(address, BluetoothDevice.BOND_NOT_BONDED,
- BluetoothDevice.UNBOND_REASON_CANCELLED);
+ BluetoothDevice.UNBOND_REASON_AUTH_CANCELED);
cancelBondingProcessNative(address);
return true;
}
@@ -717,7 +551,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
public synchronized String[] listBonds() {
mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- return mBondState.listBonds();
+ return mBondState.listInState(BluetoothDevice.BOND_BONDED);
}
public synchronized int getBondState(String address) {
@@ -919,69 +753,6 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
private native String lastUsedNative(String address);
/**
- * Gets the major device class of the specified device.
- * Example: "computer"
- *
- * Note: This is simply a string desciption of the major class of the
- * device-class information, which is returned as a 32-bit value
- * during device discovery.
- *
- * @param address The Bluetooth address of the remote device.
- *
- * @return remote-device major class
- *
- * @see #getRemoteClass
- */
- public synchronized String getRemoteMajorClass(String address) {
- if (!BluetoothDevice.checkBluetoothAddress(address)) {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- return null;
- }
- return getRemoteMajorClassNative(address);
- }
- private native String getRemoteMajorClassNative(String address);
-
- /**
- * Gets the minor device class of the specified device.
- * Example: "laptop"
- *
- * Note: This is simply a string desciption of the minor class of the
- * device-class information, which is returned as a 32-bit value
- * during device discovery.
- *
- * @param address The Bluetooth address of the remote device.
- *
- * @return remote-device minor class
- *
- * @see #getRemoteClass
- */
- public synchronized String getRemoteMinorClass(String address) {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- if (!BluetoothDevice.checkBluetoothAddress(address)) {
- return null;
- }
- return getRemoteMinorClassNative(address);
- }
- private native String getRemoteMinorClassNative(String address);
-
- /**
- * Gets the service classes of the specified device.
- * Example: ["networking", "object transfer"]
- *
- * @return a String array with the descriptions of the service classes.
- *
- * @see #getRemoteClass
- */
- public synchronized String[] getRemoteServiceClasses(String address) {
- mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
- if (!BluetoothDevice.checkBluetoothAddress(address)) {
- return null;
- }
- return getRemoteServiceClassesNative(address);
- }
- private native String[] getRemoteServiceClassesNative(String address);
-
- /**
* Gets the remote major, minor, and service classes encoded as a 32-bit
* integer.
*
@@ -1188,16 +959,6 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
}
- private static final String DISABLE_ESCO_PATH = "/sys/module/sco/parameters/disable_esco";
- private static void disableEsco() {
- try {
- FileWriter file = new FileWriter(DISABLE_ESCO_PATH);
- file.write("Y");
- file.close();
- } catch (FileNotFoundException e) {
- } catch (IOException e) {}
- }
-
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
if (mIsEnabled) {
@@ -1248,6 +1009,34 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub {
pw.println("\nmIsAirplaneSensitive = " + mIsAirplaneSensitive);
}
+ /* package */ static int bluezStringToScanMode(String mode) {
+ if (mode == null) {
+ return BluetoothError.ERROR;
+ }
+ mode = mode.toLowerCase();
+ if (mode.equals("off")) {
+ return BluetoothDevice.SCAN_MODE_NONE;
+ } else if (mode.equals("connectable")) {
+ return BluetoothDevice.SCAN_MODE_CONNECTABLE;
+ } else if (mode.equals("discoverable")) {
+ return BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
+ } else {
+ return BluetoothError.ERROR;
+ }
+ }
+
+ /* package */ static String scanModeToBluezString(int mode) {
+ switch (mode) {
+ case BluetoothDevice.SCAN_MODE_NONE:
+ return "off";
+ case BluetoothDevice.SCAN_MODE_CONNECTABLE:
+ return "connectable";
+ case BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
+ return "discoverable";
+ }
+ return null;
+ }
+
private static void log(String msg) {
Log.d(TAG, msg);
}