blob: 3c91423a5d1163a800bb81ba8d2531ae032e12b0 [file] [log] [blame]
fredc0f420372012-04-12 00:02:00 -07001/*
Zhihai Xufa0fd392012-10-23 17:31:56 -07002 * Copyright (C) 2012 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.
fredc0f420372012-04-12 00:02:00 -070015 */
16
17package com.android.server;
18
Svet Ganov408abf72015-05-12 19:13:36 -070019import android.Manifest;
Zhihai Xu40874a02012-10-08 17:57:03 -070020import android.app.ActivityManager;
fredc0f420372012-04-12 00:02:00 -070021import android.bluetooth.BluetoothAdapter;
Sharvil Nanavati7c250052015-06-20 12:05:52 -070022import android.bluetooth.BluetoothDevice;
Benjamin Franze8b98922014-11-12 15:57:54 +000023import android.bluetooth.BluetoothProfile;
fredc0f420372012-04-12 00:02:00 -070024import android.bluetooth.IBluetooth;
fredcbf072a72012-05-09 16:52:50 -070025import android.bluetooth.IBluetoothCallback;
Wei Wange4a744b2015-06-11 17:50:29 -070026import android.bluetooth.IBluetoothGatt;
Benjamin Franze8b98922014-11-12 15:57:54 +000027import android.bluetooth.IBluetoothHeadset;
fredc0f420372012-04-12 00:02:00 -070028import android.bluetooth.IBluetoothManager;
29import android.bluetooth.IBluetoothManagerCallback;
Benjamin Franze8b98922014-11-12 15:57:54 +000030import android.bluetooth.IBluetoothProfileServiceConnection;
fredc0f420372012-04-12 00:02:00 -070031import android.bluetooth.IBluetoothStateChangeCallback;
fredc0f420372012-04-12 00:02:00 -070032import android.content.BroadcastReceiver;
33import android.content.ComponentName;
34import android.content.ContentResolver;
35import android.content.Context;
36import android.content.Intent;
37import android.content.IntentFilter;
38import android.content.ServiceConnection;
Matthew Xie32ab77b2013-05-08 19:26:57 -070039import android.content.pm.PackageManager;
Benjamin Franze8b98922014-11-12 15:57:54 +000040import android.content.pm.UserInfo;
Wei Wange4a744b2015-06-11 17:50:29 -070041import android.database.ContentObserver;
Zhihai Xu40874a02012-10-08 17:57:03 -070042import android.os.Binder;
fredc0f420372012-04-12 00:02:00 -070043import android.os.Handler;
fredc0f420372012-04-12 00:02:00 -070044import android.os.IBinder;
Zhihai Xu40874a02012-10-08 17:57:03 -070045import android.os.Looper;
fredc0f420372012-04-12 00:02:00 -070046import android.os.Message;
Andre Eisenbach14dcb5f2014-12-05 09:31:30 -080047import android.os.ParcelFileDescriptor;
Zhihai Xu40874a02012-10-08 17:57:03 -070048import android.os.Process;
fredcd6883532012-04-25 17:46:13 -070049import android.os.RemoteCallbackList;
fredc0f420372012-04-12 00:02:00 -070050import android.os.RemoteException;
Zhihai Xu40874a02012-10-08 17:57:03 -070051import android.os.SystemClock;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070052import android.os.UserHandle;
Benjamin Franze8b98922014-11-12 15:57:54 +000053import android.os.UserManager;
fredc0f420372012-04-12 00:02:00 -070054import android.provider.Settings;
Wei Wang67d84162015-04-26 17:04:29 -070055import android.provider.Settings.SettingNotFoundException;
fredc0f420372012-04-12 00:02:00 -070056import android.util.Log;
Mike Lockwood726d4de2014-10-28 14:06:28 -070057
58import java.io.FileDescriptor;
Andre Eisenbach14dcb5f2014-12-05 09:31:30 -080059import java.io.IOException;
Mike Lockwood726d4de2014-10-28 14:06:28 -070060import java.io.PrintWriter;
Benjamin Franze8b98922014-11-12 15:57:54 +000061import java.util.HashMap;
62import java.util.Map;
Miao Chou658bf2f2015-06-26 17:14:35 -070063
fredc0f420372012-04-12 00:02:00 -070064class BluetoothManagerService extends IBluetoothManager.Stub {
65 private static final String TAG = "BluetoothManagerService";
Pavlin Radoslavovb5a479c2015-06-20 22:55:04 -070066 private static final boolean DBG = false;
fredc0f420372012-04-12 00:02:00 -070067
fredc0f420372012-04-12 00:02:00 -070068 private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN;
69 private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH;
fredc0f420372012-04-12 00:02:00 -070070 private static final String ACTION_SERVICE_STATE_CHANGED="com.android.bluetooth.btservice.action.STATE_CHANGED";
71 private static final String EXTRA_ACTION="action";
Zhihai Xud31c3222012-10-31 16:08:57 -070072 private static final String SECURE_SETTINGS_BLUETOOTH_ADDR_VALID="bluetooth_addr_valid";
fredc0f420372012-04-12 00:02:00 -070073 private static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS="bluetooth_address";
74 private static final String SECURE_SETTINGS_BLUETOOTH_NAME="bluetooth_name";
fredc0f420372012-04-12 00:02:00 -070075 private static final int TIMEOUT_BIND_MS = 3000; //Maximum msec to wait for a bind
76 private static final int TIMEOUT_SAVE_MS = 500; //Maximum msec to wait for a save
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053077 //Maximum msec to wait for service restart
78 private static final int SERVICE_RESTART_TIME_MS = 200;
Zhihai Xudd9d17d2013-01-08 17:05:58 -080079 //Maximum msec to wait for restart due to error
80 private static final int ERROR_RESTART_TIME_MS = 3000;
Zhihai Xu40874a02012-10-08 17:57:03 -070081 //Maximum msec to delay MESSAGE_USER_SWITCHED
82 private static final int USER_SWITCHED_TIME_MS = 200;
Benjamin Franze8b98922014-11-12 15:57:54 +000083 // Delay for the addProxy function in msec
84 private static final int ADD_PROXY_DELAY_MS = 100;
fredc0f420372012-04-12 00:02:00 -070085
86 private static final int MESSAGE_ENABLE = 1;
87 private static final int MESSAGE_DISABLE = 2;
fredc649fe492012-04-19 01:07:18 -070088 private static final int MESSAGE_REGISTER_ADAPTER = 20;
89 private static final int MESSAGE_UNREGISTER_ADAPTER = 21;
90 private static final int MESSAGE_REGISTER_STATE_CHANGE_CALLBACK = 30;
91 private static final int MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK = 31;
92 private static final int MESSAGE_BLUETOOTH_SERVICE_CONNECTED = 40;
93 private static final int MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED = 41;
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +053094 private static final int MESSAGE_RESTART_BLUETOOTH_SERVICE = 42;
fredcbf072a72012-05-09 16:52:50 -070095 private static final int MESSAGE_BLUETOOTH_STATE_CHANGE=60;
fredc0f420372012-04-12 00:02:00 -070096 private static final int MESSAGE_TIMEOUT_BIND =100;
97 private static final int MESSAGE_TIMEOUT_UNBIND =101;
Zhihai Xu40874a02012-10-08 17:57:03 -070098 private static final int MESSAGE_USER_SWITCHED = 300;
Benjamin Franze8b98922014-11-12 15:57:54 +000099 private static final int MESSAGE_ADD_PROXY_DELAYED = 400;
100 private static final int MESSAGE_BIND_PROFILE_SERVICE = 401;
fredc0f420372012-04-12 00:02:00 -0700101 private static final int MAX_SAVE_RETRIES=3;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800102 private static final int MAX_ERROR_RESTART_RETRIES=6;
103
Zhihai Xu401202b2012-12-03 11:36:21 -0800104 // Bluetooth persisted setting is off
105 private static final int BLUETOOTH_OFF=0;
106 // Bluetooth persisted setting is on
107 // and Airplane mode won't affect Bluetooth state at start up
108 private static final int BLUETOOTH_ON_BLUETOOTH=1;
109 // Bluetooth persisted setting is on
110 // but Airplane mode will affect Bluetooth state at start up
111 // and Airplane mode will have higher priority.
112 private static final int BLUETOOTH_ON_AIRPLANE=2;
fredc0f420372012-04-12 00:02:00 -0700113
Matthew Xieddf7e472013-03-01 18:41:02 -0800114 private static final int SERVICE_IBLUETOOTH = 1;
115 private static final int SERVICE_IBLUETOOTHGATT = 2;
116
Sharvil Nanavati7c250052015-06-20 12:05:52 -0700117 private static final String[] DEVICE_TYPE_NAMES = new String[] {
118 "???",
119 "BR/EDR",
120 "LE",
121 "DUAL"
122 };
123
fredc0f420372012-04-12 00:02:00 -0700124 private final Context mContext;
Nitin Arorad055adb2015-03-02 15:03:51 -0800125 private static int mBleAppCount = 0;
Matthew Xiecdce0b92012-07-12 19:06:15 -0700126
127 // Locks are not provided for mName and mAddress.
128 // They are accessed in handler or broadcast receiver, same thread context.
fredc0f420372012-04-12 00:02:00 -0700129 private String mAddress;
130 private String mName;
Matthew Xie6fde3092012-07-11 17:10:07 -0700131 private final ContentResolver mContentResolver;
132 private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
133 private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
fredc649fe492012-04-19 01:07:18 -0700134 private IBluetooth mBluetooth;
Matthew Xieddf7e472013-03-01 18:41:02 -0800135 private IBluetoothGatt mBluetoothGatt;
fredc649fe492012-04-19 01:07:18 -0700136 private boolean mBinding;
137 private boolean mUnbinding;
Zhihai Xu401202b2012-12-03 11:36:21 -0800138 // used inside handler thread
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700139 private boolean mQuietEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -0800140 // configuarion from external IBinder call which is used to
141 // synchronize with broadcast receiver.
142 private boolean mQuietEnableExternal;
143 // configuarion from external IBinder call which is used to
144 // synchronize with broadcast receiver.
145 private boolean mEnableExternal;
146 // used inside handler thread
Zhihai Xu40874a02012-10-08 17:57:03 -0700147 private boolean mEnable;
148 private int mState;
Zhihai Xu40874a02012-10-08 17:57:03 -0700149 private final BluetoothHandler mHandler;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800150 private int mErrorRecoveryRetryCounter;
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200151 private final int mSystemUiUid;
fredc0f420372012-04-12 00:02:00 -0700152
Benjamin Franze8b98922014-11-12 15:57:54 +0000153 // Save a ProfileServiceConnections object for each of the bound
154 // bluetooth profile services
155 private final Map <Integer, ProfileServiceConnections> mProfileServices =
156 new HashMap <Integer, ProfileServiceConnections>();
157
fredc649fe492012-04-19 01:07:18 -0700158 private void registerForAirplaneMode(IntentFilter filter) {
159 final ContentResolver resolver = mContext.getContentResolver();
Christopher Tatec09cdce2012-09-10 16:50:14 -0700160 final String airplaneModeRadios = Settings.Global.getString(resolver,
161 Settings.Global.AIRPLANE_MODE_RADIOS);
162 final String toggleableRadios = Settings.Global.getString(resolver,
163 Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
fredc649fe492012-04-19 01:07:18 -0700164 boolean mIsAirplaneSensitive = airplaneModeRadios == null ? true :
Christopher Tatec09cdce2012-09-10 16:50:14 -0700165 airplaneModeRadios.contains(Settings.Global.RADIO_BLUETOOTH);
fredc649fe492012-04-19 01:07:18 -0700166 if (mIsAirplaneSensitive) {
167 filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
168 }
169 }
170
fredcbf072a72012-05-09 16:52:50 -0700171 private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
172 @Override
173 public void onBluetoothStateChange(int prevState, int newState) throws RemoteException {
174 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE,prevState,newState);
175 mHandler.sendMessage(msg);
176 }
177 };
178
179 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
fredc0f420372012-04-12 00:02:00 -0700180 @Override
181 public void onReceive(Context context, Intent intent) {
182 String action = intent.getAction();
fredcbf072a72012-05-09 16:52:50 -0700183 if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action)) {
fredc0f420372012-04-12 00:02:00 -0700184 String newName = intent.getStringExtra(BluetoothAdapter.EXTRA_LOCAL_NAME);
Freda8c6df02012-07-11 10:25:23 -0700185 if (DBG) Log.d(TAG, "Bluetooth Adapter name changed to " + newName);
fredc0f420372012-04-12 00:02:00 -0700186 if (newName != null) {
187 storeNameAndAddress(newName, null);
188 }
fredc649fe492012-04-19 01:07:18 -0700189 } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
Zhihai Xu401202b2012-12-03 11:36:21 -0800190 synchronized(mReceiver) {
191 if (isBluetoothPersistedStateOn()) {
192 if (isAirplaneModeOn()) {
193 persistBluetoothSetting(BLUETOOTH_ON_AIRPLANE);
194 } else {
195 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
196 }
197 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800198
199 int st = BluetoothAdapter.STATE_OFF;
200 if (mBluetooth != null) {
201 try {
202 st = mBluetooth.getState();
203 } catch (RemoteException e) {
204 Log.e(TAG,"Unable to call getState", e);
205 }
206 }
207 Log.d(TAG, "state" + st);
208
Zhihai Xu401202b2012-12-03 11:36:21 -0800209 if (isAirplaneModeOn()) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800210 // Clear registered LE apps to force shut-off
211 synchronized (this) {
212 mBleAppCount = 0;
Nitin Arora11f83882015-05-07 18:45:44 -0700213 mBleApps.clear();
Nitin Arorad055adb2015-03-02 15:03:51 -0800214 }
215 if (st == BluetoothAdapter.STATE_BLE_ON) {
216 //if state is BLE_ON make sure you trigger disableBLE part
217 try {
218 if (mBluetooth != null) {
219 mBluetooth.onBrEdrDown();
220 mEnableExternal = false;
221 }
222 } catch(RemoteException e) {
223 Log.e(TAG,"Unable to call onBrEdrDown", e);
224 }
225 } else if (st == BluetoothAdapter.STATE_ON){
226 // disable without persisting the setting
227 Log.d(TAG, "Calling disable");
228 sendDisableMsg();
229 }
Zhihai Xu401202b2012-12-03 11:36:21 -0800230 } else if (mEnableExternal) {
231 // enable without persisting the setting
Nitin Arorad055adb2015-03-02 15:03:51 -0800232 Log.d(TAG, "Calling enable");
Zhihai Xu401202b2012-12-03 11:36:21 -0800233 sendEnableMsg(mQuietEnableExternal);
234 }
fredc649fe492012-04-19 01:07:18 -0700235 }
fredc0f420372012-04-12 00:02:00 -0700236 }
237 }
238 };
239
240 BluetoothManagerService(Context context) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700241 mHandler = new BluetoothHandler(IoThread.get().getLooper());
Zhihai Xu40874a02012-10-08 17:57:03 -0700242
fredc0f420372012-04-12 00:02:00 -0700243 mContext = context;
244 mBluetooth = null;
Nitin Arorad055adb2015-03-02 15:03:51 -0800245 mBluetoothGatt = null;
fredc0f420372012-04-12 00:02:00 -0700246 mBinding = false;
247 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700248 mEnable = false;
249 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu401202b2012-12-03 11:36:21 -0800250 mQuietEnableExternal = false;
251 mEnableExternal = false;
fredc0f420372012-04-12 00:02:00 -0700252 mAddress = null;
253 mName = null;
Zhihai Xudd9d17d2013-01-08 17:05:58 -0800254 mErrorRecoveryRetryCounter = 0;
fredc0f420372012-04-12 00:02:00 -0700255 mContentResolver = context.getContentResolver();
Wei Wange4a744b2015-06-11 17:50:29 -0700256 // Observe BLE scan only mode settings change.
257 registerForBleScanModeChange();
fredcd6883532012-04-25 17:46:13 -0700258 mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
259 mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
Miao Chou658bf2f2015-06-26 17:14:35 -0700260 IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
Matthew Xie6fde3092012-07-11 17:10:07 -0700261 registerForAirplaneMode(filter);
Dianne Hackbornd83a0962014-05-02 16:28:33 -0700262 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Matthew Xie6fde3092012-07-11 17:10:07 -0700263 mContext.registerReceiver(mReceiver, filter);
fredc0f420372012-04-12 00:02:00 -0700264 loadStoredNameAndAddress();
Zhihai Xu401202b2012-12-03 11:36:21 -0800265 if (isBluetoothPersistedStateOn()) {
266 mEnableExternal = true;
fredc0f420372012-04-12 00:02:00 -0700267 }
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200268
269 int sysUiUid = -1;
270 try {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700271 sysUiUid = mContext.getPackageManager().getPackageUidAsUser("com.android.systemui",
Xiaohui Chen7c696362015-09-16 09:56:14 -0700272 UserHandle.USER_SYSTEM);
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200273 } catch (PackageManager.NameNotFoundException e) {
Joe LaPennaacddf2b2015-09-04 12:52:42 -0700274 // Some platforms, such as wearables do not have a system ui.
275 Log.w(TAG, "Unable to resolve SystemUI's UID.", e);
Adrian Roosbd9a9a52014-08-18 15:31:57 +0200276 }
277 mSystemUiUid = sysUiUid;
fredc0f420372012-04-12 00:02:00 -0700278 }
279
fredc649fe492012-04-19 01:07:18 -0700280 /**
281 * Returns true if airplane mode is currently on
282 */
283 private final boolean isAirplaneModeOn() {
Christopher Tatec09cdce2012-09-10 16:50:14 -0700284 return Settings.Global.getInt(mContext.getContentResolver(),
285 Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
fredc649fe492012-04-19 01:07:18 -0700286 }
287
288 /**
289 * Returns true if the Bluetooth saved state is "on"
290 */
291 private final boolean isBluetoothPersistedStateOn() {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700292 return Settings.Global.getInt(mContentResolver,
Zhihai Xu401202b2012-12-03 11:36:21 -0800293 Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF;
294 }
295
296 /**
297 * Returns true if the Bluetooth saved state is BLUETOOTH_ON_BLUETOOTH
298 */
299 private final boolean isBluetoothPersistedStateOnBluetooth() {
300 return Settings.Global.getInt(mContentResolver,
301 Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH;
fredc649fe492012-04-19 01:07:18 -0700302 }
303
304 /**
305 * Save the Bluetooth on/off state
306 *
307 */
Zhihai Xu401202b2012-12-03 11:36:21 -0800308 private void persistBluetoothSetting(int value) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700309 Settings.Global.putInt(mContext.getContentResolver(),
310 Settings.Global.BLUETOOTH_ON,
Zhihai Xu401202b2012-12-03 11:36:21 -0800311 value);
fredc649fe492012-04-19 01:07:18 -0700312 }
313
314 /**
315 * Returns true if the Bluetooth Adapter's name and address is
316 * locally cached
317 * @return
318 */
fredc0f420372012-04-12 00:02:00 -0700319 private boolean isNameAndAddressSet() {
320 return mName !=null && mAddress!= null && mName.length()>0 && mAddress.length()>0;
321 }
322
fredc649fe492012-04-19 01:07:18 -0700323 /**
324 * Retrieve the Bluetooth Adapter's name and address and save it in
325 * in the local cache
326 */
fredc0f420372012-04-12 00:02:00 -0700327 private void loadStoredNameAndAddress() {
328 if (DBG) Log.d(TAG, "Loading stored name and address");
Zhihai Xud31c3222012-10-31 16:08:57 -0700329 if (mContext.getResources().getBoolean
330 (com.android.internal.R.bool.config_bluetooth_address_validation) &&
331 Settings.Secure.getInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 0) == 0) {
332 // if the valid flag is not set, don't load the address and name
333 if (DBG) Log.d(TAG, "invalid bluetooth name and address stored");
334 return;
335 }
fredc0f420372012-04-12 00:02:00 -0700336 mName = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME);
337 mAddress = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
Zhihai Xud31c3222012-10-31 16:08:57 -0700338 if (DBG) Log.d(TAG, "Stored bluetooth Name=" + mName + ",Address=" + mAddress);
fredc0f420372012-04-12 00:02:00 -0700339 }
340
fredc649fe492012-04-19 01:07:18 -0700341 /**
342 * Save the Bluetooth name and address in the persistent store.
343 * Only non-null values will be saved.
344 * @param name
345 * @param address
346 */
fredc0f420372012-04-12 00:02:00 -0700347 private void storeNameAndAddress(String name, String address) {
348 if (name != null) {
349 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_NAME, name);
fredc0f420372012-04-12 00:02:00 -0700350 mName = name;
fredc649fe492012-04-19 01:07:18 -0700351 if (DBG) Log.d(TAG,"Stored Bluetooth name: " +
352 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_NAME));
fredc0f420372012-04-12 00:02:00 -0700353 }
354
355 if (address != null) {
356 Settings.Secure.putString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS, address);
fredc0f420372012-04-12 00:02:00 -0700357 mAddress=address;
fredc649fe492012-04-19 01:07:18 -0700358 if (DBG) Log.d(TAG,"Stored Bluetoothaddress: " +
359 Settings.Secure.getString(mContentResolver,SECURE_SETTINGS_BLUETOOTH_ADDRESS));
fredc0f420372012-04-12 00:02:00 -0700360 }
Zhihai Xud31c3222012-10-31 16:08:57 -0700361
362 if ((name != null) && (address != null)) {
363 Settings.Secure.putInt(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDR_VALID, 1);
364 }
fredc0f420372012-04-12 00:02:00 -0700365 }
366
367 public IBluetooth registerAdapter(IBluetoothManagerCallback callback){
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700368 if (callback == null) {
369 Log.w(TAG, "Callback is null in registerAdapter");
370 return null;
371 }
fredc0f420372012-04-12 00:02:00 -0700372 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_ADAPTER);
373 msg.obj = callback;
374 mHandler.sendMessage(msg);
375 synchronized(mConnection) {
376 return mBluetooth;
377 }
378 }
379
380 public void unregisterAdapter(IBluetoothManagerCallback callback) {
Natalie Silvanovich55db6462014-05-01 16:12:23 -0700381 if (callback == null) {
382 Log.w(TAG, "Callback is null in unregisterAdapter");
383 return;
384 }
fredc0f420372012-04-12 00:02:00 -0700385 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
386 "Need BLUETOOTH permission");
387 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_ADAPTER);
388 msg.obj = callback;
389 mHandler.sendMessage(msg);
390 }
391
392 public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
393 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
394 "Need BLUETOOTH permission");
395 Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
396 msg.obj = callback;
397 mHandler.sendMessage(msg);
398 }
399
400 public void unregisterStateChangeCallback(IBluetoothStateChangeCallback callback) {
401 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
402 "Need BLUETOOTH permission");
403 Message msg = mHandler.obtainMessage(MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK);
404 msg.obj = callback;
405 mHandler.sendMessage(msg);
406 }
407
408 public boolean isEnabled() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800409 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
410 (!checkIfCallerIsForegroundUser())) {
411 Log.w(TAG,"isEnabled(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700412 return false;
413 }
414
fredc0f420372012-04-12 00:02:00 -0700415 synchronized(mConnection) {
416 try {
417 return (mBluetooth != null && mBluetooth.isEnabled());
418 } catch (RemoteException e) {
419 Log.e(TAG, "isEnabled()", e);
420 }
421 }
422 return false;
423 }
424
Nitin Arorad055adb2015-03-02 15:03:51 -0800425 class ClientDeathRecipient implements IBinder.DeathRecipient {
426 public void binderDied() {
427 if (DBG) Log.d(TAG, "Binder is dead - unregister Ble App");
428 if (mBleAppCount > 0) --mBleAppCount;
429
430 if (mBleAppCount == 0) {
431 if (DBG) Log.d(TAG, "Disabling LE only mode after application crash");
432 try {
433 if (mBluetooth != null) {
434 mBluetooth.onBrEdrDown();
435 }
436 } catch(RemoteException e) {
437 Log.e(TAG,"Unable to call onBrEdrDown", e);
438 }
439 }
440 }
441 }
442
443 /** Internal death rec list */
444 Map<IBinder, ClientDeathRecipient> mBleApps = new HashMap<IBinder, ClientDeathRecipient>();
445
Wei Wang67d84162015-04-26 17:04:29 -0700446 @Override
447 public boolean isBleScanAlwaysAvailable() {
448 try {
449 return (Settings.Global.getInt(mContentResolver,
450 Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE)) != 0;
451 } catch (SettingNotFoundException e) {
452 }
453 return false;
454 }
455
Wei Wange4a744b2015-06-11 17:50:29 -0700456 // Monitor change of BLE scan only mode settings.
457 private void registerForBleScanModeChange() {
458 ContentObserver contentObserver = new ContentObserver(null) {
459 @Override
460 public void onChange(boolean selfChange) {
461 if (!isBleScanAlwaysAvailable()) {
462 disableBleScanMode();
463 clearBleApps();
464 try {
465 if (mBluetooth != null) mBluetooth.onBrEdrDown();
466 } catch (RemoteException e) {
467 Log.e(TAG, "error when disabling bluetooth", e);
468 }
469 }
470 }
471 };
472
473 mContentResolver.registerContentObserver(
474 Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE),
475 false, contentObserver);
476 }
477
478 // Disable ble scan only mode.
479 private void disableBleScanMode() {
480 try {
481 if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) {
482 if (DBG) Log.d(TAG, "Reseting the mEnable flag for clean disable");
483 mEnable = false;
484 }
485 } catch (RemoteException e) {
486 Log.e(TAG, "getState()", e);
487 }
488 }
489
Nitin Arorad055adb2015-03-02 15:03:51 -0800490 public int updateBleAppCount(IBinder token, boolean enable) {
491 if (enable) {
492 ClientDeathRecipient r = mBleApps.get(token);
493 if (r == null) {
494 ClientDeathRecipient deathRec = new ClientDeathRecipient();
495 try {
496 token.linkToDeath(deathRec, 0);
497 } catch (RemoteException ex) {
498 throw new IllegalArgumentException("Wake lock is already dead.");
499 }
500 mBleApps.put(token, deathRec);
501 synchronized (this) {
502 ++mBleAppCount;
503 }
504 if (DBG) Log.d(TAG, "Registered for death Notification");
505 }
506
507 } else {
508 ClientDeathRecipient r = mBleApps.get(token);
509 if (r != null) {
Wei Wange4a744b2015-06-11 17:50:29 -0700510 // Unregister death recipient as the app goes away.
511 token.unlinkToDeath(r, 0);
Nitin Arorad055adb2015-03-02 15:03:51 -0800512 mBleApps.remove(token);
513 synchronized (this) {
514 if (mBleAppCount > 0) --mBleAppCount;
515 }
516 if (DBG) Log.d(TAG, "Unregistered for death Notification");
517 }
518 }
519 if (DBG) Log.d(TAG, "Updated BleAppCount" + mBleAppCount);
520 if (mBleAppCount == 0 && mEnable) {
Wei Wange4a744b2015-06-11 17:50:29 -0700521 disableBleScanMode();
Nitin Arorad055adb2015-03-02 15:03:51 -0800522 }
523 return mBleAppCount;
524 }
525
Wei Wange4a744b2015-06-11 17:50:29 -0700526 // Clear all apps using BLE scan only mode.
527 private void clearBleApps() {
528 synchronized (this) {
529 mBleApps.clear();
530 mBleAppCount = 0;
531 }
532 }
533
Nitin Arorad055adb2015-03-02 15:03:51 -0800534 /** @hide*/
535 public boolean isBleAppPresent() {
536 if (DBG) Log.d(TAG, "isBleAppPresent() count: " + mBleAppCount);
537 return (mBleAppCount > 0);
538 }
539
540 /**
541 * Action taken when GattService is turned off
542 */
543 private void onBluetoothGattServiceUp() {
544 if (DBG) Log.d(TAG,"BluetoothGatt Service is Up");
545 try{
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700546 if (isBleAppPresent() == false && mBluetooth != null
547 && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
Nitin Arorad055adb2015-03-02 15:03:51 -0800548 mBluetooth.onLeServiceUp();
549
550 // waive WRITE_SECURE_SETTINGS permission check
551 long callingIdentity = Binder.clearCallingIdentity();
552 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
553 Binder.restoreCallingIdentity(callingIdentity);
554 }
555 } catch(RemoteException e) {
556 Log.e(TAG,"Unable to call onServiceUp", e);
557 }
558 }
559
560 /**
561 * Inform BluetoothAdapter instances that BREDR part is down
562 * and turn off all service and stack if no LE app needs it
563 */
564 private void sendBrEdrDownCallback() {
565 if (DBG) Log.d(TAG,"Calling sendBrEdrDownCallback callbacks");
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700566
567 if(mBluetooth == null) {
568 Log.w(TAG, "Bluetooth handle is null");
569 return;
570 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800571
572 if (isBleAppPresent() == false) {
573 try {
574 mBluetooth.onBrEdrDown();
575 } catch(RemoteException e) {
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700576 Log.e(TAG, "Call to onBrEdrDown() failed.", e);
Nitin Arorad055adb2015-03-02 15:03:51 -0800577 }
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700578 } else {
579 // Need to stay at BLE ON. Disconnect all Gatt connections
Nitin Arorad055adb2015-03-02 15:03:51 -0800580 try{
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700581 mBluetoothGatt.unregAll();
Nitin Arorad055adb2015-03-02 15:03:51 -0800582 } catch(RemoteException e) {
Nitin Arorabdfaa7f2015-04-29 12:35:03 -0700583 Log.e(TAG, "Unable to disconnect all apps.", e);
Nitin Arorad055adb2015-03-02 15:03:51 -0800584 }
585 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800586 }
587
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700588 public boolean enableNoAutoConnect()
589 {
590 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
591 "Need BLUETOOTH ADMIN permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700592
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700593 if (DBG) {
594 Log.d(TAG,"enableNoAutoConnect(): mBluetooth =" + mBluetooth +
595 " mBinding = " + mBinding);
596 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800597 int callingAppId = UserHandle.getAppId(Binder.getCallingUid());
598
599 if (callingAppId != Process.NFC_UID) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700600 throw new SecurityException("no permission to enable Bluetooth quietly");
601 }
Martijn Coenen8385c5a2012-11-29 10:14:16 -0800602
Zhihai Xu401202b2012-12-03 11:36:21 -0800603 synchronized(mReceiver) {
604 mQuietEnableExternal = true;
605 mEnableExternal = true;
606 sendEnableMsg(true);
607 }
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -0700608 return true;
609
610 }
fredc0f420372012-04-12 00:02:00 -0700611 public boolean enable() {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800612 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
613 (!checkIfCallerIsForegroundUser())) {
614 Log.w(TAG,"enable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700615 return false;
fredcf2458862012-04-16 15:18:27 -0700616 }
617
Zhihai Xu401202b2012-12-03 11:36:21 -0800618 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
619 "Need BLUETOOTH ADMIN permission");
620 if (DBG) {
621 Log.d(TAG,"enable(): mBluetooth =" + mBluetooth +
622 " mBinding = " + mBinding);
623 }
624
625 synchronized(mReceiver) {
626 mQuietEnableExternal = false;
627 mEnableExternal = true;
628 // waive WRITE_SECURE_SETTINGS permission check
Zhihai Xu401202b2012-12-03 11:36:21 -0800629 sendEnableMsg(false);
630 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800631 if (DBG) Log.d(TAG, "enable returning");
Zhihai Xu401202b2012-12-03 11:36:21 -0800632 return true;
fredc0f420372012-04-12 00:02:00 -0700633 }
634
635 public boolean disable(boolean persist) {
636 mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
637 "Need BLUETOOTH ADMIN permissicacheNameAndAddresson");
Zhihai Xu40874a02012-10-08 17:57:03 -0700638
Zhihai Xu6eb76522012-11-29 15:41:04 -0800639 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
640 (!checkIfCallerIsForegroundUser())) {
641 Log.w(TAG,"disable(): not allowed for non-active and non system user");
Zhihai Xu40874a02012-10-08 17:57:03 -0700642 return false;
643 }
644
fredcf2458862012-04-16 15:18:27 -0700645 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700646 Log.d(TAG,"disable(): mBluetooth = " + mBluetooth +
647 " mBinding = " + mBinding);
648 }
fredcf2458862012-04-16 15:18:27 -0700649
Zhihai Xu401202b2012-12-03 11:36:21 -0800650 synchronized(mReceiver) {
651 if (persist) {
652 // waive WRITE_SECURE_SETTINGS permission check
653 long callingIdentity = Binder.clearCallingIdentity();
654 persistBluetoothSetting(BLUETOOTH_OFF);
655 Binder.restoreCallingIdentity(callingIdentity);
656 }
657 mEnableExternal = false;
658 sendDisableMsg();
659 }
fredc0f420372012-04-12 00:02:00 -0700660 return true;
661 }
662
fredc649fe492012-04-19 01:07:18 -0700663 public void unbindAndFinish() {
fredcf2458862012-04-16 15:18:27 -0700664 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -0700665 Log.d(TAG,"unbindAndFinish(): " + mBluetooth +
666 " mBinding = " + mBinding);
fredcf2458862012-04-16 15:18:27 -0700667 }
668
fredc0f420372012-04-12 00:02:00 -0700669 synchronized (mConnection) {
670 if (mUnbinding) return;
671 mUnbinding = true;
Zhihai Xu40874a02012-10-08 17:57:03 -0700672 if (mBluetooth != null) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800673 //Unregister callback object
674 try {
675 mBluetooth.unregisterCallback(mBluetoothCallback);
676 } catch (RemoteException re) {
677 Log.e(TAG, "Unable to unregister BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -0700678 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800679
fredc0f420372012-04-12 00:02:00 -0700680 if (DBG) Log.d(TAG, "Sending unbind request.");
fredcd6883532012-04-25 17:46:13 -0700681 mBluetooth = null;
682 //Unbind
fredc0f420372012-04-12 00:02:00 -0700683 mContext.unbindService(mConnection);
fredcd6883532012-04-25 17:46:13 -0700684 mUnbinding = false;
Zhihai Xu40874a02012-10-08 17:57:03 -0700685 mBinding = false;
fredcf2458862012-04-16 15:18:27 -0700686 } else {
687 mUnbinding=false;
fredc0f420372012-04-12 00:02:00 -0700688 }
Nitin Arorad055adb2015-03-02 15:03:51 -0800689 mBluetoothGatt = null;
fredc0f420372012-04-12 00:02:00 -0700690 }
691 }
692
Matthew Xieddf7e472013-03-01 18:41:02 -0800693 public IBluetoothGatt getBluetoothGatt() {
694 // sync protection
695 return mBluetoothGatt;
696 }
697
Benjamin Franze8b98922014-11-12 15:57:54 +0000698 @Override
699 public boolean bindBluetoothProfileService(int bluetoothProfile,
700 IBluetoothProfileServiceConnection proxy) {
701 if (!mEnable) {
702 if (DBG) {
703 Log.d(TAG, "Trying to bind to profile: " + bluetoothProfile +
704 ", while Bluetooth was disabled");
705 }
706 return false;
707 }
708 synchronized (mProfileServices) {
709 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
710 if (psc == null) {
711 if (DBG) {
712 Log.d(TAG, "Creating new ProfileServiceConnections object for"
713 + " profile: " + bluetoothProfile);
714 }
Benjamin Franz5b614592014-12-09 18:58:45 +0000715
716 if (bluetoothProfile != BluetoothProfile.HEADSET) return false;
717
718 Intent intent = new Intent(IBluetoothHeadset.class.getName());
Benjamin Franze8b98922014-11-12 15:57:54 +0000719 psc = new ProfileServiceConnections(intent);
Benjamin Franz5b614592014-12-09 18:58:45 +0000720 if (!psc.bindService()) return false;
721
Benjamin Franze8b98922014-11-12 15:57:54 +0000722 mProfileServices.put(new Integer(bluetoothProfile), psc);
Benjamin Franze8b98922014-11-12 15:57:54 +0000723 }
724 }
725
726 // Introducing a delay to give the client app time to prepare
727 Message addProxyMsg = mHandler.obtainMessage(MESSAGE_ADD_PROXY_DELAYED);
728 addProxyMsg.arg1 = bluetoothProfile;
729 addProxyMsg.obj = proxy;
730 mHandler.sendMessageDelayed(addProxyMsg, ADD_PROXY_DELAY_MS);
731 return true;
732 }
733
734 @Override
735 public void unbindBluetoothProfileService(int bluetoothProfile,
736 IBluetoothProfileServiceConnection proxy) {
737 synchronized (mProfileServices) {
738 ProfileServiceConnections psc = mProfileServices.get(new Integer(bluetoothProfile));
739 if (psc == null) {
740 return;
741 }
742 psc.removeProxy(proxy);
743 }
744 }
745
746 private void unbindAllBluetoothProfileServices() {
747 synchronized (mProfileServices) {
748 for (Integer i : mProfileServices.keySet()) {
749 ProfileServiceConnections psc = mProfileServices.get(i);
Benjamin Franz5b614592014-12-09 18:58:45 +0000750 try {
751 mContext.unbindService(psc);
752 } catch (IllegalArgumentException e) {
753 Log.e(TAG, "Unable to unbind service with intent: " + psc.mIntent, e);
754 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000755 psc.removeAllProxies();
756 }
757 mProfileServices.clear();
758 }
759 }
760
761 /**
Miao Chou658bf2f2015-06-26 17:14:35 -0700762 * Send enable message and set adapter name and address. Called when the boot phase becomes
763 * PHASE_SYSTEM_SERVICES_READY.
764 */
765 public void handleOnBootPhase() {
766 if (DBG) Log.d(TAG, "Bluetooth boot completed");
767 if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {
768 if (DBG) Log.d(TAG, "Auto-enabling Bluetooth.");
769 sendEnableMsg(mQuietEnableExternal);
770 }
Miao Chou658bf2f2015-06-26 17:14:35 -0700771 }
772
773 /**
774 * Called when switching to a different foreground user.
775 */
776 public void handleOnSwitchUser(int userHandle) {
777 if (DBG) Log.d(TAG, "Bluetooth user switched");
778 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle, 0));
779 }
780
781 /**
Benjamin Franze8b98922014-11-12 15:57:54 +0000782 * This class manages the clients connected to a given ProfileService
783 * and maintains the connection with that service.
784 */
785 final private class ProfileServiceConnections implements ServiceConnection,
786 IBinder.DeathRecipient {
787 final RemoteCallbackList<IBluetoothProfileServiceConnection> mProxies =
788 new RemoteCallbackList <IBluetoothProfileServiceConnection>();
789 IBinder mService;
790 ComponentName mClassName;
791 Intent mIntent;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700792 boolean mInvokingProxyCallbacks = false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000793
794 ProfileServiceConnections(Intent intent) {
795 mService = null;
796 mClassName = null;
797 mIntent = intent;
798 }
799
Benjamin Franz5b614592014-12-09 18:58:45 +0000800 private boolean bindService() {
801 if (mIntent != null && mService == null &&
802 doBind(mIntent, this, 0, UserHandle.CURRENT_OR_SELF)) {
Benjamin Franze8b98922014-11-12 15:57:54 +0000803 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
804 msg.obj = this;
805 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
Benjamin Franz5b614592014-12-09 18:58:45 +0000806 return true;
Benjamin Franze8b98922014-11-12 15:57:54 +0000807 }
Benjamin Franz5b614592014-12-09 18:58:45 +0000808 Log.w(TAG, "Unable to bind with intent: " + mIntent);
809 return false;
Benjamin Franze8b98922014-11-12 15:57:54 +0000810 }
811
812 private void addProxy(IBluetoothProfileServiceConnection proxy) {
813 mProxies.register(proxy);
814 if (mService != null) {
815 try{
816 proxy.onServiceConnected(mClassName, mService);
817 } catch (RemoteException e) {
818 Log.e(TAG, "Unable to connect to proxy", e);
819 }
820 } else {
821 if (!mHandler.hasMessages(MESSAGE_BIND_PROFILE_SERVICE, this)) {
822 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
823 msg.obj = this;
824 mHandler.sendMessage(msg);
825 }
826 }
827 }
828
829 private void removeProxy(IBluetoothProfileServiceConnection proxy) {
830 if (proxy != null) {
831 if (mProxies.unregister(proxy)) {
832 try {
833 proxy.onServiceDisconnected(mClassName);
834 } catch (RemoteException e) {
835 Log.e(TAG, "Unable to disconnect proxy", e);
836 }
837 }
838 } else {
839 Log.w(TAG, "Trying to remove a null proxy");
840 }
841 }
842
843 private void removeAllProxies() {
844 onServiceDisconnected(mClassName);
845 mProxies.kill();
846 }
847
848 @Override
849 public void onServiceConnected(ComponentName className, IBinder service) {
850 // remove timeout message
851 mHandler.removeMessages(MESSAGE_BIND_PROFILE_SERVICE, this);
852 mService = service;
853 mClassName = className;
854 try {
855 mService.linkToDeath(this, 0);
856 } catch (RemoteException e) {
857 Log.e(TAG, "Unable to linkToDeath", e);
858 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700859
860 if (mInvokingProxyCallbacks) {
861 Log.e(TAG, "Proxy callbacks already in progress.");
862 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000863 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700864 mInvokingProxyCallbacks = true;
865
866 final int n = mProxies.beginBroadcast();
867 try {
868 for (int i = 0; i < n; i++) {
869 try {
870 mProxies.getBroadcastItem(i).onServiceConnected(className, service);
871 } catch (RemoteException e) {
872 Log.e(TAG, "Unable to connect to proxy", e);
873 }
874 }
875 } finally {
876 mProxies.finishBroadcast();
877 mInvokingProxyCallbacks = false;
878 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000879 }
880
881 @Override
882 public void onServiceDisconnected(ComponentName className) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700883 if (mService == null) return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000884 mService.unlinkToDeath(this, 0);
885 mService = null;
886 mClassName = null;
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700887
888 if (mInvokingProxyCallbacks) {
889 Log.e(TAG, "Proxy callbacks already in progress.");
890 return;
Benjamin Franze8b98922014-11-12 15:57:54 +0000891 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700892 mInvokingProxyCallbacks = true;
893
894 final int n = mProxies.beginBroadcast();
895 try {
896 for (int i = 0; i < n; i++) {
897 try {
898 mProxies.getBroadcastItem(i).onServiceDisconnected(className);
899 } catch (RemoteException e) {
900 Log.e(TAG, "Unable to disconnect from proxy", e);
901 }
902 }
903 } finally {
904 mProxies.finishBroadcast();
905 mInvokingProxyCallbacks = false;
906 }
Benjamin Franze8b98922014-11-12 15:57:54 +0000907 }
908
909 @Override
910 public void binderDied() {
911 if (DBG) {
912 Log.w(TAG, "Profile service for profile: " + mClassName
913 + " died.");
914 }
915 onServiceDisconnected(mClassName);
916 // Trigger rebind
917 Message msg = mHandler.obtainMessage(MESSAGE_BIND_PROFILE_SERVICE);
918 msg.obj = this;
919 mHandler.sendMessageDelayed(msg, TIMEOUT_BIND_MS);
920 }
921 }
922
fredcbf072a72012-05-09 16:52:50 -0700923 private void sendBluetoothStateCallback(boolean isUp) {
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700924 try {
925 int n = mStateChangeCallbacks.beginBroadcast();
926 if (DBG) Log.d(TAG,"Broadcasting onBluetoothStateChange("+isUp+") to " + n + " receivers.");
927 for (int i=0; i <n;i++) {
928 try {
929 mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
930 } catch (RemoteException e) {
931 Log.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i , e);
932 }
fredcbf072a72012-05-09 16:52:50 -0700933 }
Andre Eisenbach3bf1ac52015-07-30 08:59:32 -0700934 } finally {
935 mStateChangeCallbacks.finishBroadcast();
fredcbf072a72012-05-09 16:52:50 -0700936 }
fredcbf072a72012-05-09 16:52:50 -0700937 }
938
939 /**
Zhihai Xu40874a02012-10-08 17:57:03 -0700940 * Inform BluetoothAdapter instances that Adapter service is up
941 */
942 private void sendBluetoothServiceUpCallback() {
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800943 if (DBG) Log.d(TAG,"Calling onBluetoothServiceUp callbacks");
944 try {
945 int n = mCallbacks.beginBroadcast();
946 Log.d(TAG,"Broadcasting onBluetoothServiceUp() to " + n + " receivers.");
947 for (int i=0; i <n;i++) {
948 try {
949 mCallbacks.getBroadcastItem(i).onBluetoothServiceUp(mBluetooth);
950 } catch (RemoteException e) {
951 Log.e(TAG, "Unable to call onBluetoothServiceUp() on callback #" + i, e);
Zhihai Xu40874a02012-10-08 17:57:03 -0700952 }
953 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800954 } finally {
955 mCallbacks.finishBroadcast();
Zhihai Xu40874a02012-10-08 17:57:03 -0700956 }
957 }
958 /**
fredcbf072a72012-05-09 16:52:50 -0700959 * Inform BluetoothAdapter instances that Adapter service is down
960 */
961 private void sendBluetoothServiceDownCallback() {
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800962 if (DBG) Log.d(TAG,"Calling onBluetoothServiceDown callbacks");
963 try {
964 int n = mCallbacks.beginBroadcast();
965 Log.d(TAG,"Broadcasting onBluetoothServiceDown() to " + n + " receivers.");
966 for (int i=0; i <n;i++) {
967 try {
968 mCallbacks.getBroadcastItem(i).onBluetoothServiceDown();
969 } catch (RemoteException e) {
970 Log.e(TAG, "Unable to call onBluetoothServiceDown() on callback #" + i, e);
fredcd6883532012-04-25 17:46:13 -0700971 }
972 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -0800973 } finally {
974 mCallbacks.finishBroadcast();
fredcd6883532012-04-25 17:46:13 -0700975 }
976 }
Svet Ganov408abf72015-05-12 19:13:36 -0700977
fredc0f420372012-04-12 00:02:00 -0700978 public String getAddress() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -0800979 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
Svet Ganov408abf72015-05-12 19:13:36 -0700980 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -0700981
Zhihai Xu6eb76522012-11-29 15:41:04 -0800982 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
Svet Ganov408abf72015-05-12 19:13:36 -0700983 (!checkIfCallerIsForegroundUser())) {
Zhihai Xu6eb76522012-11-29 15:41:04 -0800984 Log.w(TAG,"getAddress(): not allowed for non-active and non system user");
985 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -0700986 }
987
Svet Ganov408abf72015-05-12 19:13:36 -0700988 if (mContext.checkCallingOrSelfPermission(Manifest.permission.LOCAL_MAC_ADDRESS)
989 != PackageManager.PERMISSION_GRANTED) {
990 return BluetoothAdapter.DEFAULT_MAC_ADDRESS;
991 }
992
fredc116d1d462012-04-20 14:47:08 -0700993 synchronized(mConnection) {
994 if (mBluetooth != null) {
995 try {
996 return mBluetooth.getAddress();
997 } catch (RemoteException e) {
998 Log.e(TAG, "getAddress(): Unable to retrieve address remotely..Returning cached address",e);
999 }
1000 }
1001 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001002 // mAddress is accessed from outside.
1003 // It is alright without a lock. Here, bluetooth is off, no other thread is
1004 // changing mAddress
fredc0f420372012-04-12 00:02:00 -07001005 return mAddress;
1006 }
fredc649fe492012-04-19 01:07:18 -07001007
fredc0f420372012-04-12 00:02:00 -07001008 public String getName() {
Matthew Xieaf5ddbf2012-12-04 10:47:43 -08001009 mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM,
1010 "Need BLUETOOTH permission");
Zhihai Xu40874a02012-10-08 17:57:03 -07001011
Zhihai Xu6eb76522012-11-29 15:41:04 -08001012 if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
1013 (!checkIfCallerIsForegroundUser())) {
1014 Log.w(TAG,"getName(): not allowed for non-active and non system user");
1015 return null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001016 }
1017
fredc116d1d462012-04-20 14:47:08 -07001018 synchronized(mConnection) {
1019 if (mBluetooth != null) {
1020 try {
1021 return mBluetooth.getName();
1022 } catch (RemoteException e) {
1023 Log.e(TAG, "getName(): Unable to retrieve name remotely..Returning cached name",e);
1024 }
1025 }
1026 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001027 // mName is accessed from outside.
1028 // It alright without a lock. Here, bluetooth is off, no other thread is
1029 // changing mName
fredc0f420372012-04-12 00:02:00 -07001030 return mName;
1031 }
1032
fredc0f420372012-04-12 00:02:00 -07001033 private class BluetoothServiceConnection implements ServiceConnection {
fredc0f420372012-04-12 00:02:00 -07001034 public void onServiceConnected(ComponentName className, IBinder service) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001035 if (DBG) Log.d(TAG, "BluetoothServiceConnection: " + className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001036 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_CONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001037 // TBD if (className.getClassName().equals(IBluetooth.class.getName())) {
1038 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1039 msg.arg1 = SERVICE_IBLUETOOTH;
1040 // } else if (className.getClassName().equals(IBluetoothGatt.class.getName())) {
1041 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1042 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1043 } else {
1044 Log.e(TAG, "Unknown service connected: " + className.getClassName());
1045 return;
1046 }
fredc0f420372012-04-12 00:02:00 -07001047 msg.obj = service;
1048 mHandler.sendMessage(msg);
1049 }
1050
1051 public void onServiceDisconnected(ComponentName className) {
fredc0f420372012-04-12 00:02:00 -07001052 // Called if we unexpected disconnected.
Matthew Xieddf7e472013-03-01 18:41:02 -08001053 if (DBG) Log.d(TAG, "BluetoothServiceConnection, disconnected: " +
1054 className.getClassName());
fredc0f420372012-04-12 00:02:00 -07001055 Message msg = mHandler.obtainMessage(MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED);
Matthew Xieddf7e472013-03-01 18:41:02 -08001056 if (className.getClassName().equals("com.android.bluetooth.btservice.AdapterService")) {
1057 msg.arg1 = SERVICE_IBLUETOOTH;
1058 } else if (className.getClassName().equals("com.android.bluetooth.gatt.GattService")) {
1059 msg.arg1 = SERVICE_IBLUETOOTHGATT;
1060 } else {
1061 Log.e(TAG, "Unknown service disconnected: " + className.getClassName());
1062 return;
1063 }
fredc0f420372012-04-12 00:02:00 -07001064 mHandler.sendMessage(msg);
1065 }
1066 }
1067
1068 private BluetoothServiceConnection mConnection = new BluetoothServiceConnection();
1069
Zhihai Xu40874a02012-10-08 17:57:03 -07001070 private class BluetoothHandler extends Handler {
1071 public BluetoothHandler(Looper looper) {
1072 super(looper);
1073 }
1074
fredc0f420372012-04-12 00:02:00 -07001075 @Override
1076 public void handleMessage(Message msg) {
1077 if (DBG) Log.d (TAG, "Message: " + msg.what);
fredc0f420372012-04-12 00:02:00 -07001078 switch (msg.what) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001079 case MESSAGE_ENABLE:
fredcf2458862012-04-16 15:18:27 -07001080 if (DBG) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001081 Log.d(TAG, "MESSAGE_ENABLE: mBluetooth = " + mBluetooth);
fredc649fe492012-04-19 01:07:18 -07001082 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001083 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1084 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -08001085 handleEnable(msg.arg1 == 1);
fredc649fe492012-04-19 01:07:18 -07001086 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001087
fredc0f420372012-04-12 00:02:00 -07001088 case MESSAGE_DISABLE:
Zhihai Xu40874a02012-10-08 17:57:03 -07001089 mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
1090 if (mEnable && mBluetooth != null) {
1091 waitForOnOff(true, false);
1092 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001093 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001094 waitForOnOff(false, false);
1095 } else {
1096 mEnable = false;
Zhihai Xu401202b2012-12-03 11:36:21 -08001097 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001098 }
fredc0f420372012-04-12 00:02:00 -07001099 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001100
fredc0f420372012-04-12 00:02:00 -07001101 case MESSAGE_REGISTER_ADAPTER:
1102 {
1103 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001104 boolean added = mCallbacks.register(callback);
1105 Log.d(TAG,"Added callback: " + (callback == null? "null": callback) +":" +added );
fredc0f420372012-04-12 00:02:00 -07001106 }
1107 break;
1108 case MESSAGE_UNREGISTER_ADAPTER:
1109 {
1110 IBluetoothManagerCallback callback = (IBluetoothManagerCallback) msg.obj;
fredcd6883532012-04-25 17:46:13 -07001111 boolean removed = mCallbacks.unregister(callback);
1112 Log.d(TAG,"Removed callback: " + (callback == null? "null": callback) +":" + removed);
fredc0f420372012-04-12 00:02:00 -07001113 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001114 }
fredc0f420372012-04-12 00:02:00 -07001115 case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK:
1116 {
1117 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001118 if (callback != null) {
1119 mStateChangeCallbacks.register(callback);
1120 }
fredc0f420372012-04-12 00:02:00 -07001121 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001122 }
fredc0f420372012-04-12 00:02:00 -07001123 case MESSAGE_UNREGISTER_STATE_CHANGE_CALLBACK:
1124 {
1125 IBluetoothStateChangeCallback callback = (IBluetoothStateChangeCallback) msg.obj;
Matthew Xie9b693992013-10-10 11:21:40 -07001126 if (callback != null) {
1127 mStateChangeCallbacks.unregister(callback);
1128 }
fredc0f420372012-04-12 00:02:00 -07001129 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001130 }
Benjamin Franze8b98922014-11-12 15:57:54 +00001131 case MESSAGE_ADD_PROXY_DELAYED:
1132 {
1133 ProfileServiceConnections psc = mProfileServices.get(
1134 new Integer(msg.arg1));
1135 if (psc == null) {
1136 break;
1137 }
1138 IBluetoothProfileServiceConnection proxy =
1139 (IBluetoothProfileServiceConnection) msg.obj;
1140 psc.addProxy(proxy);
1141 break;
1142 }
1143 case MESSAGE_BIND_PROFILE_SERVICE:
1144 {
1145 ProfileServiceConnections psc = (ProfileServiceConnections) msg.obj;
1146 removeMessages(MESSAGE_BIND_PROFILE_SERVICE, msg.obj);
1147 if (psc == null) {
1148 break;
1149 }
1150 psc.bindService();
1151 break;
1152 }
fredc0f420372012-04-12 00:02:00 -07001153 case MESSAGE_BLUETOOTH_SERVICE_CONNECTED:
1154 {
Matthew Xieddf7e472013-03-01 18:41:02 -08001155 if (DBG) Log.d(TAG,"MESSAGE_BLUETOOTH_SERVICE_CONNECTED: " + msg.arg1);
fredc0f420372012-04-12 00:02:00 -07001156
1157 IBinder service = (IBinder) msg.obj;
1158 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001159 if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1160 mBluetoothGatt = IBluetoothGatt.Stub.asInterface(service);
Nitin Arorad055adb2015-03-02 15:03:51 -08001161 onBluetoothGattServiceUp();
Matthew Xieddf7e472013-03-01 18:41:02 -08001162 break;
1163 } // else must be SERVICE_IBLUETOOTH
1164
1165 //Remove timeout
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001166 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Matthew Xieddf7e472013-03-01 18:41:02 -08001167
fredc0f420372012-04-12 00:02:00 -07001168 mBinding = false;
1169 mBluetooth = IBluetooth.Stub.asInterface(service);
fredc0f420372012-04-12 00:02:00 -07001170
Zhihai Xuaf5971e2013-06-10 20:28:31 -07001171 try {
1172 boolean enableHciSnoopLog = (Settings.Secure.getInt(mContentResolver,
1173 Settings.Secure.BLUETOOTH_HCI_LOG, 0) == 1);
1174 if (!mBluetooth.configHciSnoopLog(enableHciSnoopLog)) {
1175 Log.e(TAG,"IBluetooth.configHciSnoopLog return false");
1176 }
1177 } catch (RemoteException e) {
1178 Log.e(TAG,"Unable to call configHciSnoopLog", e);
1179 }
1180
Matthew Xiecdce0b92012-07-12 19:06:15 -07001181 //Register callback object
fredcbf072a72012-05-09 16:52:50 -07001182 try {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001183 mBluetooth.registerCallback(mBluetoothCallback);
1184 } catch (RemoteException re) {
1185 Log.e(TAG, "Unable to register BluetoothCallback",re);
fredcbf072a72012-05-09 16:52:50 -07001186 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001187 //Inform BluetoothAdapter instances that service is up
Zhihai Xu40874a02012-10-08 17:57:03 -07001188 sendBluetoothServiceUpCallback();
1189
Matthew Xiecdce0b92012-07-12 19:06:15 -07001190 //Do enable request
1191 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001192 if (mQuietEnable == false) {
1193 if(!mBluetooth.enable()) {
1194 Log.e(TAG,"IBluetooth.enable() returned false");
1195 }
1196 }
1197 else
1198 {
1199 if(!mBluetooth.enableNoAutoConnect()) {
1200 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
1201 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001202 }
1203 } catch (RemoteException e) {
1204 Log.e(TAG,"Unable to call enable()",e);
1205 }
Freda8c6df02012-07-11 10:25:23 -07001206 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001207
1208 if (!mEnable) {
1209 waitForOnOff(true, false);
Zhihai Xu401202b2012-12-03 11:36:21 -08001210 handleDisable();
Zhihai Xu40874a02012-10-08 17:57:03 -07001211 waitForOnOff(false, false);
1212 }
fredc649fe492012-04-19 01:07:18 -07001213 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001214 }
fredc649fe492012-04-19 01:07:18 -07001215 case MESSAGE_TIMEOUT_BIND: {
1216 Log.e(TAG, "MESSAGE_TIMEOUT_BIND");
fredc0f420372012-04-12 00:02:00 -07001217 synchronized(mConnection) {
1218 mBinding = false;
1219 }
fredc649fe492012-04-19 01:07:18 -07001220 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001221 }
fredcbf072a72012-05-09 16:52:50 -07001222 case MESSAGE_BLUETOOTH_STATE_CHANGE:
fredc0f420372012-04-12 00:02:00 -07001223 {
fredcbf072a72012-05-09 16:52:50 -07001224 int prevState = msg.arg1;
1225 int newState = msg.arg2;
1226 if (DBG) Log.d(TAG, "MESSAGE_BLUETOOTH_STATE_CHANGE: prevState = " + prevState + ", newState=" + newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001227 mState = newState;
1228 bluetoothStateChangeHandler(prevState, newState);
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001229 // handle error state transition case from TURNING_ON to OFF
1230 // unbind and rebind bluetooth service and enable bluetooth
Nitin Arorad055adb2015-03-02 15:03:51 -08001231 if ((prevState == BluetoothAdapter.STATE_BLE_TURNING_ON) &&
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001232 (newState == BluetoothAdapter.STATE_OFF) &&
1233 (mBluetooth != null) && mEnable) {
1234 recoverBluetoothServiceFromError();
1235 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001236 if ((prevState == BluetoothAdapter.STATE_TURNING_ON) &&
1237 (newState == BluetoothAdapter.STATE_BLE_ON) &&
1238 (mBluetooth != null) && mEnable) {
1239 recoverBluetoothServiceFromError();
1240 }
1241 if (newState == BluetoothAdapter.STATE_ON ||
1242 newState == BluetoothAdapter.STATE_BLE_ON) {
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001243 // bluetooth is working, reset the counter
1244 if (mErrorRecoveryRetryCounter != 0) {
1245 Log.w(TAG, "bluetooth is recovered from error");
1246 mErrorRecoveryRetryCounter = 0;
1247 }
1248 }
fredc649fe492012-04-19 01:07:18 -07001249 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001250 }
fredc0f420372012-04-12 00:02:00 -07001251 case MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED:
1252 {
Matthew Xieddf7e472013-03-01 18:41:02 -08001253 Log.e(TAG, "MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED: " + msg.arg1);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301254 synchronized(mConnection) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001255 if (msg.arg1 == SERVICE_IBLUETOOTH) {
1256 // if service is unbinded already, do nothing and return
1257 if (mBluetooth == null) break;
1258 mBluetooth = null;
1259 } else if (msg.arg1 == SERVICE_IBLUETOOTHGATT) {
1260 mBluetoothGatt = null;
1261 break;
1262 } else {
1263 Log.e(TAG, "Bad msg.arg1: " + msg.arg1);
1264 break;
1265 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301266 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001267
1268 if (mEnable) {
1269 mEnable = false;
1270 // Send a Bluetooth Restart message
1271 Message restartMsg = mHandler.obtainMessage(
1272 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1273 mHandler.sendMessageDelayed(restartMsg,
1274 SERVICE_RESTART_TIME_MS);
1275 }
1276
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001277 sendBluetoothServiceDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001278
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001279 // Send BT state broadcast to update
1280 // the BT icon correctly
1281 if ((mState == BluetoothAdapter.STATE_TURNING_ON) ||
1282 (mState == BluetoothAdapter.STATE_ON)) {
1283 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1284 BluetoothAdapter.STATE_TURNING_OFF);
1285 mState = BluetoothAdapter.STATE_TURNING_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001286 }
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001287 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1288 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
1289 BluetoothAdapter.STATE_OFF);
1290 }
1291
1292 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1293 mState = BluetoothAdapter.STATE_OFF;
fredc649fe492012-04-19 01:07:18 -07001294 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001295 }
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301296 case MESSAGE_RESTART_BLUETOOTH_SERVICE:
1297 {
1298 Log.d(TAG, "MESSAGE_RESTART_BLUETOOTH_SERVICE:"
1299 +" Restart IBluetooth service");
1300 /* Enable without persisting the setting as
1301 it doesnt change when IBluetooth
1302 service restarts */
Zhihai Xu40874a02012-10-08 17:57:03 -07001303 mEnable = true;
Zhihai Xu401202b2012-12-03 11:36:21 -08001304 handleEnable(mQuietEnable);
Syed Ibrahim M1223e5a2012-08-29 18:07:26 +05301305 break;
1306 }
1307
fredc0f420372012-04-12 00:02:00 -07001308 case MESSAGE_TIMEOUT_UNBIND:
1309 {
fredc649fe492012-04-19 01:07:18 -07001310 Log.e(TAG, "MESSAGE_TIMEOUT_UNBIND");
fredc0f420372012-04-12 00:02:00 -07001311 synchronized(mConnection) {
1312 mUnbinding = false;
1313 }
fredc649fe492012-04-19 01:07:18 -07001314 break;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001315 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001316
1317 case MESSAGE_USER_SWITCHED:
1318 {
1319 if (DBG) {
1320 Log.d(TAG, "MESSAGE_USER_SWITCHED");
1321 }
1322 mHandler.removeMessages(MESSAGE_USER_SWITCHED);
1323 /* disable and enable BT when detect a user switch */
1324 if (mEnable && mBluetooth != null) {
1325 synchronized (mConnection) {
1326 if (mBluetooth != null) {
1327 //Unregister callback object
1328 try {
1329 mBluetooth.unregisterCallback(mBluetoothCallback);
1330 } catch (RemoteException re) {
1331 Log.e(TAG, "Unable to unregister",re);
1332 }
1333 }
1334 }
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001335
1336 if (mState == BluetoothAdapter.STATE_TURNING_OFF) {
1337 // MESSAGE_USER_SWITCHED happened right after MESSAGE_ENABLE
1338 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_OFF);
1339 mState = BluetoothAdapter.STATE_OFF;
1340 }
1341 if (mState == BluetoothAdapter.STATE_OFF) {
1342 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_TURNING_ON);
1343 mState = BluetoothAdapter.STATE_TURNING_ON;
1344 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001345
1346 waitForOnOff(true, false);
1347
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001348 if (mState == BluetoothAdapter.STATE_TURNING_ON) {
1349 bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
1350 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001351
Benjamin Franze8b98922014-11-12 15:57:54 +00001352 unbindAllBluetoothProfileServices();
Zhihai Xu40874a02012-10-08 17:57:03 -07001353 // disable
Zhihai Xu401202b2012-12-03 11:36:21 -08001354 handleDisable();
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001355 // Pbap service need receive STATE_TURNING_OFF intent to close
1356 bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
1357 BluetoothAdapter.STATE_TURNING_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001358
1359 waitForOnOff(false, true);
1360
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001361 bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
Zhihai Xu40874a02012-10-08 17:57:03 -07001362 BluetoothAdapter.STATE_OFF);
Zhihai Xu40874a02012-10-08 17:57:03 -07001363 sendBluetoothServiceDownCallback();
1364 synchronized (mConnection) {
1365 if (mBluetooth != null) {
1366 mBluetooth = null;
1367 //Unbind
1368 mContext.unbindService(mConnection);
1369 }
Pavlin Radoslavov29b8c4a2015-08-03 17:44:47 -07001370 mBluetoothGatt = null;
Zhihai Xu40874a02012-10-08 17:57:03 -07001371 }
1372 SystemClock.sleep(100);
1373
Zhihai Xu4e22ad32012-11-13 15:11:26 -08001374 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1375 mState = BluetoothAdapter.STATE_OFF;
Zhihai Xu40874a02012-10-08 17:57:03 -07001376 // enable
Zhihai Xu401202b2012-12-03 11:36:21 -08001377 handleEnable(mQuietEnable);
John Spurlock8a985d22014-02-25 09:40:05 -05001378 } else if (mBinding || mBluetooth != null) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001379 Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
1380 userMsg.arg2 = 1 + msg.arg2;
1381 // if user is switched when service is being binding
1382 // delay sending MESSAGE_USER_SWITCHED
1383 mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
1384 if (DBG) {
1385 Log.d(TAG, "delay MESSAGE_USER_SWITCHED " + userMsg.arg2);
1386 }
John Spurlock8a985d22014-02-25 09:40:05 -05001387 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001388 break;
1389 }
fredc0f420372012-04-12 00:02:00 -07001390 }
1391 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001392 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001393
Zhihai Xu401202b2012-12-03 11:36:21 -08001394 private void handleEnable(boolean quietMode) {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001395 mQuietEnable = quietMode;
1396
Matthew Xiecdce0b92012-07-12 19:06:15 -07001397 synchronized(mConnection) {
Zhihai Xu40874a02012-10-08 17:57:03 -07001398 if ((mBluetooth == null) && (!mBinding)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001399 //Start bind timeout and bind
1400 Message timeoutMsg=mHandler.obtainMessage(MESSAGE_TIMEOUT_BIND);
1401 mHandler.sendMessageDelayed(timeoutMsg,TIMEOUT_BIND_MS);
Matthew Xiecdce0b92012-07-12 19:06:15 -07001402 Intent i = new Intent(IBluetooth.class.getName());
Dianne Hackbornce09f5a2014-10-10 15:03:13 -07001403 if (!doBind(i, mConnection,Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
1404 UserHandle.CURRENT)) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001405 mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);
Zhihai Xu40874a02012-10-08 17:57:03 -07001406 } else {
1407 mBinding = true;
Matthew Xiecdce0b92012-07-12 19:06:15 -07001408 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001409 } else if (mBluetooth != null) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001410 //Enable bluetooth
1411 try {
Ganesh Ganapathi Battafffa86b2012-08-08 15:35:49 -07001412 if (!mQuietEnable) {
1413 if(!mBluetooth.enable()) {
1414 Log.e(TAG,"IBluetooth.enable() returned false");
1415 }
1416 }
1417 else {
1418 if(!mBluetooth.enableNoAutoConnect()) {
1419 Log.e(TAG,"IBluetooth.enableNoAutoConnect() returned false");
1420 }
Matthew Xiecdce0b92012-07-12 19:06:15 -07001421 }
1422 } catch (RemoteException e) {
1423 Log.e(TAG,"Unable to call enable()",e);
1424 }
1425 }
1426 }
1427 }
1428
Dianne Hackborn221ea892013-08-04 16:50:16 -07001429 boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
1430 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
1431 intent.setComponent(comp);
1432 if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
1433 Log.e(TAG, "Fail to bind to: " + intent);
1434 return false;
1435 }
1436 return true;
1437 }
1438
Zhihai Xu401202b2012-12-03 11:36:21 -08001439 private void handleDisable() {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001440 synchronized(mConnection) {
Andre Eisenbach305fdab2015-11-11 21:43:26 -08001441 if (mBluetooth != null) {
Matthew Xiecdce0b92012-07-12 19:06:15 -07001442 if (DBG) Log.d(TAG,"Sending off request.");
1443
1444 try {
1445 if(!mBluetooth.disable()) {
1446 Log.e(TAG,"IBluetooth.disable() returned false");
1447 }
1448 } catch (RemoteException e) {
1449 Log.e(TAG,"Unable to call disable()",e);
1450 }
1451 }
1452 }
1453 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001454
1455 private boolean checkIfCallerIsForegroundUser() {
1456 int foregroundUser;
1457 int callingUser = UserHandle.getCallingUserId();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001458 int callingUid = Binder.getCallingUid();
Zhihai Xu40874a02012-10-08 17:57:03 -07001459 long callingIdentity = Binder.clearCallingIdentity();
Benjamin Franze8b98922014-11-12 15:57:54 +00001460 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1461 UserInfo ui = um.getProfileParent(callingUser);
1462 int parentUser = (ui != null) ? ui.id : UserHandle.USER_NULL;
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001463 int callingAppId = UserHandle.getAppId(callingUid);
Zhihai Xu40874a02012-10-08 17:57:03 -07001464 boolean valid = false;
1465 try {
1466 foregroundUser = ActivityManager.getCurrentUser();
Martijn Coenen8385c5a2012-11-29 10:14:16 -08001467 valid = (callingUser == foregroundUser) ||
Benjamin Franze8b98922014-11-12 15:57:54 +00001468 parentUser == foregroundUser ||
Adrian Roosbd9a9a52014-08-18 15:31:57 +02001469 callingAppId == Process.NFC_UID ||
1470 callingAppId == mSystemUiUid;
Zhihai Xu40874a02012-10-08 17:57:03 -07001471 if (DBG) {
1472 Log.d(TAG, "checkIfCallerIsForegroundUser: valid=" + valid
1473 + " callingUser=" + callingUser
Benjamin Franze8b98922014-11-12 15:57:54 +00001474 + " parentUser=" + parentUser
Zhihai Xu40874a02012-10-08 17:57:03 -07001475 + " foregroundUser=" + foregroundUser);
1476 }
1477 } finally {
1478 Binder.restoreCallingIdentity(callingIdentity);
1479 }
1480 return valid;
1481 }
1482
Nitin Arorad055adb2015-03-02 15:03:51 -08001483 private void sendBleStateChanged(int prevState, int newState) {
1484 if (DBG) Log.d(TAG,"BLE State Change Intent: " + prevState + " -> " + newState);
1485 // Send broadcast message to everyone else
1486 Intent intent = new Intent(BluetoothAdapter.ACTION_BLE_STATE_CHANGED);
1487 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1488 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1489 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1490 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1491 }
1492
Zhihai Xu40874a02012-10-08 17:57:03 -07001493 private void bluetoothStateChangeHandler(int prevState, int newState) {
Nitin Arorad055adb2015-03-02 15:03:51 -08001494 boolean isStandardBroadcast = true;
Zhihai Xu40874a02012-10-08 17:57:03 -07001495 if (prevState != newState) {
1496 //Notify all proxy objects first of adapter state change
Nitin Arorad055adb2015-03-02 15:03:51 -08001497 if (newState == BluetoothAdapter.STATE_BLE_ON
1498 || newState == BluetoothAdapter.STATE_OFF) {
1499 boolean intermediate_off = (prevState == BluetoothAdapter.STATE_TURNING_OFF
1500 && newState == BluetoothAdapter.STATE_BLE_ON);
Zhihai Xu40874a02012-10-08 17:57:03 -07001501
Nitin Arorad055adb2015-03-02 15:03:51 -08001502 if (newState == BluetoothAdapter.STATE_OFF) {
1503 // If Bluetooth is off, send service down event to proxy objects, and unbind
1504 if (DBG) Log.d(TAG, "Bluetooth is complete turn off");
1505 if (canUnbindBluetoothService()) {
1506 if (DBG) Log.d(TAG, "Good to unbind!");
Matthew Xieddf7e472013-03-01 18:41:02 -08001507 sendBluetoothServiceDownCallback();
1508 unbindAndFinish();
Nitin Arorad055adb2015-03-02 15:03:51 -08001509 sendBleStateChanged(prevState, newState);
1510 // Don't broadcast as it has already been broadcast before
1511 isStandardBroadcast = false;
Matthew Xieddf7e472013-03-01 18:41:02 -08001512 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001513
1514 } else if (!intermediate_off) {
1515 // connect to GattService
1516 if (DBG) Log.d(TAG, "Bluetooth is in LE only mode");
1517 if (mBluetoothGatt != null) {
1518 if (DBG) Log.d(TAG, "Calling BluetoothGattServiceUp");
1519 onBluetoothGattServiceUp();
1520 } else {
1521 if (DBG) Log.d(TAG, "Binding Bluetooth GATT service");
1522 if (mContext.getPackageManager().hasSystemFeature(
1523 PackageManager.FEATURE_BLUETOOTH_LE)) {
1524 Intent i = new Intent(IBluetoothGatt.class.getName());
1525 doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.CURRENT);
1526 }
1527 }
1528 sendBleStateChanged(prevState, newState);
1529 //Don't broadcase this as std intent
1530 isStandardBroadcast = false;
1531
1532 } else if (intermediate_off){
1533 if (DBG) Log.d(TAG, "Intermediate off, back to LE only mode");
1534 // For LE only mode, broadcast as is
1535 sendBleStateChanged(prevState, newState);
1536 sendBluetoothStateCallback(false); // BT is OFF for general users
1537 // Broadcast as STATE_OFF
1538 newState = BluetoothAdapter.STATE_OFF;
1539 sendBrEdrDownCallback();
Zhihai Xu40874a02012-10-08 17:57:03 -07001540 }
Nitin Arorad055adb2015-03-02 15:03:51 -08001541 } else if (newState == BluetoothAdapter.STATE_ON) {
1542 boolean isUp = (newState==BluetoothAdapter.STATE_ON);
1543 sendBluetoothStateCallback(isUp);
1544 sendBleStateChanged(prevState, newState);
1545
1546 } else if (newState == BluetoothAdapter.STATE_BLE_TURNING_ON
1547 || newState == BluetoothAdapter.STATE_BLE_TURNING_OFF ) {
1548 sendBleStateChanged(prevState, newState);
1549 isStandardBroadcast = false;
1550
1551 } else if (newState == BluetoothAdapter.STATE_TURNING_ON
1552 || newState == BluetoothAdapter.STATE_TURNING_OFF) {
1553 sendBleStateChanged(prevState, newState);
Zhihai Xu40874a02012-10-08 17:57:03 -07001554 }
1555
Nitin Arorad055adb2015-03-02 15:03:51 -08001556 if (isStandardBroadcast) {
1557 if (prevState == BluetoothAdapter.STATE_BLE_ON) {
1558 // Show prevState of BLE_ON as OFF to standard users
1559 prevState = BluetoothAdapter.STATE_OFF;
1560 }
1561 Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
1562 intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, prevState);
1563 intent.putExtra(BluetoothAdapter.EXTRA_STATE, newState);
1564 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
1565 mContext.sendBroadcastAsUser(intent, UserHandle.ALL, BLUETOOTH_PERM);
1566 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001567 }
1568 }
1569
1570 /**
1571 * if on is true, wait for state become ON
1572 * if off is true, wait for state become OFF
1573 * if both on and off are false, wait for state not ON
1574 */
1575 private boolean waitForOnOff(boolean on, boolean off) {
1576 int i = 0;
1577 while (i < 10) {
1578 synchronized(mConnection) {
1579 try {
1580 if (mBluetooth == null) break;
1581 if (on) {
1582 if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
1583 } else if (off) {
1584 if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001585 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001586 if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001587 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001588 } catch (RemoteException e) {
1589 Log.e(TAG, "getState()", e);
1590 break;
1591 }
1592 }
1593 if (on || off) {
1594 SystemClock.sleep(300);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001595 } else {
Zhihai Xu40874a02012-10-08 17:57:03 -07001596 SystemClock.sleep(50);
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001597 }
Zhihai Xu40874a02012-10-08 17:57:03 -07001598 i++;
1599 }
1600 Log.e(TAG,"waitForOnOff time out");
1601 return false;
1602 }
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001603
Zhihai Xu401202b2012-12-03 11:36:21 -08001604 private void sendDisableMsg() {
1605 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
1606 }
1607
1608 private void sendEnableMsg(boolean quietMode) {
1609 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE,
1610 quietMode ? 1 : 0, 0));
1611 }
1612
Zhihai Xu681ae7f2012-11-12 15:14:18 -08001613 private boolean canUnbindBluetoothService() {
1614 synchronized(mConnection) {
1615 //Only unbind with mEnable flag not set
1616 //For race condition: disable and enable back-to-back
1617 //Avoid unbind right after enable due to callback from disable
1618 //Only unbind with Bluetooth at OFF state
1619 //Only unbind without any MESSAGE_BLUETOOTH_STATE_CHANGE message
1620 try {
1621 if (mEnable || (mBluetooth == null)) return false;
1622 if (mHandler.hasMessages(MESSAGE_BLUETOOTH_STATE_CHANGE)) return false;
1623 return (mBluetooth.getState() == BluetoothAdapter.STATE_OFF);
1624 } catch (RemoteException e) {
1625 Log.e(TAG, "getState()", e);
1626 }
1627 }
1628 return false;
1629 }
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001630
1631 private void recoverBluetoothServiceFromError() {
1632 Log.e(TAG,"recoverBluetoothServiceFromError");
1633 synchronized (mConnection) {
1634 if (mBluetooth != null) {
1635 //Unregister callback object
1636 try {
1637 mBluetooth.unregisterCallback(mBluetoothCallback);
1638 } catch (RemoteException re) {
1639 Log.e(TAG, "Unable to unregister",re);
1640 }
1641 }
1642 }
1643
1644 SystemClock.sleep(500);
1645
1646 // disable
1647 handleDisable();
1648
1649 waitForOnOff(false, true);
1650
1651 sendBluetoothServiceDownCallback();
1652 synchronized (mConnection) {
1653 if (mBluetooth != null) {
1654 mBluetooth = null;
1655 //Unbind
1656 mContext.unbindService(mConnection);
1657 }
Pavlin Radoslavov29b8c4a2015-08-03 17:44:47 -07001658 mBluetoothGatt = null;
Zhihai Xudd9d17d2013-01-08 17:05:58 -08001659 }
1660
1661 mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
1662 mState = BluetoothAdapter.STATE_OFF;
1663
1664 mEnable = false;
1665
1666 if (mErrorRecoveryRetryCounter++ < MAX_ERROR_RESTART_RETRIES) {
1667 // Send a Bluetooth Restart message to reenable bluetooth
1668 Message restartMsg = mHandler.obtainMessage(
1669 MESSAGE_RESTART_BLUETOOTH_SERVICE);
1670 mHandler.sendMessageDelayed(restartMsg, ERROR_RESTART_TIME_MS);
1671 } else {
1672 // todo: notify user to power down and power up phone to make bluetooth work.
1673 }
1674 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001675
1676 @Override
1677 public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Mike Lockwood75b52bb2014-12-18 14:16:36 -08001678 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1679
Andre Eisenbach14dcb5f2014-12-05 09:31:30 -08001680 writer.println("Bluetooth Status");
1681 writer.println(" enabled: " + mEnable);
1682 writer.println(" state: " + mState);
1683 writer.println(" address: " + mAddress);
1684 writer.println(" name: " + mName + "\n");
1685 writer.flush();
1686
Mike Lockwood726d4de2014-10-28 14:06:28 -07001687 if (mBluetooth == null) {
1688 writer.println("Bluetooth Service not connected");
1689 } else {
Andre Eisenbach14dcb5f2014-12-05 09:31:30 -08001690 ParcelFileDescriptor pfd = null;
Mike Lockwood726d4de2014-10-28 14:06:28 -07001691 try {
Sharvil Nanavati7c250052015-06-20 12:05:52 -07001692 writer.println("Bonded devices:");
1693 for (BluetoothDevice device : mBluetooth.getBondedDevices()) {
1694 writer.println(" " + device.getAddress() +
1695 " [" + DEVICE_TYPE_NAMES[device.getType()] + "] " +
1696 device.getName());
1697 }
1698 writer.flush();
1699
Andre Eisenbach14dcb5f2014-12-05 09:31:30 -08001700 pfd = ParcelFileDescriptor.dup(fd);
1701 mBluetooth.dump(pfd);
Mike Lockwood726d4de2014-10-28 14:06:28 -07001702 } catch (RemoteException re) {
1703 writer.println("RemoteException while calling Bluetooth Service");
Andre Eisenbach14dcb5f2014-12-05 09:31:30 -08001704 } catch (IOException ioe) {
1705 writer.println("IOException attempting to dup() fd");
1706 } finally {
1707 if (pfd != null) {
1708 try {
1709 pfd.close();
1710 } catch (IOException ioe) {
1711 writer.println("IOException attempting to close() fd");
1712 }
1713 }
Mike Lockwood726d4de2014-10-28 14:06:28 -07001714 }
1715 }
1716 }
fredc0f420372012-04-12 00:02:00 -07001717}