/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settingslib.bluetooth; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.bluetooth.le.BluetoothLeScanner; import android.content.Context; import android.os.ParcelUuid; import android.util.Log; import java.util.Set; /** * LocalBluetoothAdapter provides an interface between the Settings app * and the functionality of the local {@link BluetoothAdapter}, specifically * those related to state transitions of the adapter itself. * *
Connection and bonding state changes affecting specific devices
* are handled by {@link CachedBluetoothDeviceManager},
* {@link BluetoothEventManager}, and {@link LocalBluetoothProfileManager}.
*/
public final class LocalBluetoothAdapter {
private static final String TAG = "LocalBluetoothAdapter";
/** This class does not allow direct access to the BluetoothAdapter. */
private final BluetoothAdapter mAdapter;
private LocalBluetoothProfileManager mProfileManager;
private static LocalBluetoothAdapter sInstance;
private int mState = BluetoothAdapter.ERROR;
private static final int SCAN_EXPIRATION_MS = 5 * 60 * 1000; // 5 mins
private long mLastScan;
private LocalBluetoothAdapter(BluetoothAdapter adapter) {
mAdapter = adapter;
}
void setProfileManager(LocalBluetoothProfileManager manager) {
mProfileManager = manager;
}
/**
* Get the singleton instance of the LocalBluetoothAdapter. If this device
* doesn't support Bluetooth, then null will be returned. Callers must be
* prepared to handle a null return value.
* @return the LocalBluetoothAdapter object, or null if not supported
*/
static synchronized LocalBluetoothAdapter getInstance() {
if (sInstance == null) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) {
sInstance = new LocalBluetoothAdapter(adapter);
}
}
return sInstance;
}
// Pass-through BluetoothAdapter methods that we can intercept if necessary
public void cancelDiscovery() {
mAdapter.cancelDiscovery();
}
public boolean enable() {
return mAdapter.enable();
}
public boolean disable() {
return mAdapter.disable();
}
void getProfileProxy(Context context,
BluetoothProfile.ServiceListener listener, int profile) {
mAdapter.getProfileProxy(context, listener, profile);
}
public Set