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