Use {@link #getDefaultAdapter} to get the default local Bluetooth + * adapter. * *
Use the {@link BluetoothDevice} class for operations on remote Bluetooth * devices. @@ -257,12 +257,40 @@ public final class BluetoothAdapter { */ public static final String EXTRA_LOCAL_NAME = "android.bluetooth.adapter.extra.LOCAL_NAME"; + /** @hide */ + public static final String BLUETOOTH_SERVICE = "bluetooth"; + private static final int ADDRESS_LENGTH = 17; + /** + * Lazyily initialized singleton. Guaranteed final after first object + * constructed. + */ + private static BluetoothAdapter sAdapter; + private final IBluetooth mService; /** - * Do not use this constructor. Use Context.getSystemService() instead. + * Get a handle to the default local Bluetooth adapter. + *
Currently Android only supports one Bluetooth adapter, but the API + * could be extended to support more. This will always return the default + * adapter. + * @return the default local adapter, or null if Bluetooth is not supported + * on this hardware platform + */ + public static synchronized BluetoothAdapter getDefaultAdapter() { + if (sAdapter == null) { + IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); + if (b != null) { + IBluetooth service = IBluetooth.Stub.asInterface(b); + sAdapter = new BluetoothAdapter(service); + } + } + return sAdapter; + } + + /** + * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance. * @hide */ public BluetoothAdapter(IBluetooth service) { diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java index ce975c2279427..9c23746494a4f 100644 --- a/core/java/android/bluetooth/BluetoothDevice.java +++ b/core/java/android/bluetooth/BluetoothDevice.java @@ -18,7 +18,6 @@ package android.bluetooth; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; -import android.content.Context; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; @@ -328,7 +327,7 @@ public final class BluetoothDevice implements Parcelable { /*package*/ static IBluetooth getService() { synchronized (BluetoothDevice.class) { if (sService == null) { - IBinder b = ServiceManager.getService(Context.BLUETOOTH_SERVICE); + IBinder b = ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE); if (b == null) { throw new RuntimeException("Bluetooth service not available"); } diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index fe4665e5e7aec..8f1c671046ae6 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -1217,14 +1217,6 @@ public abstract class Context { * @see android.hardware.SensorManager */ public static final String SENSOR_SERVICE = "sensor"; - /** - * Use with {@link #getSystemService} to retrieve a {@link - * android.bluetooth.BluetoothAdapter} for using Bluetooth. - * - * @see #getSystemService - * @see android.bluetooth.BluetoothAdapter - */ - public static final String BLUETOOTH_SERVICE = "bluetooth"; /** * Use with {@link #getSystemService} to retrieve a * com.android.server.WallpaperService for accessing wallpapers. diff --git a/core/java/android/server/BluetoothA2dpService.java b/core/java/android/server/BluetoothA2dpService.java index d61b42f71be10..b73e53ffca838 100644 --- a/core/java/android/server/BluetoothA2dpService.java +++ b/core/java/android/server/BluetoothA2dpService.java @@ -137,7 +137,7 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { throw new RuntimeException("Could not init BluetoothA2dpService"); } - mAdapter = (BluetoothAdapter) context.getSystemService(Context.BLUETOOTH_SERVICE); + mAdapter = BluetoothAdapter.getDefaultAdapter(); mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); mIntentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java index 3fdbb68018bb7..6d4d1527a9292 100644 --- a/core/java/android/server/BluetoothService.java +++ b/core/java/android/server/BluetoothService.java @@ -154,7 +154,7 @@ public class BluetoothService extends IBluetooth.Stub { } public synchronized void initAfterRegistration() { - mAdapter = (BluetoothAdapter) mContext.getSystemService(Context.BLUETOOTH_SERVICE); + mAdapter = BluetoothAdapter.getDefaultAdapter(); mEventLoop = new BluetoothEventLoop(mContext, mAdapter, this); } diff --git a/core/java/com/android/internal/app/ShutdownThread.java b/core/java/com/android/internal/app/ShutdownThread.java index 9e1f325d1d4e9..01f6dac2a4b15 100644 --- a/core/java/com/android/internal/app/ShutdownThread.java +++ b/core/java/com/android/internal/app/ShutdownThread.java @@ -181,7 +181,7 @@ public final class ShutdownThread extends Thread { ITelephony.Stub.asInterface(ServiceManager.checkService("phone")); final IBluetooth bluetooth = IBluetooth.Stub.asInterface(ServiceManager.checkService( - Context.BLUETOOTH_SERVICE)); + BluetoothAdapter.BLUETOOTH_SERVICE)); try { bluetoothOff = bluetooth == null || diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index e5c60109fb19a..b8cf844edaeed 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -23,6 +23,7 @@ import com.android.internal.os.SamplingProfilerIntegration; import dalvik.system.VMRuntime; import android.app.ActivityManagerNative; +import android.bluetooth.BluetoothAdapter; import android.content.ComponentName; import android.content.ContentResolver; import android.content.ContentService; @@ -172,14 +173,14 @@ class ServerThread extends Thread { // support Bluetooth - see bug 988521 if (SystemProperties.get("ro.kernel.qemu").equals("1")) { Log.i(TAG, "Registering null Bluetooth Service (emulator)"); - ServiceManager.addService(Context.BLUETOOTH_SERVICE, null); + ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null); } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { Log.i(TAG, "Registering null Bluetooth Service (factory test)"); - ServiceManager.addService(Context.BLUETOOTH_SERVICE, null); + ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null); } else { Log.i(TAG, "Bluetooth Service"); bluetooth = new BluetoothService(context); - ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth); + ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth); bluetooth.initAfterRegistration(); bluetoothA2dp = new BluetoothA2dpService(context, bluetooth); ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE, diff --git a/services/java/com/android/server/status/StatusBarPolicy.java b/services/java/com/android/server/status/StatusBarPolicy.java index cf63d0220c036..801a938c46960 100644 --- a/services/java/com/android/server/status/StatusBarPolicy.java +++ b/services/java/com/android/server/status/StatusBarPolicy.java @@ -448,8 +448,7 @@ public class StatusBarPolicy { mBluetoothData = IconData.makeIcon("bluetooth", null, com.android.internal.R.drawable.stat_sys_data_bluetooth, 0, 0); mBluetoothIcon = service.addIcon(mBluetoothData, null); - BluetoothAdapter adapter = - (BluetoothAdapter) mContext.getSystemService(Context.BLUETOOTH_SERVICE); + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if (adapter != null) { mBluetoothEnabled = adapter.isEnabled(); } else {