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.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 public void cancelDiscovery() { 81 mAdapter.cancelDiscovery(); 82 } 83 84 public boolean enable() { 85 return mAdapter.enable(); 86 } 87 88 public 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 public Set<BluetoothDevice> getBondedDevices() { 98 return mAdapter.getBondedDevices(); 99 } 100 101 public String getName() { 102 return mAdapter.getName(); 103 } 104 105 public int getScanMode() { 106 return mAdapter.getScanMode(); 107 } 108 109 public int getState() { 110 return mAdapter.getState(); 111 } 112 113 public ParcelUuid[] getUuids() { 114 return mAdapter.getUuids(); 115 } 116 117 public boolean isDiscovering() { 118 return mAdapter.isDiscovering(); 119 } 120 121 public boolean isEnabled() { 122 return mAdapter.isEnabled(); 123 } 124 125 public int getConnectionState() { 126 return mAdapter.getConnectionState(); 127 } 128 129 public void setDiscoverableTimeout(int timeout) { 130 mAdapter.setDiscoverableTimeout(timeout); 131 } 132 133 public void setName(String name) { 134 mAdapter.setName(name); 135 } 136 137 public void setScanMode(int mode) { 138 mAdapter.setScanMode(mode); 139 } 140 141 public boolean setScanMode(int mode, int duration) { 142 return mAdapter.setScanMode(mode, duration); 143 } 144 145 public void startScanning(boolean force) { 146 // Only start if we're not already scanning 147 if (!mAdapter.isDiscovering()) { 148 if (!force) { 149 // Don't scan more than frequently than SCAN_EXPIRATION_MS, 150 // unless forced 151 if (mLastScan + SCAN_EXPIRATION_MS > System.currentTimeMillis()) { 152 return; 153 } 154 155 // If we are playing music, don't scan unless forced. 156 A2dpProfile a2dp = mProfileManager.getA2dpProfile(); 157 if (a2dp != null && a2dp.isA2dpPlaying()) { 158 return; 159 } 160 } 161 162 if (mAdapter.startDiscovery()) { 163 mLastScan = System.currentTimeMillis(); 164 } 165 } 166 } 167 168 public void stopScanning() { 169 if (mAdapter.isDiscovering()) { 170 mAdapter.cancelDiscovery(); 171 } 172 } 173 174 public synchronized int getBluetoothState() { 175 // Always sync state, in case it changed while paused 176 syncBluetoothState(); 177 return mState; 178 } 179 180 synchronized void setBluetoothStateInt(int state) { 181 mState = state; 182 183 if (state == BluetoothAdapter.STATE_ON) { 184 // if mProfileManager hasn't been constructed yet, it will 185 // get the adapter UUIDs in its constructor when it is. 186 if (mProfileManager != null) { 187 mProfileManager.setBluetoothStateOn(); 188 } 189 } 190 } 191 192 // Returns true if the state changed; false otherwise. 193 boolean syncBluetoothState() { 194 int currentState = mAdapter.getState(); 195 if (currentState != mState) { 196 setBluetoothStateInt(mAdapter.getState()); 197 return true; 198 } 199 return false; 200 } 201 202 public void setBluetoothEnabled(boolean enabled) { 203 boolean success = enabled 204 ? mAdapter.enable() 205 : mAdapter.disable(); 206 207 if (success) { 208 setBluetoothStateInt(enabled 209 ? BluetoothAdapter.STATE_TURNING_ON 210 : BluetoothAdapter.STATE_TURNING_OFF); 211 } else { 212 if (Utils.V) { 213 Log.v(TAG, "setBluetoothEnabled call, manager didn't return " + 214 "success for enabled: " + enabled); 215 } 216 217 syncBluetoothState(); 218 } 219 } 220 221 public BluetoothDevice getRemoteDevice(String address) { 222 return mAdapter.getRemoteDevice(address); 223 } 224 } 225