• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib.bluetooth;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.bluetooth.BluetoothDevice;
21 import android.bluetooth.BluetoothProfile;
22 import android.bluetooth.le.BluetoothLeScanner;
23 import android.content.Context;
24 import android.os.ParcelUuid;
25 import android.util.Log;
26 
27 import java.util.List;
28 import java.util.Set;
29 
30 /**
31  * LocalBluetoothAdapter provides an interface between the Settings app
32  * and the functionality of the local {@link BluetoothAdapter}, specifically
33  * those related to state transitions of the adapter itself.
34  *
35  * <p>Connection and bonding state changes affecting specific devices
36  * are handled by {@link CachedBluetoothDeviceManager},
37  * {@link BluetoothEventManager}, and {@link LocalBluetoothProfileManager}.
38  *
39  * @deprecated use {@link BluetoothAdapter} instead.
40  */
41 @Deprecated
42 public class LocalBluetoothAdapter {
43     private static final String TAG = "LocalBluetoothAdapter";
44 
45     /** This class does not allow direct access to the BluetoothAdapter. */
46     private final BluetoothAdapter mAdapter;
47 
48     private LocalBluetoothProfileManager mProfileManager;
49 
50     private static LocalBluetoothAdapter sInstance;
51 
52     private int mState = BluetoothAdapter.ERROR;
53 
54     private static final int SCAN_EXPIRATION_MS = 5 * 60 * 1000; // 5 mins
55 
56     private long mLastScan;
57 
LocalBluetoothAdapter(BluetoothAdapter adapter)58     private LocalBluetoothAdapter(BluetoothAdapter adapter) {
59         mAdapter = adapter;
60     }
61 
setProfileManager(LocalBluetoothProfileManager manager)62     void setProfileManager(LocalBluetoothProfileManager manager) {
63         mProfileManager = manager;
64     }
65 
66     /**
67      * Get the singleton instance of the LocalBluetoothAdapter. If this device
68      * doesn't support Bluetooth, then null will be returned. Callers must be
69      * prepared to handle a null return value.
70      * @return the LocalBluetoothAdapter object, or null if not supported
71      */
getInstance()72     static synchronized LocalBluetoothAdapter getInstance() {
73         if (sInstance == null) {
74             BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
75             if (adapter != null) {
76                 sInstance = new LocalBluetoothAdapter(adapter);
77             }
78         }
79 
80         return sInstance;
81     }
82 
83     // Pass-through BluetoothAdapter methods that we can intercept if necessary
84 
cancelDiscovery()85     public void cancelDiscovery() {
86         mAdapter.cancelDiscovery();
87     }
88 
enable()89     public boolean enable() {
90         return mAdapter.enable();
91     }
92 
disable()93     public boolean disable() {
94         return mAdapter.disable();
95     }
96 
getAddress()97     public String getAddress() {
98         return mAdapter.getAddress();
99     }
100 
getProfileProxy(Context context, BluetoothProfile.ServiceListener listener, int profile)101     void getProfileProxy(Context context,
102             BluetoothProfile.ServiceListener listener, int profile) {
103         mAdapter.getProfileProxy(context, listener, profile);
104     }
105 
getBondedDevices()106     public Set<BluetoothDevice> getBondedDevices() {
107         return mAdapter.getBondedDevices();
108     }
109 
getName()110     public String getName() {
111         return mAdapter.getName();
112     }
113 
getScanMode()114     public int getScanMode() {
115         return mAdapter.getScanMode();
116     }
117 
getBluetoothLeScanner()118     public BluetoothLeScanner getBluetoothLeScanner() {
119         return mAdapter.getBluetoothLeScanner();
120     }
121 
getState()122     public int getState() {
123         return mAdapter.getState();
124     }
125 
getUuids()126     public ParcelUuid[] getUuids() {
127         return mAdapter.getUuids();
128     }
129 
isDiscovering()130     public boolean isDiscovering() {
131         return mAdapter.isDiscovering();
132     }
133 
isEnabled()134     public boolean isEnabled() {
135         return mAdapter.isEnabled();
136     }
137 
getConnectionState()138     public int getConnectionState() {
139         return mAdapter.getConnectionState();
140     }
141 
setDiscoverableTimeout(int timeout)142     public void setDiscoverableTimeout(int timeout) {
143         mAdapter.setDiscoverableTimeout(timeout);
144     }
145 
getDiscoveryEndMillis()146     public long getDiscoveryEndMillis() {
147         return mAdapter.getDiscoveryEndMillis();
148     }
149 
setName(String name)150     public void setName(String name) {
151         mAdapter.setName(name);
152     }
153 
setScanMode(int mode)154     public void setScanMode(int mode) {
155         mAdapter.setScanMode(mode);
156     }
157 
setScanMode(int mode, int duration)158     public boolean setScanMode(int mode, int duration) {
159         return mAdapter.setScanMode(mode, duration);
160     }
161 
startScanning(boolean force)162     public void startScanning(boolean force) {
163         // Only start if we're not already scanning
164         if (!mAdapter.isDiscovering()) {
165             if (!force) {
166                 // Don't scan more than frequently than SCAN_EXPIRATION_MS,
167                 // unless forced
168                 if (mLastScan + SCAN_EXPIRATION_MS > System.currentTimeMillis()) {
169                     return;
170                 }
171 
172                 // If we are playing music, don't scan unless forced.
173                 A2dpProfile a2dp = mProfileManager.getA2dpProfile();
174                 if (a2dp != null && a2dp.isA2dpPlaying()) {
175                     return;
176                 }
177                 A2dpSinkProfile a2dpSink = mProfileManager.getA2dpSinkProfile();
178                 if ((a2dpSink != null) && (a2dpSink.isA2dpPlaying())){
179                     return;
180                 }
181             }
182 
183             if (mAdapter.startDiscovery()) {
184                 mLastScan = System.currentTimeMillis();
185             }
186         }
187     }
188 
stopScanning()189     public void stopScanning() {
190         if (mAdapter.isDiscovering()) {
191             mAdapter.cancelDiscovery();
192         }
193     }
194 
getBluetoothState()195     public synchronized int getBluetoothState() {
196         // Always sync state, in case it changed while paused
197         syncBluetoothState();
198         return mState;
199     }
200 
setBluetoothStateInt(int state)201     void setBluetoothStateInt(int state) {
202         synchronized(this) {
203             if (mState == state) {
204                 return;
205             }
206             mState = state;
207         }
208 
209         if (state == BluetoothAdapter.STATE_ON) {
210             // if mProfileManager hasn't been constructed yet, it will
211             // get the adapter UUIDs in its constructor when it is.
212             if (mProfileManager != null) {
213                 mProfileManager.setBluetoothStateOn();
214             }
215         }
216     }
217 
218     // Returns true if the state changed; false otherwise.
syncBluetoothState()219     boolean syncBluetoothState() {
220         int currentState = mAdapter.getState();
221         if (currentState != mState) {
222             setBluetoothStateInt(mAdapter.getState());
223             return true;
224         }
225         return false;
226     }
227 
setBluetoothEnabled(boolean enabled)228     public boolean setBluetoothEnabled(boolean enabled) {
229         boolean success = enabled
230                 ? mAdapter.enable()
231                 : mAdapter.disable();
232 
233         if (success) {
234             setBluetoothStateInt(enabled
235                 ? BluetoothAdapter.STATE_TURNING_ON
236                 : BluetoothAdapter.STATE_TURNING_OFF);
237         } else {
238             if (BluetoothUtils.V) {
239                 Log.v(TAG, "setBluetoothEnabled call, manager didn't return " +
240                         "success for enabled: " + enabled);
241             }
242 
243             syncBluetoothState();
244         }
245         return success;
246     }
247 
getRemoteDevice(String address)248     public BluetoothDevice getRemoteDevice(String address) {
249         return mAdapter.getRemoteDevice(address);
250     }
251 
getSupportedProfiles()252     public List<Integer> getSupportedProfiles() {
253         return mAdapter.getSupportedProfiles();
254     }
255 }
256