BluetoothControllerImpl.java revision ccb6b9a90f228cc4e31a9442ed28756ff474c080
1/* 2 * Copyright (C) 2008 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 17package com.android.systemui.statusbar.policy; 18 19import android.bluetooth.BluetoothAdapter; 20import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback; 21import android.bluetooth.BluetoothDevice; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.Intent; 25import android.content.IntentFilter; 26 27import java.util.ArrayList; 28import java.util.HashSet; 29import java.util.Set; 30 31public class BluetoothControllerImpl extends BroadcastReceiver implements BluetoothController { 32 private static final String TAG = "StatusBar.BluetoothController"; 33 34 private final BluetoothAdapter mAdapter; 35 36 private boolean mEnabled = false; 37 38 private Set<BluetoothDevice> mBondedDevices = new HashSet<BluetoothDevice>(); 39 40 private ArrayList<BluetoothStateChangeCallback> mChangeCallbacks = 41 new ArrayList<BluetoothStateChangeCallback>(); 42 43 public BluetoothControllerImpl(Context context) { 44 mAdapter = BluetoothAdapter.getDefaultAdapter(); 45 46 IntentFilter filter = new IntentFilter(); 47 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); 48 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED); 49 filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 50 context.registerReceiver(this, filter); 51 52 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 53 if (adapter != null) { 54 handleAdapterStateChange(adapter.getState()); 55 } 56 fireCallbacks(); 57 updateBondedBluetoothDevices(); 58 } 59 60 public void addStateChangedCallback(BluetoothStateChangeCallback cb) { 61 mChangeCallbacks.add(cb); 62 fireCallback(cb); 63 } 64 65 @Override 66 public void removeStateChangedCallback(BluetoothStateChangeCallback cb) { 67 mChangeCallbacks.remove(cb); 68 } 69 70 @Override 71 public boolean isBluetoothEnabled() { 72 return mAdapter != null && mAdapter.isEnabled(); 73 } 74 75 @Override 76 public boolean isBluetoothConnected() { 77 return mAdapter != null 78 && mAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED; 79 } 80 81 @Override 82 public void setBluetoothEnabled(boolean enabled) { 83 if (mAdapter != null) { 84 if (enabled) { 85 mAdapter.enable(); 86 } else { 87 mAdapter.disable(); 88 } 89 } 90 } 91 92 @Override 93 public boolean isBluetoothSupported() { 94 return mAdapter != null; 95 } 96 97 public Set<BluetoothDevice> getBondedBluetoothDevices() { 98 return mBondedDevices; 99 } 100 101 @Override 102 public void onReceive(Context context, Intent intent) { 103 final String action = intent.getAction(); 104 105 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 106 handleAdapterStateChange( 107 intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)); 108 } 109 fireCallbacks(); 110 updateBondedBluetoothDevices(); 111 } 112 113 private void updateBondedBluetoothDevices() { 114 mBondedDevices.clear(); 115 116 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 117 if (adapter != null) { 118 Set<BluetoothDevice> devices = adapter.getBondedDevices(); 119 if (devices != null) { 120 for (BluetoothDevice device : devices) { 121 if (device.getBondState() != BluetoothDevice.BOND_NONE) { 122 mBondedDevices.add(device); 123 } 124 } 125 } 126 } 127 } 128 129 private void handleAdapterStateChange(int adapterState) { 130 mEnabled = (adapterState == BluetoothAdapter.STATE_ON); 131 } 132 133 private void fireCallbacks() { 134 for (BluetoothStateChangeCallback cb : mChangeCallbacks) { 135 fireCallback(cb); 136 } 137 } 138 139 private void fireCallback(BluetoothStateChangeCallback cb) { 140 cb.onBluetoothStateChange(mEnabled); 141 } 142} 143