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