Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 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.BluetoothA2dp; |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 20 | import android.bluetooth.BluetoothA2dpSink; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 21 | import android.bluetooth.BluetoothDevice; |
| 22 | import android.bluetooth.BluetoothHeadset; |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 23 | import android.bluetooth.BluetoothHeadsetClient; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 24 | import android.bluetooth.BluetoothHearingAid; |
Hansong Zhang | dcae293 | 2017-11-13 10:56:23 -0800 | [diff] [blame^] | 25 | import android.bluetooth.BluetoothHidDevice; |
Hansong Zhang | 0edf754 | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 26 | import android.bluetooth.BluetoothHidHost; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 27 | import android.bluetooth.BluetoothMap; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 28 | import android.bluetooth.BluetoothMapClient; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 29 | import android.bluetooth.BluetoothPan; |
Hemant Gupta | dbc3d8d | 2017-05-12 21:14:44 +0530 | [diff] [blame] | 30 | import android.bluetooth.BluetoothPbap; |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 31 | import android.bluetooth.BluetoothPbapClient; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 32 | import android.bluetooth.BluetoothProfile; |
| 33 | import android.bluetooth.BluetoothUuid; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.os.ParcelUuid; |
| 37 | import android.util.Log; |
Andrew Sapperstein | 7aeb3f5 | 2017-04-19 20:03:42 -0700 | [diff] [blame] | 38 | import com.android.internal.R; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | import java.util.Collection; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.Map; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * LocalBluetoothProfileManager provides access to the LocalBluetoothProfile |
| 46 | * objects for the available Bluetooth profiles. |
| 47 | */ |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 48 | public class LocalBluetoothProfileManager { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 49 | private static final String TAG = "LocalBluetoothProfileManager"; |
| 50 | private static final boolean DEBUG = Utils.D; |
| 51 | /** Singleton instance. */ |
| 52 | private static LocalBluetoothProfileManager sInstance; |
| 53 | |
| 54 | /** |
| 55 | * An interface for notifying BluetoothHeadset IPC clients when they have |
| 56 | * been connected to the BluetoothHeadset service. |
| 57 | * Only used by com.android.settings.bluetooth.DockService. |
| 58 | */ |
| 59 | public interface ServiceListener { |
| 60 | /** |
| 61 | * Called to notify the client when this proxy object has been |
| 62 | * connected to the BluetoothHeadset service. Clients must wait for |
| 63 | * this callback before making IPC calls on the BluetoothHeadset |
| 64 | * service. |
| 65 | */ |
| 66 | void onServiceConnected(); |
| 67 | |
| 68 | /** |
| 69 | * Called to notify the client that this proxy object has been |
| 70 | * disconnected from the BluetoothHeadset service. Clients must not |
| 71 | * make IPC calls on the BluetoothHeadset service after this callback. |
| 72 | * This callback will currently only occur if the application hosting |
| 73 | * the BluetoothHeadset service, but may be called more often in future. |
| 74 | */ |
| 75 | void onServiceDisconnected(); |
| 76 | } |
| 77 | |
| 78 | private final Context mContext; |
| 79 | private final LocalBluetoothAdapter mLocalAdapter; |
| 80 | private final CachedBluetoothDeviceManager mDeviceManager; |
| 81 | private final BluetoothEventManager mEventManager; |
| 82 | |
| 83 | private A2dpProfile mA2dpProfile; |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 84 | private A2dpSinkProfile mA2dpSinkProfile; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 85 | private HeadsetProfile mHeadsetProfile; |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 86 | private HfpClientProfile mHfpClientProfile; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 87 | private MapProfile mMapProfile; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 88 | private MapClientProfile mMapClientProfile; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 89 | private final HidProfile mHidProfile; |
Hansong Zhang | dcae293 | 2017-11-13 10:56:23 -0800 | [diff] [blame^] | 90 | private HidDeviceProfile mHidDeviceProfile; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 91 | private OppProfile mOppProfile; |
| 92 | private final PanProfile mPanProfile; |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 93 | private PbapClientProfile mPbapClientProfile; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 94 | private final PbapServerProfile mPbapProfile; |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 95 | private final boolean mUsePbapPce; |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 96 | private final boolean mUseMapClient; |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 97 | private HearingAidProfile mHearingAidProfile; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Mapping from profile name, e.g. "HEADSET" to profile object. |
| 101 | */ |
| 102 | private final Map<String, LocalBluetoothProfile> |
| 103 | mProfileNameMap = new HashMap<String, LocalBluetoothProfile>(); |
| 104 | |
| 105 | LocalBluetoothProfileManager(Context context, |
| 106 | LocalBluetoothAdapter adapter, |
| 107 | CachedBluetoothDeviceManager deviceManager, |
| 108 | BluetoothEventManager eventManager) { |
| 109 | mContext = context; |
| 110 | |
| 111 | mLocalAdapter = adapter; |
| 112 | mDeviceManager = deviceManager; |
| 113 | mEventManager = eventManager; |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 114 | mUsePbapPce = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 115 | // MAP Client is typically used in the same situations as PBAP Client |
| 116 | mUseMapClient = mContext.getResources().getBoolean(R.bool.enable_pbap_pce_profile); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 117 | // pass this reference to adapter and event manager (circular dependency) |
| 118 | mLocalAdapter.setProfileManager(this); |
| 119 | mEventManager.setProfileManager(this); |
| 120 | |
| 121 | ParcelUuid[] uuids = adapter.getUuids(); |
| 122 | |
| 123 | // uuids may be null if Bluetooth is turned off |
| 124 | if (uuids != null) { |
| 125 | updateLocalProfiles(uuids); |
| 126 | } |
| 127 | |
Hansong Zhang | dcae293 | 2017-11-13 10:56:23 -0800 | [diff] [blame^] | 128 | // Always add HID host, HID device, and PAN profiles |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 129 | mHidProfile = new HidProfile(context, mLocalAdapter, mDeviceManager, this); |
| 130 | addProfile(mHidProfile, HidProfile.NAME, |
Hansong Zhang | 0edf754 | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 131 | BluetoothHidHost.ACTION_CONNECTION_STATE_CHANGED); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 132 | |
| 133 | mPanProfile = new PanProfile(context); |
| 134 | addPanProfile(mPanProfile, PanProfile.NAME, |
| 135 | BluetoothPan.ACTION_CONNECTION_STATE_CHANGED); |
| 136 | |
Hansong Zhang | dcae293 | 2017-11-13 10:56:23 -0800 | [diff] [blame^] | 137 | mHidDeviceProfile = new HidDeviceProfile(context, mLocalAdapter, mDeviceManager, this); |
| 138 | addProfile(mHidDeviceProfile, HidDeviceProfile.NAME, |
| 139 | BluetoothHidDevice.ACTION_CONNECTION_STATE_CHANGED); |
| 140 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 141 | if(DEBUG) Log.d(TAG, "Adding local MAP profile"); |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 142 | if (mUseMapClient) { |
| 143 | mMapClientProfile = new MapClientProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 144 | addProfile(mMapClientProfile, MapClientProfile.NAME, |
| 145 | BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED); |
| 146 | } else { |
| 147 | mMapProfile = new MapProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 148 | addProfile(mMapProfile, MapProfile.NAME, |
| 149 | BluetoothMap.ACTION_CONNECTION_STATE_CHANGED); |
| 150 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 151 | |
Hemant Gupta | dbc3d8d | 2017-05-12 21:14:44 +0530 | [diff] [blame] | 152 | //Create PBAP server profile |
| 153 | if(DEBUG) Log.d(TAG, "Adding local PBAP profile"); |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 154 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 155 | mPbapProfile = new PbapServerProfile(context); |
Hemant Gupta | dbc3d8d | 2017-05-12 21:14:44 +0530 | [diff] [blame] | 156 | addProfile(mPbapProfile, PbapServerProfile.NAME, |
| 157 | BluetoothPbap.ACTION_CONNECTION_STATE_CHANGED); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 158 | |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 159 | mHearingAidProfile = new HearingAidProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 160 | addProfile(mHearingAidProfile, HearingAidProfile.NAME, |
| 161 | BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 162 | if (DEBUG) Log.d(TAG, "LocalBluetoothProfileManager construction complete"); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Initialize or update the local profile objects. If a UUID was previously |
| 167 | * present but has been removed, we print a warning but don't remove the |
| 168 | * profile object as it might be referenced elsewhere, or the UUID might |
| 169 | * come back and we don't want multiple copies of the profile objects. |
| 170 | * @param uuids |
| 171 | */ |
| 172 | void updateLocalProfiles(ParcelUuid[] uuids) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 173 | // A2DP SRC |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 174 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSource)) { |
| 175 | if (mA2dpProfile == null) { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 176 | if(DEBUG) Log.d(TAG, "Adding local A2DP SRC profile"); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 177 | mA2dpProfile = new A2dpProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 178 | addProfile(mA2dpProfile, A2dpProfile.NAME, |
| 179 | BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); |
| 180 | } |
| 181 | } else if (mA2dpProfile != null) { |
| 182 | Log.w(TAG, "Warning: A2DP profile was previously added but the UUID is now missing."); |
| 183 | } |
| 184 | |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 185 | // A2DP SINK |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 186 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSink)) { |
| 187 | if (mA2dpSinkProfile == null) { |
| 188 | if(DEBUG) Log.d(TAG, "Adding local A2DP Sink profile"); |
| 189 | mA2dpSinkProfile = new A2dpSinkProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 190 | addProfile(mA2dpSinkProfile, A2dpSinkProfile.NAME, |
| 191 | BluetoothA2dpSink.ACTION_CONNECTION_STATE_CHANGED); |
| 192 | } |
| 193 | } else if (mA2dpSinkProfile != null) { |
| 194 | Log.w(TAG, "Warning: A2DP Sink profile was previously added but the UUID is now missing."); |
| 195 | } |
| 196 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 197 | // Headset / Handsfree |
| 198 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree_AG) || |
| 199 | BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP_AG)) { |
| 200 | if (mHeadsetProfile == null) { |
| 201 | if (DEBUG) Log.d(TAG, "Adding local HEADSET profile"); |
| 202 | mHeadsetProfile = new HeadsetProfile(mContext, mLocalAdapter, |
| 203 | mDeviceManager, this); |
| 204 | addProfile(mHeadsetProfile, HeadsetProfile.NAME, |
| 205 | BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED); |
| 206 | } |
| 207 | } else if (mHeadsetProfile != null) { |
| 208 | Log.w(TAG, "Warning: HEADSET profile was previously added but the UUID is now missing."); |
| 209 | } |
| 210 | |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 211 | // Headset HF |
| 212 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree)) { |
| 213 | if (mHfpClientProfile == null) { |
| 214 | if(DEBUG) Log.d(TAG, "Adding local HfpClient profile"); |
| 215 | mHfpClientProfile = |
| 216 | new HfpClientProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 217 | addProfile(mHfpClientProfile, HfpClientProfile.NAME, |
| 218 | BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED); |
| 219 | } |
| 220 | } else if (mHfpClientProfile != null) { |
| 221 | Log.w(TAG, |
| 222 | "Warning: Hfp Client profile was previously added but the UUID is now missing."); |
| 223 | } else { |
| 224 | Log.d(TAG, "Handsfree Uuid not found."); |
| 225 | } |
| 226 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 227 | // Message Access Profile Client |
| 228 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.MNS)) { |
| 229 | if (mMapClientProfile == null) { |
| 230 | if(DEBUG) Log.d(TAG, "Adding local Map Client profile"); |
| 231 | mMapClientProfile = |
| 232 | new MapClientProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 233 | addProfile(mMapClientProfile, MapClientProfile.NAME, |
| 234 | BluetoothMapClient.ACTION_CONNECTION_STATE_CHANGED); |
| 235 | } |
| 236 | } else if (mMapClientProfile != null) { |
| 237 | Log.w(TAG, |
| 238 | "Warning: MAP Client profile was previously added but the UUID is now missing."); |
| 239 | } else { |
| 240 | Log.d(TAG, "MAP Client Uuid not found."); |
| 241 | } |
| 242 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 243 | // OPP |
| 244 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.ObexObjectPush)) { |
| 245 | if (mOppProfile == null) { |
| 246 | if(DEBUG) Log.d(TAG, "Adding local OPP profile"); |
| 247 | mOppProfile = new OppProfile(); |
| 248 | // Note: no event handler for OPP, only name map. |
| 249 | mProfileNameMap.put(OppProfile.NAME, mOppProfile); |
| 250 | } |
| 251 | } else if (mOppProfile != null) { |
| 252 | Log.w(TAG, "Warning: OPP profile was previously added but the UUID is now missing."); |
| 253 | } |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 254 | |
| 255 | //PBAP Client |
| 256 | if (mUsePbapPce) { |
| 257 | if (mPbapClientProfile == null) { |
| 258 | if(DEBUG) Log.d(TAG, "Adding local PBAP Client profile"); |
| 259 | mPbapClientProfile = new PbapClientProfile(mContext, mLocalAdapter, mDeviceManager, |
| 260 | this); |
| 261 | addProfile(mPbapClientProfile, PbapClientProfile.NAME, |
| 262 | BluetoothPbapClient.ACTION_CONNECTION_STATE_CHANGED); |
| 263 | } |
| 264 | } else if (mPbapClientProfile != null) { |
| 265 | Log.w(TAG, |
| 266 | "Warning: PBAP Client profile was previously added but the UUID is now missing."); |
| 267 | } |
| 268 | |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 269 | //Hearing Aid Client |
| 270 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HearingAid)) { |
| 271 | if (mHearingAidProfile == null) { |
| 272 | if(DEBUG) Log.d(TAG, "Adding local Hearing Aid profile"); |
| 273 | mHearingAidProfile = new HearingAidProfile(mContext, mLocalAdapter, mDeviceManager, this); |
| 274 | addProfile(mHearingAidProfile, HearingAidProfile.NAME, |
| 275 | BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED); |
| 276 | } |
| 277 | } else if (mHearingAidProfile != null) { |
| 278 | Log.w(TAG, "Warning: Hearing Aid profile was previously added but the UUID is now missing."); |
| 279 | } |
| 280 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 281 | mEventManager.registerProfileIntentReceiver(); |
| 282 | |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 283 | // There is no local SDP record for HID and Settings app doesn't control PBAP Server. |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | private final Collection<ServiceListener> mServiceListeners = |
| 287 | new ArrayList<ServiceListener>(); |
| 288 | |
| 289 | private void addProfile(LocalBluetoothProfile profile, |
| 290 | String profileName, String stateChangedAction) { |
| 291 | mEventManager.addProfileHandler(stateChangedAction, new StateChangedHandler(profile)); |
| 292 | mProfileNameMap.put(profileName, profile); |
| 293 | } |
| 294 | |
| 295 | private void addPanProfile(LocalBluetoothProfile profile, |
| 296 | String profileName, String stateChangedAction) { |
| 297 | mEventManager.addProfileHandler(stateChangedAction, |
| 298 | new PanStateChangedHandler(profile)); |
| 299 | mProfileNameMap.put(profileName, profile); |
| 300 | } |
| 301 | |
| 302 | public LocalBluetoothProfile getProfileByName(String name) { |
| 303 | return mProfileNameMap.get(name); |
| 304 | } |
| 305 | |
| 306 | // Called from LocalBluetoothAdapter when state changes to ON |
| 307 | void setBluetoothStateOn() { |
| 308 | ParcelUuid[] uuids = mLocalAdapter.getUuids(); |
| 309 | if (uuids != null) { |
| 310 | updateLocalProfiles(uuids); |
| 311 | } |
| 312 | mEventManager.readPairedDevices(); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Generic handler for connection state change events for the specified profile. |
| 317 | */ |
| 318 | private class StateChangedHandler implements BluetoothEventManager.Handler { |
| 319 | final LocalBluetoothProfile mProfile; |
| 320 | |
| 321 | StateChangedHandler(LocalBluetoothProfile profile) { |
| 322 | mProfile = profile; |
| 323 | } |
| 324 | |
| 325 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
| 326 | CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device); |
| 327 | if (cachedDevice == null) { |
| 328 | Log.w(TAG, "StateChangedHandler found new device: " + device); |
| 329 | cachedDevice = mDeviceManager.addDevice(mLocalAdapter, |
| 330 | LocalBluetoothProfileManager.this, device); |
| 331 | } |
| 332 | int newState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0); |
| 333 | int oldState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, 0); |
| 334 | if (newState == BluetoothProfile.STATE_DISCONNECTED && |
| 335 | oldState == BluetoothProfile.STATE_CONNECTING) { |
| 336 | Log.i(TAG, "Failed to connect " + mProfile + " device"); |
| 337 | } |
| 338 | |
| 339 | cachedDevice.onProfileStateChanged(mProfile, newState); |
| 340 | cachedDevice.refresh(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** State change handler for NAP and PANU profiles. */ |
| 345 | private class PanStateChangedHandler extends StateChangedHandler { |
| 346 | |
| 347 | PanStateChangedHandler(LocalBluetoothProfile profile) { |
| 348 | super(profile); |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
| 353 | PanProfile panProfile = (PanProfile) mProfile; |
| 354 | int role = intent.getIntExtra(BluetoothPan.EXTRA_LOCAL_ROLE, 0); |
| 355 | panProfile.setLocalRole(device, role); |
| 356 | super.onReceive(context, intent, device); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // called from DockService |
| 361 | public void addServiceListener(ServiceListener l) { |
| 362 | mServiceListeners.add(l); |
| 363 | } |
| 364 | |
| 365 | // called from DockService |
| 366 | public void removeServiceListener(ServiceListener l) { |
| 367 | mServiceListeners.remove(l); |
| 368 | } |
| 369 | |
| 370 | // not synchronized: use only from UI thread! (TODO: verify) |
| 371 | void callServiceConnectedListeners() { |
| 372 | for (ServiceListener l : mServiceListeners) { |
| 373 | l.onServiceConnected(); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // not synchronized: use only from UI thread! (TODO: verify) |
| 378 | void callServiceDisconnectedListeners() { |
| 379 | for (ServiceListener listener : mServiceListeners) { |
| 380 | listener.onServiceDisconnected(); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // This is called by DockService, so check Headset and A2DP. |
| 385 | public synchronized boolean isManagerReady() { |
| 386 | // Getting just the headset profile is fine for now. Will need to deal with A2DP |
| 387 | // and others if they aren't always in a ready state. |
| 388 | LocalBluetoothProfile profile = mHeadsetProfile; |
| 389 | if (profile != null) { |
| 390 | return profile.isProfileReady(); |
| 391 | } |
| 392 | profile = mA2dpProfile; |
| 393 | if (profile != null) { |
| 394 | return profile.isProfileReady(); |
| 395 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 396 | profile = mA2dpSinkProfile; |
| 397 | if (profile != null) { |
| 398 | return profile.isProfileReady(); |
| 399 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 400 | return false; |
| 401 | } |
| 402 | |
| 403 | public A2dpProfile getA2dpProfile() { |
| 404 | return mA2dpProfile; |
| 405 | } |
| 406 | |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 407 | public A2dpSinkProfile getA2dpSinkProfile() { |
| 408 | if ((mA2dpSinkProfile != null) && (mA2dpSinkProfile.isProfileReady())) { |
| 409 | return mA2dpSinkProfile; |
| 410 | } else { |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 411 | return null; |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 412 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 415 | public HeadsetProfile getHeadsetProfile() { |
| 416 | return mHeadsetProfile; |
| 417 | } |
| 418 | |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 419 | public HfpClientProfile getHfpClientProfile() { |
| 420 | if ((mHfpClientProfile != null) && (mHfpClientProfile.isProfileReady())) { |
| 421 | return mHfpClientProfile; |
| 422 | } else { |
| 423 | return null; |
| 424 | } |
| 425 | } |
| 426 | |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 427 | public PbapClientProfile getPbapClientProfile() { |
| 428 | return mPbapClientProfile; |
| 429 | } |
| 430 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 431 | public PbapServerProfile getPbapProfile(){ |
| 432 | return mPbapProfile; |
| 433 | } |
| 434 | |
| 435 | public MapProfile getMapProfile(){ |
| 436 | return mMapProfile; |
| 437 | } |
| 438 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 439 | public MapClientProfile getMapClientProfile() { |
| 440 | return mMapClientProfile; |
| 441 | } |
| 442 | |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 443 | public HearingAidProfile getHearingAidProfile() { |
| 444 | return mHearingAidProfile; |
| 445 | } |
| 446 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 447 | /** |
| 448 | * Fill in a list of LocalBluetoothProfile objects that are supported by |
| 449 | * the local device and the remote device. |
| 450 | * |
| 451 | * @param uuids of the remote device |
| 452 | * @param localUuids UUIDs of the local device |
| 453 | * @param profiles The list of profiles to fill |
| 454 | * @param removedProfiles list of profiles that were removed |
| 455 | */ |
| 456 | synchronized void updateProfiles(ParcelUuid[] uuids, ParcelUuid[] localUuids, |
| 457 | Collection<LocalBluetoothProfile> profiles, |
| 458 | Collection<LocalBluetoothProfile> removedProfiles, |
| 459 | boolean isPanNapConnected, BluetoothDevice device) { |
| 460 | // Copy previous profile list into removedProfiles |
| 461 | removedProfiles.clear(); |
| 462 | removedProfiles.addAll(profiles); |
Joseph Pirozzo | 99fecc0 | 2016-04-07 13:43:49 -0700 | [diff] [blame] | 463 | if (DEBUG) { |
| 464 | Log.d(TAG,"Current Profiles" + profiles.toString()); |
| 465 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 466 | profiles.clear(); |
| 467 | |
| 468 | if (uuids == null) { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | if (mHeadsetProfile != null) { |
| 473 | if ((BluetoothUuid.isUuidPresent(localUuids, BluetoothUuid.HSP_AG) && |
| 474 | BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP)) || |
| 475 | (BluetoothUuid.isUuidPresent(localUuids, BluetoothUuid.Handsfree_AG) && |
| 476 | BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree))) { |
| 477 | profiles.add(mHeadsetProfile); |
| 478 | removedProfiles.remove(mHeadsetProfile); |
| 479 | } |
| 480 | } |
| 481 | |
Joseph Pirozzo | 99fecc0 | 2016-04-07 13:43:49 -0700 | [diff] [blame] | 482 | if ((mHfpClientProfile != null) && |
| 483 | BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree_AG) && |
| 484 | BluetoothUuid.isUuidPresent(localUuids, BluetoothUuid.Handsfree)) { |
| 485 | profiles.add(mHfpClientProfile); |
| 486 | removedProfiles.remove(mHfpClientProfile); |
| 487 | } |
| 488 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 489 | if (BluetoothUuid.containsAnyUuid(uuids, A2dpProfile.SINK_UUIDS) && |
| 490 | mA2dpProfile != null) { |
| 491 | profiles.add(mA2dpProfile); |
| 492 | removedProfiles.remove(mA2dpProfile); |
| 493 | } |
| 494 | |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 495 | if (BluetoothUuid.containsAnyUuid(uuids, A2dpSinkProfile.SRC_UUIDS) && |
| 496 | mA2dpSinkProfile != null) { |
| 497 | profiles.add(mA2dpSinkProfile); |
| 498 | removedProfiles.remove(mA2dpSinkProfile); |
Joseph Pirozzo | 99fecc0 | 2016-04-07 13:43:49 -0700 | [diff] [blame] | 499 | } |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 500 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 501 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.ObexObjectPush) && |
| 502 | mOppProfile != null) { |
| 503 | profiles.add(mOppProfile); |
| 504 | removedProfiles.remove(mOppProfile); |
| 505 | } |
| 506 | |
| 507 | if ((BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hid) || |
| 508 | BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hogp)) && |
| 509 | mHidProfile != null) { |
| 510 | profiles.add(mHidProfile); |
| 511 | removedProfiles.remove(mHidProfile); |
| 512 | } |
| 513 | |
Hansong Zhang | dcae293 | 2017-11-13 10:56:23 -0800 | [diff] [blame^] | 514 | if (mHidProfile != null && mHidDeviceProfile.getConnectionStatus(device) |
| 515 | != BluetoothProfile.STATE_DISCONNECTED) { |
| 516 | profiles.add(mHidDeviceProfile); |
| 517 | removedProfiles.remove(mHidDeviceProfile); |
| 518 | } |
| 519 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 520 | if(isPanNapConnected) |
| 521 | if(DEBUG) Log.d(TAG, "Valid PAN-NAP connection exists."); |
| 522 | if ((BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.NAP) && |
| 523 | mPanProfile != null) || isPanNapConnected) { |
| 524 | profiles.add(mPanProfile); |
| 525 | removedProfiles.remove(mPanProfile); |
| 526 | } |
| 527 | |
| 528 | if ((mMapProfile != null) && |
| 529 | (mMapProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED)) { |
| 530 | profiles.add(mMapProfile); |
| 531 | removedProfiles.remove(mMapProfile); |
| 532 | mMapProfile.setPreferred(device, true); |
| 533 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 534 | |
Hemant Gupta | dbc3d8d | 2017-05-12 21:14:44 +0530 | [diff] [blame] | 535 | if ((mPbapProfile != null) && |
| 536 | (mPbapProfile.getConnectionStatus(device) == BluetoothProfile.STATE_CONNECTED)) { |
| 537 | profiles.add(mPbapProfile); |
| 538 | removedProfiles.remove(mPbapProfile); |
| 539 | mPbapProfile.setPreferred(device, true); |
| 540 | } |
| 541 | |
Joseph Pirozzo | 631768d | 2016-09-01 14:19:28 -0700 | [diff] [blame] | 542 | if (mMapClientProfile != null) { |
| 543 | profiles.add(mMapClientProfile); |
| 544 | removedProfiles.remove(mMapClientProfile); |
| 545 | } |
| 546 | |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 547 | if (mUsePbapPce) { |
| 548 | profiles.add(mPbapClientProfile); |
| 549 | removedProfiles.remove(mPbapClientProfile); |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 550 | } |
Joseph Pirozzo | 99fecc0 | 2016-04-07 13:43:49 -0700 | [diff] [blame] | 551 | |
Jakub Pawlowski | ea580fa | 2017-11-22 11:02:34 -0800 | [diff] [blame] | 552 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HearingAid) && |
| 553 | mHearingAidProfile != null) { |
| 554 | profiles.add(mHearingAidProfile); |
| 555 | removedProfiles.remove(mHearingAidProfile); |
| 556 | } |
| 557 | |
Joseph Pirozzo | 99fecc0 | 2016-04-07 13:43:49 -0700 | [diff] [blame] | 558 | if (DEBUG) { |
| 559 | Log.d(TAG,"New Profiles" + profiles.toString()); |
| 560 | } |
Joseph Pirozzo | 563c700 | 2016-03-21 15:49:48 -0700 | [diff] [blame] | 561 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 562 | } |