diff options
author | Nick Pelly <npelly@google.com> | 2009-08-14 18:33:38 -0700 |
---|---|---|
committer | Nick Pelly <npelly@google.com> | 2009-08-18 08:24:22 -0700 |
commit | bd022f423a33f0794bb53e5b0720da2d67e4631c (patch) | |
tree | 4def583f15b783ada3d49866a8cd29bcdb1bbe00 /core/java/android/server | |
parent | 82e7408be29c6c8c6ed80887ea97f48f38b3223d (diff) | |
download | frameworks_base-bd022f423a33f0794bb53e5b0720da2d67e4631c.zip frameworks_base-bd022f423a33f0794bb53e5b0720da2d67e4631c.tar.gz frameworks_base-bd022f423a33f0794bb53e5b0720da2d67e4631c.tar.bz2 |
Bluetooth: API change.
Split BluetoothDevice into BluetoothDevice and BluetoothAdapter.
BluetoothAdapter: Represents the local BT adapter. Operations on the local
adapter (start a scan, etc).
BluetoothDevice: Represents a remote BT device. Operations on remote devices
(pair, connect, etc).
IBluetoothDevice.aidl -> Bluetooth.aidl
BluetoothDeviceService.java -> BluetoothDeviceService.java
TODO:
Javadoc
Diffstat (limited to 'core/java/android/server')
-rw-r--r-- | core/java/android/server/BluetoothA2dpService.java | 164 | ||||
-rw-r--r-- | core/java/android/server/BluetoothEventLoop.java | 33 | ||||
-rw-r--r-- | core/java/android/server/BluetoothService.java (renamed from core/java/android/server/BluetoothDeviceService.java) | 121 |
3 files changed, 162 insertions, 156 deletions
diff --git a/core/java/android/server/BluetoothA2dpService.java b/core/java/android/server/BluetoothA2dpService.java index 96ce9d6..d9fcb53 100644 --- a/core/java/android/server/BluetoothA2dpService.java +++ b/core/java/android/server/BluetoothA2dpService.java @@ -23,6 +23,7 @@ package android.server; import android.bluetooth.BluetoothA2dp; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothError; import android.bluetooth.BluetoothIntent; @@ -40,9 +41,9 @@ import android.util.Log; import java.io.FileDescriptor; import java.io.PrintWriter; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; public class BluetoothA2dpService extends IBluetoothA2dp.Stub { @@ -67,26 +68,27 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { private static int mSinkCount; - private final Context mContext; private final IntentFilter mIntentFilter; - private HashMap<String, Integer> mAudioDevices; + private HashMap<BluetoothDevice, Integer> mAudioDevices; private final AudioManager mAudioManager; - private final BluetoothDeviceService mBluetoothService; + private final BluetoothService mBluetoothService; + private final BluetoothAdapter mAdapter; private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - String address = intent.getStringExtra(BluetoothIntent.ADDRESS); + BluetoothDevice device = + intent.getParcelableExtra(BluetoothIntent.DEVICE); if (action.equals(BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION)) { int state = intent.getIntExtra(BluetoothIntent.BLUETOOTH_STATE, BluetoothError.ERROR); switch (state) { - case BluetoothDevice.BLUETOOTH_STATE_ON: + case BluetoothAdapter.BLUETOOTH_STATE_ON: onBluetoothEnable(); break; - case BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF: + case BluetoothAdapter.BLUETOOTH_STATE_TURNING_OFF: onBluetoothDisable(); break; } @@ -95,28 +97,28 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { BluetoothError.ERROR); switch(bondState) { case BluetoothDevice.BOND_BONDED: - setSinkPriority(address, BluetoothA2dp.PRIORITY_AUTO); + setSinkPriority(device, BluetoothA2dp.PRIORITY_AUTO); break; case BluetoothDevice.BOND_BONDING: case BluetoothDevice.BOND_NOT_BONDED: - setSinkPriority(address, BluetoothA2dp.PRIORITY_OFF); + setSinkPriority(device, BluetoothA2dp.PRIORITY_OFF); break; } } else if (action.equals(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION)) { - if (getSinkPriority(address) > BluetoothA2dp.PRIORITY_OFF && - isSinkDevice(address)) { + if (getSinkPriority(device) > BluetoothA2dp.PRIORITY_OFF && + isSinkDevice(device)) { // This device is a preferred sink. Make an A2DP connection // after a delay. We delay to avoid connection collisions, // and to give other profiles such as HFP a chance to // connect first. - Message msg = Message.obtain(mHandler, MESSAGE_CONNECT_TO, address); + Message msg = Message.obtain(mHandler, MESSAGE_CONNECT_TO, device); mHandler.sendMessageDelayed(msg, 6000); } } } }; - public BluetoothA2dpService(Context context, BluetoothDeviceService bluetoothService) { + public BluetoothA2dpService(Context context, BluetoothService bluetoothService) { mContext = context; mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); @@ -130,12 +132,14 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { throw new RuntimeException("Could not init BluetoothA2dpService"); } + mAdapter = (BluetoothAdapter) context.getSystemService(Context.BLUETOOTH_SERVICE); + mIntentFilter = new IntentFilter(BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION); mIntentFilter.addAction(BluetoothIntent.BOND_STATE_CHANGED_ACTION); mIntentFilter.addAction(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION); mContext.registerReceiver(mReceiver, mIntentFilter); - mAudioDevices = new HashMap<String, Integer>(); + mAudioDevices = new HashMap<BluetoothDevice, Integer>(); if (mBluetoothService.isEnabled()) onBluetoothEnable(); @@ -155,18 +159,18 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_CONNECT_TO: - String address = (String)msg.obj; + BluetoothDevice device = (BluetoothDevice) msg.obj; // check bluetooth is still on, device is still preferred, and // nothing is currently connected if (mBluetoothService.isEnabled() && - getSinkPriority(address) > BluetoothA2dp.PRIORITY_OFF && + getSinkPriority(device) > BluetoothA2dp.PRIORITY_OFF && lookupSinksMatchingStates(new int[] { BluetoothA2dp.STATE_CONNECTING, BluetoothA2dp.STATE_CONNECTED, BluetoothA2dp.STATE_PLAYING, BluetoothA2dp.STATE_DISCONNECTING}).size() == 0) { - log("Auto-connecting A2DP to sink " + address); - connectSink(address); + log("Auto-connecting A2DP to sink " + device); + connectSink(device); } break; } @@ -185,8 +189,8 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { return -1; } - private boolean isSinkDevice(String address) { - String uuids[] = mBluetoothService.getRemoteUuids(address); + private boolean isSinkDevice(BluetoothDevice device) { + String uuids[] = mBluetoothService.getRemoteUuids(device.getAddress()); UUID uuid; if (uuids != null) { for (String deviceUuid: uuids) { @@ -199,11 +203,11 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { return false; } - private synchronized boolean addAudioSink (String address) { - String path = mBluetoothService.getObjectPathFromAddress(address); + private synchronized boolean addAudioSink (BluetoothDevice device) { + String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); String propValues[] = (String []) getSinkPropertiesNative(path); if (propValues == null) { - Log.e(TAG, "Error while getting AudioSink properties for device: " + address); + Log.e(TAG, "Error while getting AudioSink properties for device: " + device); return false; } Integer state = null; @@ -214,8 +218,8 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { break; } } - mAudioDevices.put(address, state); - handleSinkStateChange(address, BluetoothA2dp.STATE_DISCONNECTED, state); + mAudioDevices.put(device, state); + handleSinkStateChange(device, BluetoothA2dp.STATE_DISCONNECTED, state); return true; } @@ -226,6 +230,7 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { String [] paths = devices.split(","); for (String path: paths) { String address = mBluetoothService.getAddressFromObjectPath(path); + BluetoothDevice device = mAdapter.getRemoteDevice(address); String []uuids = mBluetoothService.getRemoteUuids(address); if (uuids != null) for (String uuid: uuids) { @@ -233,7 +238,7 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { if (BluetoothUuid.isAudioSink(remoteUuid) || BluetoothUuid.isAudioSource(remoteUuid) || BluetoothUuid.isAdvAudioDist(remoteUuid)) { - addAudioSink(address); + addAudioSink(device); break; } } @@ -244,36 +249,34 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { private synchronized void onBluetoothDisable() { if (!mAudioDevices.isEmpty()) { - String [] addresses = new String[mAudioDevices.size()]; - addresses = mAudioDevices.keySet().toArray(addresses); - for (String address : addresses) { - int state = getSinkState(address); + BluetoothDevice[] devices = new BluetoothDevice[mAudioDevices.size()]; + devices = mAudioDevices.keySet().toArray(devices); + for (BluetoothDevice device : devices) { + int state = getSinkState(device); switch (state) { case BluetoothA2dp.STATE_CONNECTING: case BluetoothA2dp.STATE_CONNECTED: case BluetoothA2dp.STATE_PLAYING: - disconnectSinkNative(mBluetoothService.getObjectPathFromAddress(address)); - handleSinkStateChange(address,state, BluetoothA2dp.STATE_DISCONNECTED); + disconnectSinkNative(mBluetoothService.getObjectPathFromAddress( + device.getAddress())); + handleSinkStateChange(device, state, BluetoothA2dp.STATE_DISCONNECTED); break; case BluetoothA2dp.STATE_DISCONNECTING: - handleSinkStateChange(address, BluetoothA2dp.STATE_DISCONNECTING, - BluetoothA2dp.STATE_DISCONNECTED); + handleSinkStateChange(device, BluetoothA2dp.STATE_DISCONNECTING, + BluetoothA2dp.STATE_DISCONNECTED); break; } } mAudioDevices.clear(); } - mAudioManager.setParameters(BLUETOOTH_ENABLED+"=false"); + mAudioManager.setParameters(BLUETOOTH_ENABLED + "=false"); } - public synchronized int connectSink(String address) { + public synchronized int connectSink(BluetoothDevice device) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission"); - if (DBG) log("connectSink(" + address + ")"); - if (!BluetoothDevice.checkBluetoothAddress(address)) { - return BluetoothError.ERROR; - } + if (DBG) log("connectSink(" + device + ")"); // ignore if there are any active sinks if (lookupSinksMatchingStates(new int[] { @@ -284,10 +287,10 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { return BluetoothError.ERROR; } - if (mAudioDevices.get(address) == null && !addAudioSink(address)) + if (mAudioDevices.get(device) == null && !addAudioSink(device)) return BluetoothError.ERROR; - int state = mAudioDevices.get(address); + int state = mAudioDevices.get(device); switch (state) { case BluetoothA2dp.STATE_CONNECTED: @@ -298,7 +301,7 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { return BluetoothError.SUCCESS; } - String path = mBluetoothService.getObjectPathFromAddress(address); + String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); if (path == null) return BluetoothError.ERROR; @@ -309,19 +312,17 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { return BluetoothError.SUCCESS; } - public synchronized int disconnectSink(String address) { + public synchronized int disconnectSink(BluetoothDevice device) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission"); - if (DBG) log("disconnectSink(" + address + ")"); - if (!BluetoothDevice.checkBluetoothAddress(address)) { - return BluetoothError.ERROR; - } - String path = mBluetoothService.getObjectPathFromAddress(address); + if (DBG) log("disconnectSink(" + device + ")"); + + String path = mBluetoothService.getObjectPathFromAddress(device.getAddress()); if (path == null) { return BluetoothError.ERROR; } - switch (getSinkState(address)) { + switch (getSinkState(device)) { case BluetoothA2dp.STATE_DISCONNECTED: return BluetoothError.ERROR; case BluetoothA2dp.STATE_DISCONNECTING: @@ -336,41 +337,36 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { } } - public synchronized List<String> listConnectedSinks() { + public synchronized BluetoothDevice[] getConnectedSinks() { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); - return lookupSinksMatchingStates(new int[] {BluetoothA2dp.STATE_CONNECTED, - BluetoothA2dp.STATE_PLAYING}); + Set<BluetoothDevice> sinks = lookupSinksMatchingStates( + new int[] {BluetoothA2dp.STATE_CONNECTED, BluetoothA2dp.STATE_PLAYING}); + return sinks.toArray(new BluetoothDevice[sinks.size()]); } - public synchronized int getSinkState(String address) { + public synchronized int getSinkState(BluetoothDevice device) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); - if (!BluetoothDevice.checkBluetoothAddress(address)) { - return BluetoothError.ERROR; - } - Integer state = mAudioDevices.get(address); + Integer state = mAudioDevices.get(device); if (state == null) return BluetoothA2dp.STATE_DISCONNECTED; return state; } - public synchronized int getSinkPriority(String address) { + public synchronized int getSinkPriority(BluetoothDevice device) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); - if (!BluetoothDevice.checkBluetoothAddress(address)) { - return BluetoothError.ERROR; - } return Settings.Secure.getInt(mContext.getContentResolver(), - Settings.Secure.getBluetoothA2dpSinkPriorityKey(address), + Settings.Secure.getBluetoothA2dpSinkPriorityKey(device.getAddress()), BluetoothA2dp.PRIORITY_OFF); } - public synchronized int setSinkPriority(String address, int priority) { + public synchronized int setSinkPriority(BluetoothDevice device, int priority) { mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission"); - if (!BluetoothDevice.checkBluetoothAddress(address)) { + if (!BluetoothDevice.checkBluetoothAddress(device.getAddress())) { return BluetoothError.ERROR; } return Settings.Secure.putInt(mContext.getContentResolver(), - Settings.Secure.getBluetoothA2dpSinkPriorityKey(address), priority) ? + Settings.Secure.getBluetoothA2dpSinkPriorityKey(device.getAddress()), priority) ? BluetoothError.SUCCESS : BluetoothError.ERROR; } @@ -386,20 +382,22 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { return; } + BluetoothDevice device = mAdapter.getRemoteDevice(address); + if (name.equals(PROPERTY_STATE)) { int state = convertBluezSinkStringtoState(propValues[1]); - if (mAudioDevices.get(address) == null) { + if (mAudioDevices.get(device) == null) { // This is for an incoming connection for a device not known to us. // We have authorized it and bluez state has changed. - addAudioSink(address); + addAudioSink(device); } else { - int prevState = mAudioDevices.get(address); - handleSinkStateChange(address, prevState, state); + int prevState = mAudioDevices.get(device); + handleSinkStateChange(device, prevState, state); } } } - private void handleSinkStateChange(String address, int prevState, int state) { + private void handleSinkStateChange(BluetoothDevice device, int prevState, int state) { if (state != prevState) { if (state == BluetoothA2dp.STATE_DISCONNECTED || state == BluetoothA2dp.STATE_DISCONNECTING) { @@ -413,28 +411,28 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { } else if (state == BluetoothA2dp.STATE_CONNECTED) { mSinkCount ++; } - mAudioDevices.put(address, state); + mAudioDevices.put(device, state); Intent intent = new Intent(BluetoothA2dp.SINK_STATE_CHANGED_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, device); intent.putExtra(BluetoothA2dp.SINK_PREVIOUS_STATE, prevState); intent.putExtra(BluetoothA2dp.SINK_STATE, state); mContext.sendBroadcast(intent, BLUETOOTH_PERM); - if (DBG) log("A2DP state : address: " + address + " State:" + prevState + "->" + state); + if (DBG) log("A2DP state : device: " + device + " State:" + prevState + "->" + state); } } - private synchronized List<String> lookupSinksMatchingStates(int[] states) { - List<String> sinks = new ArrayList<String>(); + private synchronized Set<BluetoothDevice> lookupSinksMatchingStates(int[] states) { + Set<BluetoothDevice> sinks = new HashSet<BluetoothDevice>(); if (mAudioDevices.isEmpty()) { return sinks; } - for (String path: mAudioDevices.keySet()) { - int sinkState = getSinkState(path); + for (BluetoothDevice device: mAudioDevices.keySet()) { + int sinkState = getSinkState(device); for (int state : states) { if (state == sinkState) { - sinks.add(path); + sinks.add(device); break; } } @@ -446,9 +444,9 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (mAudioDevices.isEmpty()) return; pw.println("Cached audio devices:"); - for (String address : mAudioDevices.keySet()) { - int state = mAudioDevices.get(address); - pw.println(address + " " + BluetoothA2dp.stateToString(state)); + for (BluetoothDevice device : mAudioDevices.keySet()) { + int state = mAudioDevices.get(device); + pw.println(device + " " + BluetoothA2dp.stateToString(state)); } } diff --git a/core/java/android/server/BluetoothEventLoop.java b/core/java/android/server/BluetoothEventLoop.java index 1704733..6610d0e 100644 --- a/core/java/android/server/BluetoothEventLoop.java +++ b/core/java/android/server/BluetoothEventLoop.java @@ -18,6 +18,7 @@ package android.server; import android.bluetooth.BluetoothA2dp; import android.bluetooth.BluetoothClass; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothError; import android.bluetooth.BluetoothIntent; @@ -46,8 +47,10 @@ class BluetoothEventLoop { private Thread mThread; private boolean mStarted; private boolean mInterrupted; + private final HashMap<String, Integer> mPasskeyAgentRequestData; - private final BluetoothDeviceService mBluetoothService; + private final BluetoothService mBluetoothService; + private final BluetoothAdapter mAdapter; private final Context mContext; private static final int EVENT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 1; @@ -84,10 +87,12 @@ class BluetoothEventLoop { static { classInitNative(); } private static native void classInitNative(); - /* pacakge */ BluetoothEventLoop(Context context, BluetoothDeviceService bluetoothService) { + /* pacakge */ BluetoothEventLoop(Context context, BluetoothAdapter adapter, + BluetoothService bluetoothService) { mBluetoothService = bluetoothService; mContext = context; mPasskeyAgentRequestData = new HashMap(); + mAdapter = adapter; initializeNativeDataNative(); } @@ -137,7 +142,7 @@ class BluetoothEventLoop { } if (classValue != null) { Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, mAdapter.getRemoteDevice(address)); intent.putExtra(BluetoothIntent.CLASS, Integer.valueOf(classValue)); intent.putExtra(BluetoothIntent.RSSI, rssiValue); intent.putExtra(BluetoothIntent.NAME, name); @@ -158,7 +163,7 @@ class BluetoothEventLoop { private void onDeviceDisappeared(String address) { Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, mAdapter.getRemoteDevice(address)); mContext.sendBroadcast(intent, BLUETOOTH_PERM); } @@ -251,7 +256,7 @@ class BluetoothEventLoop { if (pairable == null || discoverable == null) return; - int mode = BluetoothDeviceService.bluezStringToScanMode( + int mode = BluetoothService.bluezStringToScanMode( pairable.equals("true"), discoverable.equals("true")); if (mode >= 0) { @@ -299,15 +304,16 @@ class BluetoothEventLoop { Log.e(TAG, "onDevicePropertyChanged: Address of the remote device in null"); return; } + BluetoothDevice device = mAdapter.getRemoteDevice(address); if (name.equals("Name")) { Intent intent = new Intent(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, device); intent.putExtra(BluetoothIntent.NAME, propValues[1]); mContext.sendBroadcast(intent, BLUETOOTH_PERM); mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]); } else if (name.equals("Class")) { Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_CLASS_UPDATED_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, device); intent.putExtra(BluetoothIntent.CLASS, propValues[1]); mContext.sendBroadcast(intent, BLUETOOTH_PERM); mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]); @@ -318,7 +324,7 @@ class BluetoothEventLoop { } else { intent = new Intent(BluetoothIntent.REMOTE_DEVICE_DISCONNECTED_ACTION); } - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, device); mContext.sendBroadcast(intent, BLUETOOTH_PERM); mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]); } else if (name.equals("UUIDs")) { @@ -351,7 +357,7 @@ class BluetoothEventLoop { address = address.toUpperCase(); mPasskeyAgentRequestData.put(address, new Integer(nativeData)); - if (mBluetoothService.getBluetoothState() == BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF) { + if (mBluetoothService.getBluetoothState() == BluetoothAdapter.BLUETOOTH_STATE_TURNING_OFF) { // shutdown path mBluetoothService.cancelPairingUserInput(address); return null; @@ -364,7 +370,7 @@ class BluetoothEventLoop { if (address == null) return; Intent intent = new Intent(BluetoothIntent.PAIRING_REQUEST_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, mAdapter.getRemoteDevice(address)); intent.putExtra(BluetoothIntent.PASSKEY, passkey); intent.putExtra(BluetoothIntent.PAIRING_VARIANT, BluetoothDevice.PAIRING_VARIANT_CONFIRMATION); @@ -377,7 +383,7 @@ class BluetoothEventLoop { if (address == null) return; Intent intent = new Intent(BluetoothIntent.PAIRING_REQUEST_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, mAdapter.getRemoteDevice(address)); intent.putExtra(BluetoothIntent.PAIRING_VARIANT, BluetoothDevice.PAIRING_VARIANT_PASSKEY); mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); return; @@ -409,7 +415,7 @@ class BluetoothEventLoop { } } Intent intent = new Intent(BluetoothIntent.PAIRING_REQUEST_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, mAdapter.getRemoteDevice(address)); intent.putExtra(BluetoothIntent.PAIRING_VARIANT, BluetoothDevice.PAIRING_VARIANT_PIN); mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); return; @@ -428,7 +434,8 @@ class BluetoothEventLoop { (BluetoothUuid.isAudioSink(uuid) || BluetoothUuid.isAvrcpController(uuid) || BluetoothUuid.isAdvAudioDist(uuid))) { BluetoothA2dp a2dp = new BluetoothA2dp(mContext); - authorized = a2dp.getSinkPriority(address) > BluetoothA2dp.PRIORITY_OFF; + BluetoothDevice device = mAdapter.getRemoteDevice(address); + authorized = a2dp.getSinkPriority(device) > BluetoothA2dp.PRIORITY_OFF; if (authorized) { Log.i(TAG, "Allowing incoming A2DP / AVRCP connection from " + address); } else { diff --git a/core/java/android/server/BluetoothDeviceService.java b/core/java/android/server/BluetoothService.java index d2b4447..acee3af 100644 --- a/core/java/android/server/BluetoothDeviceService.java +++ b/core/java/android/server/BluetoothService.java @@ -16,7 +16,7 @@ /** * TODO: Move this to - * java/services/com/android/server/BluetoothDeviceService.java + * java/services/com/android/server/BluetoothService.java * and make the contructor package private again. * * @hide @@ -25,11 +25,12 @@ package android.server; import android.bluetooth.BluetoothClass; +import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothError; import android.bluetooth.BluetoothHeadset; import android.bluetooth.BluetoothIntent; -import android.bluetooth.IBluetoothDevice; +import android.bluetooth.IBluetooth; import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; @@ -55,8 +56,8 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -public class BluetoothDeviceService extends IBluetoothDevice.Stub { - private static final String TAG = "BluetoothDeviceService"; +public class BluetoothService extends IBluetooth.Stub { + private static final String TAG = "BluetoothService"; private static final boolean DBG = true; private int mNativeData; @@ -65,11 +66,11 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { private boolean mIsAirplaneSensitive; private int mBluetoothState; private boolean mRestart = false; // need to call enable() after disable() + private boolean mIsDiscovering; + private BluetoothAdapter mAdapter; // constant after init() private final BondState mBondState = new BondState(); // local cache of bondings - private boolean mIsDiscovering; private final IBatteryStats mBatteryStats; - private final Context mContext; private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; @@ -78,14 +79,14 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { private static final int MESSAGE_REGISTER_SDP_RECORDS = 1; private static final int MESSAGE_FINISH_DISABLE = 2; - private Map<String, String> mProperties; - private HashMap <String, Map<String, String>> mRemoteDeviceProperties; + private final Map<String, String> mAdapterProperties; + private final HashMap <String, Map<String, String>> mDeviceProperties; static { classInitNative(); } - public BluetoothDeviceService(Context context) { + public BluetoothService(Context context) { mContext = context; // Need to do this in place of: @@ -93,11 +94,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { // Since we can not import BatteryStatsService from here. This class really needs to be // moved to java/services/com/android/server/ mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService("batteryinfo")); - } - /** Must be called after construction, and before any other method. - */ - public synchronized void init() { initializeNativeDataNative(); if (isEnabledNative() == 1) { @@ -105,12 +102,16 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { disableNative(); } - setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_OFF); + mBluetoothState = BluetoothAdapter.BLUETOOTH_STATE_OFF; mIsDiscovering = false; - mEventLoop = new BluetoothEventLoop(mContext, this); + mAdapterProperties = new HashMap<String, String>(); + mDeviceProperties = new HashMap<String, Map<String,String>>(); registerForAirplaneMode(); - mProperties = new HashMap<String, String>(); - mRemoteDeviceProperties = new HashMap<String, Map<String,String>>(); + } + + public synchronized void initAfterRegistration() { + mAdapter = (BluetoothAdapter) mContext.getSystemService(Context.BLUETOOTH_SERVICE); + mEventLoop = new BluetoothEventLoop(mContext, mAdapter, this); } @Override @@ -127,7 +128,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { public boolean isEnabled() { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); - return mBluetoothState == BluetoothDevice.BLUETOOTH_STATE_ON; + return mBluetoothState == BluetoothAdapter.BLUETOOTH_STATE_ON; } public int getBluetoothState() { @@ -152,9 +153,9 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); switch (mBluetoothState) { - case BluetoothDevice.BLUETOOTH_STATE_OFF: + case BluetoothAdapter.BLUETOOTH_STATE_OFF: return true; - case BluetoothDevice.BLUETOOTH_STATE_ON: + case BluetoothAdapter.BLUETOOTH_STATE_ON: break; default: return false; @@ -162,11 +163,11 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { if (mEnableThread != null && mEnableThread.isAlive()) { return false; } - setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF); + setBluetoothState(BluetoothAdapter.BLUETOOTH_STATE_TURNING_OFF); // Allow 3 seconds for profiles to gracefully disconnect // TODO: Introduce a callback mechanism so that each profile can notify - // BluetoothDeviceService when it is done shutting down + // BluetoothService when it is done shutting down mHandler.sendMessageDelayed( mHandler.obtainMessage(MESSAGE_FINISH_DISABLE, saveSetting ? 1 : 0, 0), 3000); return true; @@ -174,7 +175,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { private synchronized void finishDisable(boolean saveSetting) { - if (mBluetoothState != BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF) { + if (mBluetoothState != BluetoothAdapter.BLUETOOTH_STATE_TURNING_OFF) { return; } mEventLoop.stop(); @@ -189,17 +190,17 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { // update mode Intent intent = new Intent(BluetoothIntent.SCAN_MODE_CHANGED_ACTION); - intent.putExtra(BluetoothIntent.SCAN_MODE, BluetoothDevice.SCAN_MODE_NONE); + intent.putExtra(BluetoothIntent.SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE); mContext.sendBroadcast(intent, BLUETOOTH_PERM); mIsDiscovering = false; - mProperties.clear(); + mAdapterProperties.clear(); if (saveSetting) { persistBluetoothOnSetting(false); } - setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_OFF); + setBluetoothState(BluetoothAdapter.BLUETOOTH_STATE_OFF); // Log bluetooth off to battery stats. long ident = Binder.clearCallingIdentity(); @@ -236,13 +237,13 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { if (mIsAirplaneSensitive && isAirplaneModeOn()) { return false; } - if (mBluetoothState != BluetoothDevice.BLUETOOTH_STATE_OFF) { + if (mBluetoothState != BluetoothAdapter.BLUETOOTH_STATE_OFF) { return false; } if (mEnableThread != null && mEnableThread.isAlive()) { return false; } - setBluetoothState(BluetoothDevice.BLUETOOTH_STATE_TURNING_ON); + setBluetoothState(BluetoothAdapter.BLUETOOTH_STATE_TURNING_ON); mEnableThread = new EnableThread(saveSetting); mEnableThread.start(); return true; @@ -250,7 +251,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { /** Forcibly restart Bluetooth if it is on */ /* package */ synchronized void restart() { - if (mBluetoothState != BluetoothDevice.BLUETOOTH_STATE_ON) { + if (mBluetoothState != BluetoothAdapter.BLUETOOTH_STATE_ON) { return; } mRestart = true; @@ -356,8 +357,8 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { mEnableThread = null; setBluetoothState(res ? - BluetoothDevice.BLUETOOTH_STATE_ON : - BluetoothDevice.BLUETOOTH_STATE_OFF); + BluetoothAdapter.BLUETOOTH_STATE_ON : + BluetoothAdapter.BLUETOOTH_STATE_OFF); if (res) { // Update mode @@ -410,7 +411,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { )); public synchronized void loadBondState() { - if (mBluetoothState != BluetoothDevice.BLUETOOTH_STATE_TURNING_ON) { + if (mBluetoothState != BluetoothAdapter.BLUETOOTH_STATE_TURNING_ON) { return; } String []bonds = null; @@ -442,7 +443,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { if (DBG) log(address + " bond state " + oldState + " -> " + state + " (" + reason + ")"); Intent intent = new Intent(BluetoothIntent.BOND_STATE_CHANGED_ACTION); - intent.putExtra(BluetoothIntent.ADDRESS, address); + intent.putExtra(BluetoothIntent.DEVICE, mAdapter.getRemoteDevice(address)); intent.putExtra(BluetoothIntent.BOND_STATE, state); intent.putExtra(BluetoothIntent.BOND_PREVIOUS_STATE, oldState); if (state == BluetoothDevice.BOND_NOT_BONDED) { @@ -539,7 +540,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { /*package*/synchronized void getAllProperties() { mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); - mProperties.clear(); + mAdapterProperties.clear(); String properties[] = (String [])getAdapterPropertiesNative(); // The String Array consists of key-value pairs. @@ -568,17 +569,17 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { } else { newValue = properties[++i]; } - mProperties.put(name, newValue); + mAdapterProperties.put(name, newValue); } // Add adapter object path property. String adapterPath = getAdapterPathNative(); if (adapterPath != null) - mProperties.put("ObjectPath", adapterPath + "/dev_"); + mAdapterProperties.put("ObjectPath", adapterPath + "/dev_"); } /* package */ synchronized void setProperty(String name, String value) { - mProperties.put(name, value); + mAdapterProperties.put(name, value); } public synchronized boolean setName(String name) { @@ -646,10 +647,10 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { } /*package*/ synchronized String getProperty (String name) { - if (!mProperties.isEmpty()) - return mProperties.get(name); + if (!mAdapterProperties.isEmpty()) + return mAdapterProperties.get(name); getAllProperties(); - return mProperties.get(name); + return mAdapterProperties.get(name); } public synchronized String getAddress() { @@ -678,7 +679,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { if (!BluetoothDevice.checkBluetoothAddress(address)) { return null; } - Map <String, String> properties = mRemoteDeviceProperties.get(address); + Map <String, String> properties = mDeviceProperties.get(address); if (properties != null) return properties.get("Name"); return null; } @@ -805,7 +806,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { } /*package*/ boolean isRemoteDeviceInCache(String address) { - return (mRemoteDeviceProperties.get(address) != null); + return (mDeviceProperties.get(address) != null); } /*package*/ String[] getRemoteDeviceProperties(String address) { @@ -814,7 +815,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { } /*package*/ synchronized String getRemoteDeviceProperty(String address, String property) { - Map<String, String> properties = mRemoteDeviceProperties.get(address); + Map<String, String> properties = mDeviceProperties.get(address); if (properties != null) { return properties.get(property); } else { @@ -835,7 +836,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { /* * We get a DeviceFound signal every time RSSI changes or name changes. * Don't create a new Map object every time */ - Map<String, String> propertyValues = mRemoteDeviceProperties.get(address); + Map<String, String> propertyValues = mDeviceProperties.get(address); if (propertyValues != null) { propertyValues.clear(); } else { @@ -864,19 +865,19 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { } propertyValues.put(name, newValue); } - mRemoteDeviceProperties.put(address, propertyValues); + mDeviceProperties.put(address, propertyValues); } /* package */ void removeRemoteDeviceProperties(String address) { - mRemoteDeviceProperties.remove(address); + mDeviceProperties.remove(address); } /* package */ synchronized void setRemoteDeviceProperty(String address, String name, String value) { - Map <String, String> propVal = mRemoteDeviceProperties.get(address); + Map <String, String> propVal = mDeviceProperties.get(address); if (propVal != null) { propVal.put(name, value); - mRemoteDeviceProperties.put(address, propVal); + mDeviceProperties.put(address, propVal); } else { Log.e(TAG, "setRemoteDeviceProperty for a device not in cache:" + address); } @@ -1059,16 +1060,16 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { pw.println("\nmIsAirplaneSensitive = " + mIsAirplaneSensitive + "\n"); switch(mBluetoothState) { - case BluetoothDevice.BLUETOOTH_STATE_OFF: + case BluetoothAdapter.BLUETOOTH_STATE_OFF: pw.println("\nBluetooth OFF\n"); return; - case BluetoothDevice.BLUETOOTH_STATE_TURNING_ON: + case BluetoothAdapter.BLUETOOTH_STATE_TURNING_ON: pw.println("\nBluetooth TURNING ON\n"); return; - case BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF: + case BluetoothAdapter.BLUETOOTH_STATE_TURNING_OFF: pw.println("\nBluetooth TURNING OFF\n"); return; - case BluetoothDevice.BLUETOOTH_STATE_ON: + case BluetoothAdapter.BLUETOOTH_STATE_ON: pw.println("\nBluetooth ON\n"); } @@ -1079,7 +1080,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { BluetoothHeadset headset = new BluetoothHeadset(mContext, null); pw.println("\n--Known devices--"); - for (String address : mRemoteDeviceProperties.keySet()) { + for (String address : mDeviceProperties.keySet()) { pw.printf("%s %10s (%d) %s\n", address, toBondStateString(mBondState.getBondState(address)), mBondState.getAttempt(address), @@ -1113,7 +1114,7 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { pw.println("getState() = STATE_ERROR"); break; } - pw.println("getHeadsetAddress() = " + headset.getHeadsetAddress()); + pw.println("getCurrentHeadset() = " + headset.getCurrentHeadset()); pw.println("getBatteryUsageHint() = " + headset.getBatteryUsageHint()); headset.close(); @@ -1121,20 +1122,20 @@ public class BluetoothDeviceService extends IBluetoothDevice.Stub { /* package */ static int bluezStringToScanMode(boolean pairable, boolean discoverable) { if (pairable && discoverable) - return BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE; + return BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE; else if (pairable && !discoverable) - return BluetoothDevice.SCAN_MODE_CONNECTABLE; + return BluetoothAdapter.SCAN_MODE_CONNECTABLE; else - return BluetoothDevice.SCAN_MODE_NONE; + return BluetoothAdapter.SCAN_MODE_NONE; } /* package */ static String scanModeToBluezString(int mode) { switch (mode) { - case BluetoothDevice.SCAN_MODE_NONE: + case BluetoothAdapter.SCAN_MODE_NONE: return "off"; - case BluetoothDevice.SCAN_MODE_CONNECTABLE: + case BluetoothAdapter.SCAN_MODE_CONNECTABLE: return "connectable"; - case BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE: + case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: return "discoverable"; } return null; |