AdapterService.java revision db0ae0560dbbfdfc6ffd1d62ab3008b30bed10f3
1/* 2 * 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. 15 */ 16 17/** 18 * @hide 19 */ 20 21package com.android.bluetooth.btservice; 22 23import android.app.AlarmManager; 24import android.app.Application; 25import android.app.PendingIntent; 26import android.app.Service; 27import android.bluetooth.BluetoothAdapter; 28import android.bluetooth.BluetoothDevice; 29import android.bluetooth.BluetoothProfile; 30import android.bluetooth.BluetoothUuid; 31import android.bluetooth.IBluetooth; 32import android.bluetooth.IBluetoothCallback; 33import android.bluetooth.IBluetoothManager; 34import android.bluetooth.IBluetoothManagerCallback; 35import android.bluetooth.BluetoothActivityEnergyInfo; 36import android.content.BroadcastReceiver; 37import android.content.ContentResolver; 38import android.content.Context; 39import android.content.Intent; 40import android.content.IntentFilter; 41import android.content.SharedPreferences; 42import android.os.Binder; 43import android.os.Bundle; 44import android.os.Handler; 45import android.os.IBinder; 46import android.os.Message; 47import android.os.ParcelFileDescriptor; 48import android.os.ParcelUuid; 49import android.os.PowerManager; 50import android.os.Process; 51import android.os.RemoteCallbackList; 52import android.os.RemoteException; 53import android.os.SystemClock; 54import android.provider.Settings; 55import android.util.EventLog; 56import android.util.Log; 57import android.util.Pair; 58 59import com.android.bluetooth.a2dp.A2dpService; 60import com.android.bluetooth.hid.HidService; 61import com.android.bluetooth.hfp.HeadsetService; 62import com.android.bluetooth.hdp.HealthService; 63import com.android.bluetooth.pan.PanService; 64import com.android.bluetooth.sdp.SdpManager; 65import com.android.internal.R; 66import com.android.bluetooth.Utils; 67import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties; 68 69import java.io.FileDescriptor; 70import java.io.FileWriter; 71import java.io.IOException; 72import java.util.ArrayList; 73import java.util.HashMap; 74import java.util.Set; 75import java.util.Map; 76import java.util.Iterator; 77import java.util.Map.Entry; 78import java.util.List; 79 80import android.content.pm.PackageManager; 81import android.os.ServiceManager; 82 83public class AdapterService extends Service { 84 private static final String TAG = "BluetoothAdapterService"; 85 private static final boolean DBG = false; 86 private static final boolean TRACE_REF = false; 87 private static final int MIN_ADVT_INSTANCES_FOR_MA = 5; 88 private static final int MIN_OFFLOADED_FILTERS = 10; 89 private static final int MIN_OFFLOADED_SCAN_STORAGE_BYTES = 1024; 90 //For Debugging only 91 private static int sRefCount=0; 92 93 private int mStackReportedState; 94 private int mTxTimeTotalMs; 95 private int mRxTimeTotalMs; 96 private int mIdleTimeTotalMs; 97 private int mEnergyUsedTotalVoltAmpSecMicro; 98 99 private final ArrayList<ProfileService> mProfiles = new ArrayList<ProfileService>(); 100 101 public static final String ACTION_LOAD_ADAPTER_PROPERTIES = 102 "com.android.bluetooth.btservice.action.LOAD_ADAPTER_PROPERTIES"; 103 public static final String ACTION_SERVICE_STATE_CHANGED = 104 "com.android.bluetooth.btservice.action.STATE_CHANGED"; 105 public static final String EXTRA_ACTION="action"; 106 public static final int PROFILE_CONN_CONNECTED = 1; 107 public static final int PROFILE_CONN_REJECTED = 2; 108 109 private static final String ACTION_ALARM_WAKEUP = 110 "com.android.bluetooth.btservice.action.ALARM_WAKEUP"; 111 112 public static final String BLUETOOTH_ADMIN_PERM = 113 android.Manifest.permission.BLUETOOTH_ADMIN; 114 public static final String BLUETOOTH_PRIVILEGED = 115 android.Manifest.permission.BLUETOOTH_PRIVILEGED; 116 static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; 117 static final String RECEIVE_MAP_PERM = android.Manifest.permission.RECEIVE_BLUETOOTH_MAP; 118 119 private static final String PHONEBOOK_ACCESS_PERMISSION_PREFERENCE_FILE = 120 "phonebook_access_permission"; 121 private static final String MESSAGE_ACCESS_PERMISSION_PREFERENCE_FILE = 122 "message_access_permission"; 123 private static final String SIM_ACCESS_PERMISSION_PREFERENCE_FILE = 124 "sim_access_permission"; 125 126 private static final int ADAPTER_SERVICE_TYPE=Service.START_STICKY; 127 128 static { 129 classInitNative(); 130 } 131 132 private static AdapterService sAdapterService; 133 public static synchronized AdapterService getAdapterService(){ 134 if (sAdapterService != null && !sAdapterService.mCleaningUp) { 135 Log.d(TAG, "getAdapterService() - returning " + sAdapterService); 136 return sAdapterService; 137 } 138 if (DBG) { 139 if (sAdapterService == null) { 140 Log.d(TAG, "getAdapterService() - Service not available"); 141 } else if (sAdapterService.mCleaningUp) { 142 Log.d(TAG,"getAdapterService() - Service is cleaning up"); 143 } 144 } 145 return null; 146 } 147 148 private static synchronized void setAdapterService(AdapterService instance) { 149 if (instance != null && !instance.mCleaningUp) { 150 if (DBG) Log.d(TAG, "setAdapterService() - set to: " + sAdapterService); 151 sAdapterService = instance; 152 } else { 153 if (DBG) { 154 if (sAdapterService == null) { 155 Log.d(TAG, "setAdapterService() - Service not available"); 156 } else if (sAdapterService.mCleaningUp) { 157 Log.d(TAG,"setAdapterService() - Service is cleaning up"); 158 } 159 } 160 } 161 } 162 163 private static synchronized void clearAdapterService() { 164 sAdapterService = null; 165 } 166 167 private AdapterProperties mAdapterProperties; 168 private AdapterState mAdapterStateMachine; 169 private BondStateMachine mBondStateMachine; 170 private JniCallbacks mJniCallbacks; 171 private RemoteDevices mRemoteDevices; 172 173 /* TODO: Consider to remove the search API from this class, if changed to use call-back */ 174 private SdpManager mSdpManager = null; 175 176 private boolean mProfilesStarted; 177 private boolean mNativeAvailable; 178 private boolean mCleaningUp; 179 private HashMap<String,Integer> mProfileServicesState = new HashMap<String,Integer>(); 180 //Only BluetoothManagerService should be registered 181 private RemoteCallbackList<IBluetoothCallback> mCallbacks; 182 private int mCurrentRequestId; 183 private boolean mQuietmode = false; 184 185 private AlarmManager mAlarmManager; 186 private PendingIntent mPendingAlarm; 187 private PowerManager mPowerManager; 188 private PowerManager.WakeLock mWakeLock; 189 private String mWakeLockName; 190 191 public AdapterService() { 192 super(); 193 if (TRACE_REF) { 194 synchronized (AdapterService.class) { 195 sRefCount++; 196 debugLog("AdapterService() - REFCOUNT: CREATED. INSTANCE_COUNT" + sRefCount); 197 } 198 } 199 } 200 201 public void onProfileConnectionStateChanged(BluetoothDevice device, int profileId, int newState, int prevState) { 202 Message m = mHandler.obtainMessage(MESSAGE_PROFILE_CONNECTION_STATE_CHANGED); 203 m.obj = device; 204 m.arg1 = profileId; 205 m.arg2 = newState; 206 Bundle b = new Bundle(1); 207 b.putInt("prevState", prevState); 208 m.setData(b); 209 mHandler.sendMessage(m); 210 } 211 212 public void initProfilePriorities(BluetoothDevice device, ParcelUuid[] mUuids) { 213 if(mUuids == null) return; 214 Message m = mHandler.obtainMessage(MESSAGE_PROFILE_INIT_PRIORITIES); 215 m.obj = device; 216 m.arg1 = mUuids.length; 217 Bundle b = new Bundle(1); 218 for(int i=0; i<mUuids.length; i++) { 219 b.putParcelable("uuids" + i, mUuids[i]); 220 } 221 m.setData(b); 222 mHandler.sendMessage(m); 223 } 224 225 private void processInitProfilePriorities (BluetoothDevice device, ParcelUuid[] uuids){ 226 HidService hidService = HidService.getHidService(); 227 A2dpService a2dpService = A2dpService.getA2dpService(); 228 HeadsetService headsetService = HeadsetService.getHeadsetService(); 229 230 // Set profile priorities only for the profiles discovered on the remote device. 231 // This avoids needless auto-connect attempts to profiles non-existent on the remote device 232 if ((hidService != null) && 233 (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hid) || 234 BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hogp)) && 235 (hidService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED)){ 236 hidService.setPriority(device,BluetoothProfile.PRIORITY_ON); 237 } 238 239 // If we do not have a stored priority for A2DP then default to on. 240 if ((a2dpService != null) && 241 (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AudioSink) || 242 BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.AdvAudioDist)) && 243 (a2dpService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED)){ 244 a2dpService.setPriority(device,BluetoothProfile.PRIORITY_ON); 245 } 246 247 if ((headsetService != null) && 248 ((BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP) || 249 BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree)) && 250 (headsetService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED))){ 251 headsetService.setPriority(device,BluetoothProfile.PRIORITY_ON); 252 } 253 } 254 255 private void processProfileStateChanged(BluetoothDevice device, int profileId, int newState, int prevState) { 256 if (((profileId == BluetoothProfile.A2DP) ||(profileId == BluetoothProfile.HEADSET)) && 257 (newState == BluetoothProfile.STATE_CONNECTED)){ 258 debugLog( "Profile connected. Schedule missing profile connection if any"); 259 connectOtherProfile(device, PROFILE_CONN_CONNECTED); 260 setProfileAutoConnectionPriority(device, profileId); 261 } 262 IBluetooth.Stub binder = mBinder; 263 if (binder != null) { 264 try { 265 binder.sendConnectionStateChange(device, profileId, newState,prevState); 266 } catch (RemoteException re) { 267 errorLog("" + re); 268 } 269 } 270 } 271 272 public void addProfile(ProfileService profile) { 273 synchronized (mProfiles) { 274 mProfiles.add(profile); 275 } 276 } 277 278 public void removeProfile(ProfileService profile) { 279 synchronized (mProfiles) { 280 mProfiles.remove(profile); 281 } 282 } 283 284 public void onProfileServiceStateChanged(String serviceName, int state) { 285 Message m = mHandler.obtainMessage(MESSAGE_PROFILE_SERVICE_STATE_CHANGED); 286 m.obj=serviceName; 287 m.arg1 = state; 288 mHandler.sendMessage(m); 289 } 290 291 private void processProfileServiceStateChanged(String serviceName, int state) { 292 boolean doUpdate=false; 293 boolean isBleTurningOn; 294 boolean isBleTurningOff; 295 boolean isTurningOn; 296 boolean isTurningOff; 297 298 synchronized (mProfileServicesState) { 299 Integer prevState = mProfileServicesState.get(serviceName); 300 if (prevState != null && prevState != state) { 301 mProfileServicesState.put(serviceName,state); 302 doUpdate=true; 303 } 304 } 305 debugLog("onProfileServiceStateChange() serviceName=" + serviceName 306 + ", state=" + state +", doUpdate=" + doUpdate); 307 308 if (!doUpdate) { 309 return; 310 } 311 312 synchronized (mAdapterStateMachine) { 313 isTurningOff = mAdapterStateMachine.isTurningOff(); 314 isTurningOn = mAdapterStateMachine.isTurningOn(); 315 isBleTurningOn = mAdapterStateMachine.isBleTurningOn(); 316 isBleTurningOff = mAdapterStateMachine.isBleTurningOff(); 317 } 318 319 debugLog("processProfileServiceStateChanged() - serviceName=" + serviceName + 320 " isTurningOn=" + isTurningOn + " isTurningOff=" + isTurningOff + 321 " isBleTurningOn=" + isBleTurningOn + " isBleTurningOff=" + isBleTurningOff); 322 323 if (isBleTurningOn) { 324 if (serviceName.equals("com.android.bluetooth.gatt.GattService")) { 325 debugLog("GattService is started"); 326 mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.BLE_STARTED)); 327 return; 328 } 329 330 } else if(isBleTurningOff) { 331 if (serviceName.equals("com.android.bluetooth.gatt.GattService")) { 332 debugLog("GattService stopped"); 333 mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.BLE_STOPPED)); 334 return; 335 } 336 337 } else if (isTurningOff) { 338 //On to BLE_ON 339 //Process stop or disable pending 340 //Check if all services are stopped if so, do cleanup 341 synchronized (mProfileServicesState) { 342 Iterator<Map.Entry<String,Integer>> i = mProfileServicesState.entrySet().iterator(); 343 while (i.hasNext()) { 344 Map.Entry<String,Integer> entry = i.next(); 345 debugLog("Service: " + entry.getKey()); 346 if (entry.getKey().equals("com.android.bluetooth.gatt.GattService")) { 347 debugLog("Skip GATT service - already started before"); 348 continue; 349 } 350 if (BluetoothAdapter.STATE_OFF != entry.getValue()) { 351 debugLog("onProfileServiceStateChange() - Profile still running: " 352 + entry.getKey()); 353 return; 354 } 355 } 356 } 357 debugLog("onProfileServiceStateChange() - All profile services stopped..."); 358 //Send message to state machine 359 mProfilesStarted=false; 360 mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.BREDR_STOPPED)); 361 362 } else if (isTurningOn) { 363 //Process start pending 364 //Check if all services are started if so, update state 365 synchronized (mProfileServicesState) { 366 Iterator<Map.Entry<String,Integer>> i = mProfileServicesState.entrySet().iterator(); 367 while (i.hasNext()) { 368 Map.Entry<String,Integer> entry = i.next(); 369 debugLog("Service: " + entry.getKey()); 370 if (entry.getKey().equals("com.android.bluetooth.gatt.GattService")) { 371 debugLog("Skip GATT service - already started before"); 372 continue; 373 } 374 if (BluetoothAdapter.STATE_ON != entry.getValue()) { 375 debugLog("onProfileServiceStateChange() - Profile still not running:" 376 + entry.getKey()); 377 return; 378 } 379 } 380 } 381 debugLog("onProfileServiceStateChange() - All profile services started."); 382 mProfilesStarted=true; 383 //Send message to state machine 384 mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.BREDR_STARTED)); 385 } 386 } 387 388 @Override 389 public void onCreate() { 390 super.onCreate(); 391 debugLog("onCreate()"); 392 mBinder = new AdapterServiceBinder(this); 393 mAdapterProperties = new AdapterProperties(this); 394 mAdapterStateMachine = AdapterState.make(this, mAdapterProperties); 395 mJniCallbacks = new JniCallbacks(mAdapterStateMachine, mAdapterProperties); 396 initNative(); 397 mNativeAvailable=true; 398 mCallbacks = new RemoteCallbackList<IBluetoothCallback>(); 399 //Load the name and address 400 getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR); 401 getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME); 402 mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 403 mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); 404 405 mSdpManager = SdpManager.init(this); 406 registerReceiver(mAlarmBroadcastReceiver, new IntentFilter(ACTION_ALARM_WAKEUP)); 407 408 409 410 } 411 412 @Override 413 public IBinder onBind(Intent intent) { 414 debugLog("onBind()"); 415 return mBinder; 416 } 417 public boolean onUnbind(Intent intent) { 418 debugLog("onUnbind() - calling cleanup"); 419 cleanup(); 420 return super.onUnbind(intent); 421 } 422 423 public void onDestroy() { 424 debugLog("onDestroy()"); 425 } 426 427 void BleOnProcessStart() { 428 debugLog("BleOnProcessStart()"); 429 Class[] supportedProfileServices = Config.getSupportedProfiles(); 430 //Initialize data objects 431 for (int i=0; i < supportedProfileServices.length;i++) { 432 mProfileServicesState.put(supportedProfileServices[i].getName(),BluetoothAdapter.STATE_OFF); 433 } 434 mRemoteDevices = new RemoteDevices(this); 435 mAdapterProperties.init(mRemoteDevices); 436 437 debugLog("BleOnProcessStart() - Make Bond State Machine"); 438 mBondStateMachine = BondStateMachine.make(this, mAdapterProperties, mRemoteDevices); 439 440 mJniCallbacks.init(mBondStateMachine,mRemoteDevices); 441 442 //FIXME: Set static instance here??? 443 setAdapterService(this); 444 445 //Start Gatt service 446 setGattProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_ON); 447 } 448 449 void startCoreServices() 450 { 451 debugLog("startCoreServices()"); 452 Class[] supportedProfileServices = Config.getSupportedProfiles(); 453 454 //Start profile services 455 if (!mProfilesStarted && supportedProfileServices.length >0) { 456 //Startup all profile services 457 setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_ON); 458 }else { 459 debugLog("startCoreProfiles(): Profile Services alreay started"); 460 mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.BREDR_STARTED)); 461 } 462 } 463 464 void startBluetoothDisable() { 465 mAdapterStateMachine.sendMessage(mAdapterStateMachine.obtainMessage(AdapterState.BEGIN_DISABLE)); 466 } 467 468 boolean stopProfileServices() { 469 Class[] supportedProfileServices = Config.getSupportedProfiles(); 470 if (mProfilesStarted && supportedProfileServices.length>0) { 471 setProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_OFF); 472 return true; 473 } 474 debugLog("stopProfileServices() - No profiles services to stop or already stopped."); 475 return false; 476 } 477 478 boolean stopGattProfileService() { 479 //TODO: can optimize this instead of looping around all supported profiles 480 debugLog("stopGattProfileService()"); 481 Class[] supportedProfileServices = Config.getSupportedProfiles(); 482 483 setGattProfileServiceState(supportedProfileServices,BluetoothAdapter.STATE_OFF); 484 return true; 485 } 486 487 488 void updateAdapterState(int prevState, int newState){ 489 if (mCallbacks !=null) { 490 int n=mCallbacks.beginBroadcast(); 491 debugLog("updateAdapterState() - Broadcasting state to " + n + " receivers."); 492 for (int i=0; i <n;i++) { 493 try { 494 mCallbacks.getBroadcastItem(i).onBluetoothStateChange(prevState,newState); 495 } catch (RemoteException e) { 496 debugLog("updateAdapterState() - Callback #" + i + " failed (" + e + ")"); 497 } 498 } 499 mCallbacks.finishBroadcast(); 500 } 501 } 502 503 void cleanup () { 504 debugLog("cleanup()"); 505 if (mCleaningUp) { 506 errorLog("cleanup() - Service already starting to cleanup, ignoring request..."); 507 return; 508 } 509 510 mCleaningUp = true; 511 512 unregisterReceiver(mAlarmBroadcastReceiver); 513 514 if (mPendingAlarm != null) { 515 mAlarmManager.cancel(mPendingAlarm); 516 mPendingAlarm = null; 517 } 518 519 // This wake lock release may also be called concurrently by 520 // {@link #releaseWakeLock(String lockName)}, so a synchronization is needed here. 521 synchronized (this) { 522 if (mWakeLock != null) { 523 mWakeLock.release(); 524 mWakeLock = null; 525 } 526 } 527 528 if (mAdapterStateMachine != null) { 529 mAdapterStateMachine.doQuit(); 530 mAdapterStateMachine.cleanup(); 531 } 532 533 if (mBondStateMachine != null) { 534 mBondStateMachine.doQuit(); 535 mBondStateMachine.cleanup(); 536 } 537 538 if (mRemoteDevices != null) { 539 mRemoteDevices.cleanup(); 540 } 541 542 if(mSdpManager != null) { 543 mSdpManager.cleanup(); 544 mSdpManager = null; 545 } 546 547 if (mNativeAvailable) { 548 debugLog("cleanup() - Cleaning up adapter native"); 549 cleanupNative(); 550 mNativeAvailable=false; 551 } 552 553 if (mAdapterProperties != null) { 554 mAdapterProperties.cleanup(); 555 } 556 557 if (mJniCallbacks != null) { 558 mJniCallbacks.cleanup(); 559 } 560 561 if (mProfileServicesState != null) { 562 mProfileServicesState.clear(); 563 } 564 565 clearAdapterService(); 566 567 if (mBinder != null) { 568 mBinder.cleanup(); 569 mBinder = null; //Do not remove. Otherwise Binder leak! 570 } 571 572 if (mCallbacks !=null) { 573 mCallbacks.kill(); 574 } 575 576 System.exit(0); 577 } 578 579 private static final int MESSAGE_PROFILE_SERVICE_STATE_CHANGED =1; 580 private static final int MESSAGE_PROFILE_CONNECTION_STATE_CHANGED=20; 581 private static final int MESSAGE_CONNECT_OTHER_PROFILES = 30; 582 private static final int MESSAGE_PROFILE_INIT_PRIORITIES=40; 583 private static final int CONNECT_OTHER_PROFILES_TIMEOUT= 6000; 584 585 private final Handler mHandler = new Handler() { 586 @Override 587 public void handleMessage(Message msg) { 588 debugLog("handleMessage() - Message: " + msg.what); 589 590 switch (msg.what) { 591 case MESSAGE_PROFILE_SERVICE_STATE_CHANGED: { 592 debugLog("handleMessage() - MESSAGE_PROFILE_SERVICE_STATE_CHANGED"); 593 processProfileServiceStateChanged((String) msg.obj, msg.arg1); 594 } 595 break; 596 case MESSAGE_PROFILE_CONNECTION_STATE_CHANGED: { 597 debugLog( "handleMessage() - MESSAGE_PROFILE_CONNECTION_STATE_CHANGED"); 598 processProfileStateChanged((BluetoothDevice) msg.obj, msg.arg1,msg.arg2, msg.getData().getInt("prevState",BluetoothAdapter.ERROR)); 599 } 600 break; 601 case MESSAGE_PROFILE_INIT_PRIORITIES: { 602 debugLog( "handleMessage() - MESSAGE_PROFILE_INIT_PRIORITIES"); 603 ParcelUuid[] mUuids = new ParcelUuid[msg.arg1]; 604 for(int i=0; i<mUuids.length; i++) { 605 mUuids[i] = msg.getData().getParcelable("uuids" + i); 606 } 607 processInitProfilePriorities((BluetoothDevice) msg.obj, 608 mUuids); 609 } 610 break; 611 case MESSAGE_CONNECT_OTHER_PROFILES: { 612 debugLog( "handleMessage() - MESSAGE_CONNECT_OTHER_PROFILES"); 613 processConnectOtherProfiles((BluetoothDevice) msg.obj,msg.arg1); 614 } 615 break; 616 } 617 } 618 }; 619 620 @SuppressWarnings("rawtypes") 621 private void setGattProfileServiceState(Class[] services, int state) { 622 if (state != BluetoothAdapter.STATE_ON && state != BluetoothAdapter.STATE_OFF) { 623 Log.w(TAG,"setGattProfileServiceState(): invalid state...Leaving..."); 624 return; 625 } 626 627 int expectedCurrentState= BluetoothAdapter.STATE_OFF; 628 int pendingState = BluetoothAdapter.STATE_TURNING_ON; 629 630 if (state == BluetoothAdapter.STATE_OFF) { 631 expectedCurrentState= BluetoothAdapter.STATE_ON; 632 pendingState = BluetoothAdapter.STATE_TURNING_OFF; 633 } 634 635 for (int i=0; i <services.length;i++) { 636 String serviceName = services[i].getName(); 637 String simpleName = services[i].getSimpleName(); 638 639 if (simpleName.equals("GattService")) { 640 Integer serviceState = mProfileServicesState.get(serviceName); 641 642 if(serviceState != null && serviceState != expectedCurrentState) { 643 debugLog("setProfileServiceState() - Unable to " 644 + (state == BluetoothAdapter.STATE_OFF ? "start" : "stop" ) 645 + " service " + serviceName 646 + ". Invalid state: " + serviceState); 647 continue; 648 } 649 debugLog("setProfileServiceState() - " 650 + (state == BluetoothAdapter.STATE_OFF ? "Stopping" : "Starting") 651 + " service " + serviceName); 652 653 mProfileServicesState.put(serviceName,pendingState); 654 Intent intent = new Intent(this,services[i]); 655 intent.putExtra(EXTRA_ACTION,ACTION_SERVICE_STATE_CHANGED); 656 intent.putExtra(BluetoothAdapter.EXTRA_STATE,state); 657 startService(intent); 658 return; 659 } 660 } 661 } 662 663 664 @SuppressWarnings("rawtypes") 665 private void setProfileServiceState(Class[] services, int state) { 666 if (state != BluetoothAdapter.STATE_ON && state != BluetoothAdapter.STATE_OFF) { 667 debugLog("setProfileServiceState() - Invalid state, leaving..."); 668 return; 669 } 670 671 int expectedCurrentState= BluetoothAdapter.STATE_OFF; 672 int pendingState = BluetoothAdapter.STATE_TURNING_ON; 673 if (state == BluetoothAdapter.STATE_OFF) { 674 expectedCurrentState= BluetoothAdapter.STATE_ON; 675 pendingState = BluetoothAdapter.STATE_TURNING_OFF; 676 } 677 678 for (int i=0; i <services.length;i++) { 679 String serviceName = services[i].getName(); 680 String simpleName = services[i].getSimpleName(); 681 682 if (simpleName.equals("GattService")) continue; 683 684 Integer serviceState = mProfileServicesState.get(serviceName); 685 if(serviceState != null && serviceState != expectedCurrentState) { 686 debugLog("setProfileServiceState() - Unable to " 687 + (state == BluetoothAdapter.STATE_OFF ? "start" : "stop" ) 688 + " service " + serviceName 689 + ". Invalid state: " + serviceState); 690 continue; 691 } 692 693 debugLog("setProfileServiceState() - " 694 + (state == BluetoothAdapter.STATE_OFF ? "Stopping" : "Starting") 695 + " service " + serviceName); 696 697 mProfileServicesState.put(serviceName,pendingState); 698 Intent intent = new Intent(this,services[i]); 699 intent.putExtra(EXTRA_ACTION,ACTION_SERVICE_STATE_CHANGED); 700 intent.putExtra(BluetoothAdapter.EXTRA_STATE,state); 701 startService(intent); 702 } 703 } 704 705 private boolean isAvailable() { 706 return !mCleaningUp; 707 } 708 709 /** 710 * Handlers for incoming service calls 711 */ 712 private AdapterServiceBinder mBinder; 713 714 /** 715 * The Binder implementation must be declared to be a static class, with 716 * the AdapterService instance passed in the constructor. Furthermore, 717 * when the AdapterService shuts down, the reference to the AdapterService 718 * must be explicitly removed. 719 * 720 * Otherwise, a memory leak can occur from repeated starting/stopping the 721 * service...Please refer to android.os.Binder for further details on 722 * why an inner instance class should be avoided. 723 * 724 */ 725 private static class AdapterServiceBinder extends IBluetooth.Stub { 726 private AdapterService mService; 727 728 public AdapterServiceBinder(AdapterService svc) { 729 mService = svc; 730 } 731 public boolean cleanup() { 732 mService = null; 733 return true; 734 } 735 736 public AdapterService getService() { 737 if (mService != null && mService.isAvailable()) { 738 return mService; 739 } 740 return null; 741 } 742 public boolean isEnabled() { 743 // don't check caller, may be called from system UI 744 AdapterService service = getService(); 745 if (service == null) return false; 746 return service.isEnabled(); 747 } 748 749 public int getState() { 750 // don't check caller, may be called from system UI 751 AdapterService service = getService(); 752 if (service == null) return BluetoothAdapter.STATE_OFF; 753 return service.getState(); 754 } 755 756 public boolean enable() { 757 if ((Binder.getCallingUid() != Process.SYSTEM_UID) && 758 (!Utils.checkCaller())) { 759 Log.w(TAG, "enable() - Not allowed for non-active user and non system user"); 760 return false; 761 } 762 AdapterService service = getService(); 763 if (service == null) return false; 764 return service.enable(); 765 } 766 767 public boolean enableNoAutoConnect() { 768 if ((Binder.getCallingUid() != Process.SYSTEM_UID) && 769 (!Utils.checkCaller())) { 770 Log.w(TAG, "enableNoAuto() - Not allowed for non-active user and non system user"); 771 return false; 772 } 773 774 AdapterService service = getService(); 775 if (service == null) return false; 776 return service.enableNoAutoConnect(); 777 } 778 779 public boolean disable() { 780 if ((Binder.getCallingUid() != Process.SYSTEM_UID) && 781 (!Utils.checkCaller())) { 782 Log.w(TAG, "disable() - Not allowed for non-active user and non system user"); 783 return false; 784 } 785 786 AdapterService service = getService(); 787 if (service == null) return false; 788 return service.disable(); 789 } 790 791 public String getAddress() { 792 if ((Binder.getCallingUid() != Process.SYSTEM_UID) && 793 (!Utils.checkCallerAllowManagedProfiles(mService))) { 794 Log.w(TAG, "getAddress() - Not allowed for non-active user and non system user"); 795 return null; 796 } 797 798 AdapterService service = getService(); 799 if (service == null) return null; 800 return service.getAddress(); 801 } 802 803 public ParcelUuid[] getUuids() { 804 if (!Utils.checkCaller()) { 805 Log.w(TAG, "getUuids() - Not allowed for non-active user"); 806 return new ParcelUuid[0]; 807 } 808 809 AdapterService service = getService(); 810 if (service == null) return new ParcelUuid[0]; 811 return service.getUuids(); 812 } 813 814 public String getName() { 815 if ((Binder.getCallingUid() != Process.SYSTEM_UID) && 816 (!Utils.checkCaller())) { 817 Log.w(TAG, "getName() - Not allowed for non-active user and non system user"); 818 return null; 819 } 820 821 AdapterService service = getService(); 822 if (service == null) return null; 823 return service.getName(); 824 } 825 826 public boolean setName(String name) { 827 if (!Utils.checkCaller()) { 828 Log.w(TAG, "setName() - Not allowed for non-active user"); 829 return false; 830 } 831 832 AdapterService service = getService(); 833 if (service == null) return false; 834 return service.setName(name); 835 } 836 837 public int getScanMode() { 838 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 839 Log.w(TAG, "getScanMode() - Not allowed for non-active user"); 840 return BluetoothAdapter.SCAN_MODE_NONE; 841 } 842 843 AdapterService service = getService(); 844 if (service == null) return BluetoothAdapter.SCAN_MODE_NONE; 845 return service.getScanMode(); 846 } 847 848 public boolean setScanMode(int mode, int duration) { 849 if (!Utils.checkCaller()) { 850 Log.w(TAG, "setScanMode() - Not allowed for non-active user"); 851 return false; 852 } 853 854 AdapterService service = getService(); 855 if (service == null) return false; 856 return service.setScanMode(mode,duration); 857 } 858 859 public int getDiscoverableTimeout() { 860 if (!Utils.checkCaller()) { 861 Log.w(TAG, "getDiscoverableTimeout() - Not allowed for non-active user"); 862 return 0; 863 } 864 865 AdapterService service = getService(); 866 if (service == null) return 0; 867 return service.getDiscoverableTimeout(); 868 } 869 870 public boolean setDiscoverableTimeout(int timeout) { 871 if (!Utils.checkCaller()) { 872 Log.w(TAG, "setDiscoverableTimeout() - Not allowed for non-active user"); 873 return false; 874 } 875 876 AdapterService service = getService(); 877 if (service == null) return false; 878 return service.setDiscoverableTimeout(timeout); 879 } 880 881 public boolean startDiscovery() { 882 if (!Utils.checkCaller()) { 883 Log.w(TAG, "startDiscovery() - Not allowed for non-active user"); 884 return false; 885 } 886 887 AdapterService service = getService(); 888 if (service == null) return false; 889 return service.startDiscovery(); 890 } 891 892 public boolean cancelDiscovery() { 893 if (!Utils.checkCaller()) { 894 Log.w(TAG, "cancelDiscovery() - Not allowed for non-active user"); 895 return false; 896 } 897 898 AdapterService service = getService(); 899 if (service == null) return false; 900 return service.cancelDiscovery(); 901 } 902 public boolean isDiscovering() { 903 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 904 Log.w(TAG, "isDiscovering() - Not allowed for non-active user"); 905 return false; 906 } 907 908 AdapterService service = getService(); 909 if (service == null) return false; 910 return service.isDiscovering(); 911 } 912 913 public BluetoothDevice[] getBondedDevices() { 914 // don't check caller, may be called from system UI 915 AdapterService service = getService(); 916 if (service == null) return new BluetoothDevice[0]; 917 return service.getBondedDevices(); 918 } 919 920 public int getAdapterConnectionState() { 921 // don't check caller, may be called from system UI 922 AdapterService service = getService(); 923 if (service == null) return BluetoothAdapter.STATE_DISCONNECTED; 924 return service.getAdapterConnectionState(); 925 } 926 927 public int getProfileConnectionState(int profile) { 928 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 929 Log.w(TAG, "getProfileConnectionState- Not allowed for non-active user"); 930 return BluetoothProfile.STATE_DISCONNECTED; 931 } 932 933 AdapterService service = getService(); 934 if (service == null) return BluetoothProfile.STATE_DISCONNECTED; 935 return service.getProfileConnectionState(profile); 936 } 937 938 public boolean createBond(BluetoothDevice device, int transport) { 939 if (!Utils.checkCaller()) { 940 Log.w(TAG, "createBond() - Not allowed for non-active user"); 941 return false; 942 } 943 944 AdapterService service = getService(); 945 if (service == null) return false; 946 return service.createBond(device, transport); 947 } 948 949 public boolean cancelBondProcess(BluetoothDevice device) { 950 if (!Utils.checkCaller()) { 951 Log.w(TAG, "cancelBondProcess() - Not allowed for non-active user"); 952 return false; 953 } 954 955 AdapterService service = getService(); 956 if (service == null) return false; 957 return service.cancelBondProcess(device); 958 } 959 960 public boolean removeBond(BluetoothDevice device) { 961 if (!Utils.checkCaller()) { 962 Log.w(TAG, "removeBond() - Not allowed for non-active user"); 963 return false; 964 } 965 966 AdapterService service = getService(); 967 if (service == null) return false; 968 return service.removeBond(device); 969 } 970 971 public int getBondState(BluetoothDevice device) { 972 // don't check caller, may be called from system UI 973 AdapterService service = getService(); 974 if (service == null) return BluetoothDevice.BOND_NONE; 975 return service.getBondState(device); 976 } 977 978 public int getConnectionState(BluetoothDevice device) { 979 AdapterService service = getService(); 980 if (service == null) return 0; 981 return service.getConnectionState(device); 982 } 983 984 public String getRemoteName(BluetoothDevice device) { 985 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 986 Log.w(TAG, "getRemoteName() - Not allowed for non-active user"); 987 return null; 988 } 989 990 AdapterService service = getService(); 991 if (service == null) return null; 992 return service.getRemoteName(device); 993 } 994 995 public int getRemoteType(BluetoothDevice device) { 996 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 997 Log.w(TAG, "getRemoteType() - Not allowed for non-active user"); 998 return BluetoothDevice.DEVICE_TYPE_UNKNOWN; 999 } 1000 1001 AdapterService service = getService(); 1002 if (service == null) return BluetoothDevice.DEVICE_TYPE_UNKNOWN; 1003 return service.getRemoteType(device); 1004 } 1005 1006 public String getRemoteAlias(BluetoothDevice device) { 1007 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 1008 Log.w(TAG, "getRemoteAlias() - Not allowed for non-active user"); 1009 return null; 1010 } 1011 1012 AdapterService service = getService(); 1013 if (service == null) return null; 1014 return service.getRemoteAlias(device); 1015 } 1016 1017 public boolean setRemoteAlias(BluetoothDevice device, String name) { 1018 if (!Utils.checkCaller()) { 1019 Log.w(TAG, "setRemoteAlias() - Not allowed for non-active user"); 1020 return false; 1021 } 1022 1023 AdapterService service = getService(); 1024 if (service == null) return false; 1025 return service.setRemoteAlias(device, name); 1026 } 1027 1028 public int getRemoteClass(BluetoothDevice device) { 1029 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 1030 Log.w(TAG, "getRemoteClass() - Not allowed for non-active user"); 1031 return 0; 1032 } 1033 1034 AdapterService service = getService(); 1035 if (service == null) return 0; 1036 return service.getRemoteClass(device); 1037 } 1038 1039 public ParcelUuid[] getRemoteUuids(BluetoothDevice device) { 1040 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 1041 Log.w(TAG, "getRemoteUuids() - Not allowed for non-active user"); 1042 return new ParcelUuid[0]; 1043 } 1044 1045 AdapterService service = getService(); 1046 if (service == null) return new ParcelUuid[0]; 1047 return service.getRemoteUuids(device); 1048 } 1049 1050 public boolean fetchRemoteUuids(BluetoothDevice device) { 1051 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 1052 Log.w(TAG, "fetchRemoteUuids() - Not allowed for non-active user"); 1053 return false; 1054 } 1055 1056 AdapterService service = getService(); 1057 if (service == null) return false; 1058 return service.fetchRemoteUuids(device); 1059 } 1060 1061 1062 1063 public boolean setPin(BluetoothDevice device, boolean accept, int len, byte[] pinCode) { 1064 if (!Utils.checkCaller()) { 1065 Log.w(TAG, "setPin() - Not allowed for non-active user"); 1066 return false; 1067 } 1068 1069 AdapterService service = getService(); 1070 if (service == null) return false; 1071 return service.setPin(device, accept, len, pinCode); 1072 } 1073 1074 public boolean setPasskey(BluetoothDevice device, boolean accept, int len, byte[] passkey) { 1075 if (!Utils.checkCaller()) { 1076 Log.w(TAG, "setPasskey() - Not allowed for non-active user"); 1077 return false; 1078 } 1079 1080 AdapterService service = getService(); 1081 if (service == null) return false; 1082 return service.setPasskey(device, accept, len, passkey); 1083 } 1084 1085 public boolean setPairingConfirmation(BluetoothDevice device, boolean accept) { 1086 if (!Utils.checkCaller()) { 1087 Log.w(TAG, "setPairingConfirmation() - Not allowed for non-active user"); 1088 return false; 1089 } 1090 1091 AdapterService service = getService(); 1092 if (service == null) return false; 1093 return service.setPairingConfirmation(device, accept); 1094 } 1095 1096 public int getPhonebookAccessPermission(BluetoothDevice device) { 1097 if (!Utils.checkCaller()) { 1098 Log.w(TAG, "getPhonebookAccessPermission() - Not allowed for non-active user"); 1099 return BluetoothDevice.ACCESS_UNKNOWN; 1100 } 1101 1102 AdapterService service = getService(); 1103 if (service == null) return BluetoothDevice.ACCESS_UNKNOWN; 1104 return service.getPhonebookAccessPermission(device); 1105 } 1106 1107 public boolean setPhonebookAccessPermission(BluetoothDevice device, int value) { 1108 if (!Utils.checkCaller()) { 1109 Log.w(TAG, "setPhonebookAccessPermission() - Not allowed for non-active user"); 1110 return false; 1111 } 1112 1113 AdapterService service = getService(); 1114 if (service == null) return false; 1115 return service.setPhonebookAccessPermission(device, value); 1116 } 1117 1118 public int getMessageAccessPermission(BluetoothDevice device) { 1119 if (!Utils.checkCaller()) { 1120 Log.w(TAG, "getMessageAccessPermission() - Not allowed for non-active user"); 1121 return BluetoothDevice.ACCESS_UNKNOWN; 1122 } 1123 1124 AdapterService service = getService(); 1125 if (service == null) return BluetoothDevice.ACCESS_UNKNOWN; 1126 return service.getMessageAccessPermission(device); 1127 } 1128 1129 public boolean setMessageAccessPermission(BluetoothDevice device, int value) { 1130 if (!Utils.checkCaller()) { 1131 Log.w(TAG, "setMessageAccessPermission() - Not allowed for non-active user"); 1132 return false; 1133 } 1134 1135 AdapterService service = getService(); 1136 if (service == null) return false; 1137 return service.setMessageAccessPermission(device, value); 1138 } 1139 1140 public int getSimAccessPermission(BluetoothDevice device) { 1141 if (!Utils.checkCaller()) { 1142 Log.w(TAG, "getSimAccessPermission() - Not allowed for non-active user"); 1143 return BluetoothDevice.ACCESS_UNKNOWN; 1144 } 1145 1146 AdapterService service = getService(); 1147 if (service == null) return BluetoothDevice.ACCESS_UNKNOWN; 1148 return service.getSimAccessPermission(device); 1149 } 1150 1151 public boolean setSimAccessPermission(BluetoothDevice device, int value) { 1152 if (!Utils.checkCaller()) { 1153 Log.w(TAG, "setSimAccessPermission() - Not allowed for non-active user"); 1154 return false; 1155 } 1156 1157 AdapterService service = getService(); 1158 if (service == null) return false; 1159 return service.setSimAccessPermission(device, value); 1160 } 1161 1162 public void sendConnectionStateChange(BluetoothDevice 1163 device, int profile, int state, int prevState) { 1164 AdapterService service = getService(); 1165 if (service == null) return; 1166 service.sendConnectionStateChange(device, profile, state, prevState); 1167 } 1168 1169 public ParcelFileDescriptor connectSocket(BluetoothDevice device, int type, 1170 ParcelUuid uuid, int port, int flag) { 1171 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 1172 Log.w(TAG, "connectSocket() - Not allowed for non-active user"); 1173 return null; 1174 } 1175 1176 AdapterService service = getService(); 1177 if (service == null) return null; 1178 return service.connectSocket(device, type, uuid, port, flag); 1179 } 1180 1181 public ParcelFileDescriptor createSocketChannel(int type, String serviceName, 1182 ParcelUuid uuid, int port, int flag) { 1183 if (!Utils.checkCallerAllowManagedProfiles(mService)) { 1184 Log.w(TAG, "createSocketChannel() - Not allowed for non-active user"); 1185 return null; 1186 } 1187 1188 AdapterService service = getService(); 1189 if (service == null) return null; 1190 return service.createSocketChannel(type, serviceName, uuid, port, flag); 1191 } 1192 public boolean sdpSearch(BluetoothDevice device, ParcelUuid uuid) { 1193 if (!Utils.checkCaller()) { 1194 Log.w(TAG,"sdpSea(): not allowed for non-active user"); 1195 return false; 1196 } 1197 1198 AdapterService service = getService(); 1199 if (service == null) return false; 1200 return service.sdpSearch(device,uuid); 1201 } 1202 1203 public boolean configHciSnoopLog(boolean enable) { 1204 if (Binder.getCallingUid() != Process.SYSTEM_UID) { 1205 EventLog.writeEvent(0x534e4554 /* SNET */, "Bluetooth", Binder.getCallingUid(), 1206 "configHciSnoopLog() - Not allowed for non-active user b/18643224"); 1207 return false; 1208 } 1209 1210 AdapterService service = getService(); 1211 if (service == null) return false; 1212 return service.configHciSnoopLog(enable); 1213 } 1214 1215 public boolean factoryReset() { 1216 AdapterService service = getService(); 1217 if (service == null) return false; 1218 service.disable(); 1219 return service.factoryReset(); 1220 1221 } 1222 1223 public void registerCallback(IBluetoothCallback cb) { 1224 AdapterService service = getService(); 1225 if (service == null) return ; 1226 service.registerCallback(cb); 1227 } 1228 1229 public void unregisterCallback(IBluetoothCallback cb) { 1230 AdapterService service = getService(); 1231 if (service == null) return ; 1232 service.unregisterCallback(cb); 1233 } 1234 1235 public boolean isMultiAdvertisementSupported() { 1236 AdapterService service = getService(); 1237 if (service == null) return false; 1238 return service.isMultiAdvertisementSupported(); 1239 } 1240 1241 public boolean isPeripheralModeSupported() { 1242 AdapterService service = getService(); 1243 if (service == null) return false; 1244 return service.isPeripheralModeSupported(); 1245 } 1246 1247 public boolean isOffloadedFilteringSupported() { 1248 AdapterService service = getService(); 1249 if (service == null) return false; 1250 int val = service.getNumOfOffloadedScanFilterSupported(); 1251 return (val >= MIN_OFFLOADED_FILTERS); 1252 } 1253 1254 public boolean isOffloadedScanBatchingSupported() { 1255 AdapterService service = getService(); 1256 if (service == null) return false; 1257 int val = service.getOffloadedScanResultStorage(); 1258 return (val >= MIN_OFFLOADED_SCAN_STORAGE_BYTES); 1259 } 1260 1261 public boolean isActivityAndEnergyReportingSupported() { 1262 AdapterService service = getService(); 1263 if (service == null) return false; 1264 return service.isActivityAndEnergyReportingSupported(); 1265 } 1266 1267 public void getActivityEnergyInfoFromController() { 1268 AdapterService service = getService(); 1269 if (service == null) return; 1270 service.getActivityEnergyInfoFromController(); 1271 } 1272 1273 public BluetoothActivityEnergyInfo reportActivityInfo() { 1274 AdapterService service = getService(); 1275 if (service == null) return null; 1276 return service.reportActivityInfo(); 1277 } 1278 1279 public void dump(ParcelFileDescriptor fd) { 1280 AdapterService service = getService(); 1281 if (service == null) return; 1282 service.dump(fd.getFileDescriptor()); 1283 } 1284 1285 public void onLeServiceUp(){ 1286 AdapterService service = getService(); 1287 if (service == null) return; 1288 service.onLeServiceUp(); 1289 } 1290 1291 public void onBrEdrDown(){ 1292 AdapterService service = getService(); 1293 if (service == null) return; 1294 service.onBrEdrDown(); 1295 } 1296 }; 1297 1298 // ----API Methods-------- 1299 1300 boolean isEnabled() { 1301 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1302 return mAdapterProperties.getState() == BluetoothAdapter.STATE_ON; 1303 } 1304 1305 int getState() { 1306 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1307 if (mAdapterProperties != null) return mAdapterProperties.getState(); 1308 return BluetoothAdapter.STATE_OFF; 1309 } 1310 1311 boolean enable() { 1312 return enable (false); 1313 } 1314 1315 public boolean enableNoAutoConnect() { 1316 return enable (true); 1317 } 1318 1319 public synchronized boolean enable(boolean quietMode) { 1320 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission"); 1321 1322 debugLog("enable() - Enable called with quiet mode status = " + mQuietmode); 1323 mQuietmode = quietMode; 1324 Message m = mAdapterStateMachine.obtainMessage(AdapterState.BLE_TURN_ON); 1325 mAdapterStateMachine.sendMessage(m); 1326 return true; 1327 } 1328 1329 boolean disable() { 1330 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission"); 1331 1332 debugLog("disable() called..."); 1333 Message m = mAdapterStateMachine.obtainMessage(AdapterState.BLE_TURN_OFF); 1334 mAdapterStateMachine.sendMessage(m); 1335 return true; 1336 } 1337 1338 String getAddress() { 1339 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1340 1341 String addrString = null; 1342 byte[] address = mAdapterProperties.getAddress(); 1343 return Utils.getAddressStringFromByte(address); 1344 } 1345 1346 ParcelUuid[] getUuids() { 1347 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1348 1349 return mAdapterProperties.getUuids(); 1350 } 1351 1352 String getName() { 1353 enforceCallingOrSelfPermission(BLUETOOTH_PERM, 1354 "Need BLUETOOTH permission"); 1355 1356 try { 1357 return mAdapterProperties.getName(); 1358 } catch (Throwable t) { 1359 debugLog("getName() - Unexpected exception (" + t + ")"); 1360 } 1361 return null; 1362 } 1363 1364 boolean setName(String name) { 1365 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, 1366 "Need BLUETOOTH ADMIN permission"); 1367 1368 return mAdapterProperties.setName(name); 1369 } 1370 1371 int getScanMode() { 1372 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1373 1374 return mAdapterProperties.getScanMode(); 1375 } 1376 1377 boolean setScanMode(int mode, int duration) { 1378 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1379 1380 setDiscoverableTimeout(duration); 1381 1382 int newMode = convertScanModeToHal(mode); 1383 return mAdapterProperties.setScanMode(newMode); 1384 } 1385 1386 int getDiscoverableTimeout() { 1387 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1388 1389 return mAdapterProperties.getDiscoverableTimeout(); 1390 } 1391 1392 boolean setDiscoverableTimeout(int timeout) { 1393 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1394 1395 return mAdapterProperties.setDiscoverableTimeout(timeout); 1396 } 1397 1398 boolean startDiscovery() { 1399 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, 1400 "Need BLUETOOTH ADMIN permission"); 1401 1402 return startDiscoveryNative(); 1403 } 1404 1405 boolean cancelDiscovery() { 1406 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, 1407 "Need BLUETOOTH ADMIN permission"); 1408 1409 return cancelDiscoveryNative(); 1410 } 1411 1412 boolean isDiscovering() { 1413 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1414 1415 return mAdapterProperties.isDiscovering(); 1416 } 1417 1418 BluetoothDevice[] getBondedDevices() { 1419 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1420 return mAdapterProperties.getBondedDevices(); 1421 } 1422 1423 int getAdapterConnectionState() { 1424 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1425 return mAdapterProperties.getConnectionState(); 1426 } 1427 1428 int getProfileConnectionState(int profile) { 1429 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1430 1431 return mAdapterProperties.getProfileConnectionState(profile); 1432 } 1433 boolean sdpSearch(BluetoothDevice device,ParcelUuid uuid) { 1434 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1435 if(mSdpManager != null) { 1436 mSdpManager.sdpSearch(device,uuid); 1437 return true; 1438 } else { 1439 return false; 1440 } 1441 } 1442 1443 1444 boolean createBond(BluetoothDevice device, int transport) { 1445 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, 1446 "Need BLUETOOTH ADMIN permission"); 1447 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1448 if (deviceProp != null && deviceProp.getBondState() != BluetoothDevice.BOND_NONE) { 1449 return false; 1450 } 1451 1452 // Pairing is unreliable while scanning, so cancel discovery 1453 // Note, remove this when native stack improves 1454 cancelDiscoveryNative(); 1455 1456 Message msg = mBondStateMachine.obtainMessage(BondStateMachine.CREATE_BOND); 1457 msg.obj = device; 1458 msg.arg1 = transport; 1459 mBondStateMachine.sendMessage(msg); 1460 return true; 1461 } 1462 1463 public boolean isQuietModeEnabled() { 1464 debugLog("isQuetModeEnabled() - Enabled = " + mQuietmode); 1465 return mQuietmode; 1466 } 1467 1468 public void autoConnect(){ 1469 if (getState() != BluetoothAdapter.STATE_ON){ 1470 errorLog("autoConnect() - BT is not ON. Exiting autoConnect"); 1471 return; 1472 } 1473 if (isQuietModeEnabled() == false) { 1474 debugLog( "autoConnect() - Initiate auto connection on BT on..."); 1475 autoConnectHeadset(); 1476 autoConnectA2dp(); 1477 } 1478 else { 1479 debugLog( "autoConnect() - BT is in quiet mode. Not initiating auto connections"); 1480 } 1481 } 1482 1483 private void autoConnectHeadset(){ 1484 HeadsetService hsService = HeadsetService.getHeadsetService(); 1485 1486 BluetoothDevice bondedDevices[] = getBondedDevices(); 1487 if ((bondedDevices == null) ||(hsService == null)) { 1488 return; 1489 } 1490 for (BluetoothDevice device : bondedDevices) { 1491 if (hsService.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT ){ 1492 debugLog("autoConnectHeadset() - Connecting HFP with " + device.toString()); 1493 hsService.connect(device); 1494 } 1495 } 1496 } 1497 1498 private void autoConnectA2dp(){ 1499 A2dpService a2dpSservice = A2dpService.getA2dpService(); 1500 BluetoothDevice bondedDevices[] = getBondedDevices(); 1501 if ((bondedDevices == null) ||(a2dpSservice == null)) { 1502 return; 1503 } 1504 for (BluetoothDevice device : bondedDevices) { 1505 if (a2dpSservice.getPriority(device) == BluetoothProfile.PRIORITY_AUTO_CONNECT ){ 1506 debugLog("autoConnectA2dp() - Connecting A2DP with " + device.toString()); 1507 a2dpSservice.connect(device); 1508 } 1509 } 1510 } 1511 1512 public void connectOtherProfile(BluetoothDevice device, int firstProfileStatus){ 1513 if ((mHandler.hasMessages(MESSAGE_CONNECT_OTHER_PROFILES) == false) && 1514 (isQuietModeEnabled()== false)){ 1515 Message m = mHandler.obtainMessage(MESSAGE_CONNECT_OTHER_PROFILES); 1516 m.obj = device; 1517 m.arg1 = (int)firstProfileStatus; 1518 mHandler.sendMessageDelayed(m,CONNECT_OTHER_PROFILES_TIMEOUT); 1519 } 1520 } 1521 1522 private void processConnectOtherProfiles (BluetoothDevice device, int firstProfileStatus){ 1523 if (getState()!= BluetoothAdapter.STATE_ON){ 1524 return; 1525 } 1526 HeadsetService hsService = HeadsetService.getHeadsetService(); 1527 A2dpService a2dpService = A2dpService.getA2dpService(); 1528 1529 // if any of the profile service is null, second profile connection not required 1530 if ((hsService == null) ||(a2dpService == null )){ 1531 return; 1532 } 1533 List<BluetoothDevice> a2dpConnDevList= a2dpService.getConnectedDevices(); 1534 List<BluetoothDevice> hfConnDevList= hsService.getConnectedDevices(); 1535 // Check if the device is in disconnected state and if so return 1536 // We ned to connect other profile only if one of the profile is still in connected state 1537 // This is required to avoide a race condition in which profiles would 1538 // automaticlly connect if the disconnection is initiated within 6 seconds of connection 1539 //First profile connection being rejected is an exception 1540 if((hfConnDevList.isEmpty() && a2dpConnDevList.isEmpty())&& 1541 (PROFILE_CONN_CONNECTED == firstProfileStatus)){ 1542 return; 1543 } 1544 if((hfConnDevList.isEmpty()) && 1545 (hsService.getPriority(device) >= BluetoothProfile.PRIORITY_ON)){ 1546 hsService.connect(device); 1547 } 1548 else if((a2dpConnDevList.isEmpty()) && 1549 (a2dpService.getPriority(device) >= BluetoothProfile.PRIORITY_ON)){ 1550 a2dpService.connect(device); 1551 } 1552 } 1553 1554 private void adjustOtherHeadsetPriorities(HeadsetService hsService, 1555 List<BluetoothDevice> connectedDeviceList) { 1556 for (BluetoothDevice device : getBondedDevices()) { 1557 if (hsService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT && 1558 !connectedDeviceList.contains(device)) { 1559 hsService.setPriority(device, BluetoothProfile.PRIORITY_ON); 1560 } 1561 } 1562 } 1563 1564 private void adjustOtherSinkPriorities(A2dpService a2dpService, 1565 BluetoothDevice connectedDevice) { 1566 for (BluetoothDevice device : getBondedDevices()) { 1567 if (a2dpService.getPriority(device) >= BluetoothProfile.PRIORITY_AUTO_CONNECT && 1568 !device.equals(connectedDevice)) { 1569 a2dpService.setPriority(device, BluetoothProfile.PRIORITY_ON); 1570 } 1571 } 1572 } 1573 1574 void setProfileAutoConnectionPriority (BluetoothDevice device, int profileId){ 1575 if (profileId == BluetoothProfile.HEADSET) { 1576 HeadsetService hsService = HeadsetService.getHeadsetService(); 1577 List<BluetoothDevice> deviceList = hsService.getConnectedDevices(); 1578 if ((hsService != null) && 1579 (BluetoothProfile.PRIORITY_AUTO_CONNECT != hsService.getPriority(device))){ 1580 adjustOtherHeadsetPriorities(hsService, deviceList); 1581 hsService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT); 1582 } 1583 } 1584 else if (profileId == BluetoothProfile.A2DP) { 1585 A2dpService a2dpService = A2dpService.getA2dpService(); 1586 if ((a2dpService != null) && 1587 (BluetoothProfile.PRIORITY_AUTO_CONNECT != a2dpService.getPriority(device))){ 1588 adjustOtherSinkPriorities(a2dpService, device); 1589 a2dpService.setPriority(device,BluetoothProfile.PRIORITY_AUTO_CONNECT); 1590 } 1591 } 1592 } 1593 1594 boolean cancelBondProcess(BluetoothDevice device) { 1595 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission"); 1596 byte[] addr = Utils.getBytesFromAddress(device.getAddress()); 1597 return cancelBondNative(addr); 1598 } 1599 1600 boolean removeBond(BluetoothDevice device) { 1601 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission"); 1602 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1603 if (deviceProp == null || deviceProp.getBondState() != BluetoothDevice.BOND_BONDED) { 1604 return false; 1605 } 1606 Message msg = mBondStateMachine.obtainMessage(BondStateMachine.REMOVE_BOND); 1607 msg.obj = device; 1608 mBondStateMachine.sendMessage(msg); 1609 return true; 1610 } 1611 1612 int getBondState(BluetoothDevice device) { 1613 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1614 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1615 if (deviceProp == null) { 1616 return BluetoothDevice.BOND_NONE; 1617 } 1618 return deviceProp.getBondState(); 1619 } 1620 1621 int getConnectionState(BluetoothDevice device) { 1622 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1623 byte[] addr = Utils.getBytesFromAddress(device.getAddress()); 1624 return getConnectionStateNative(addr); 1625 } 1626 1627 String getRemoteName(BluetoothDevice device) { 1628 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1629 if (mRemoteDevices == null) return null; 1630 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1631 if (deviceProp == null) return null; 1632 return deviceProp.getName(); 1633 } 1634 1635 int getRemoteType(BluetoothDevice device) { 1636 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1637 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1638 if (deviceProp == null) return BluetoothDevice.DEVICE_TYPE_UNKNOWN; 1639 return deviceProp.getDeviceType(); 1640 } 1641 1642 String getRemoteAlias(BluetoothDevice device) { 1643 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1644 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1645 if (deviceProp == null) return null; 1646 return deviceProp.getAlias(); 1647 } 1648 1649 boolean setRemoteAlias(BluetoothDevice device, String name) { 1650 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1651 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1652 if (deviceProp == null) return false; 1653 deviceProp.setAlias(device, name); 1654 return true; 1655 } 1656 1657 int getRemoteClass(BluetoothDevice device) { 1658 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1659 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1660 if (deviceProp == null) return 0; 1661 1662 return deviceProp.getBluetoothClass(); 1663 } 1664 1665 ParcelUuid[] getRemoteUuids(BluetoothDevice device) { 1666 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1667 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1668 if (deviceProp == null) return null; 1669 return deviceProp.getUuids(); 1670 } 1671 1672 boolean fetchRemoteUuids(BluetoothDevice device) { 1673 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1674 mRemoteDevices.fetchUuids(device); 1675 return true; 1676 } 1677 1678 1679 boolean setPin(BluetoothDevice device, boolean accept, int len, byte[] pinCode) { 1680 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, 1681 "Need BLUETOOTH ADMIN permission"); 1682 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1683 // Only allow setting a pin in bonding state, or bonded state in case of security upgrade. 1684 if (deviceProp == null || 1685 (deviceProp.getBondState() != BluetoothDevice.BOND_BONDING && 1686 deviceProp.getBondState() != BluetoothDevice.BOND_BONDED)) { 1687 return false; 1688 } 1689 1690 byte[] addr = Utils.getBytesFromAddress(device.getAddress()); 1691 return pinReplyNative(addr, accept, len, pinCode); 1692 } 1693 1694 boolean setPasskey(BluetoothDevice device, boolean accept, int len, byte[] passkey) { 1695 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1696 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1697 if (deviceProp == null || deviceProp.getBondState() != BluetoothDevice.BOND_BONDING) { 1698 return false; 1699 } 1700 1701 byte[] addr = Utils.getBytesFromAddress(device.getAddress()); 1702 return sspReplyNative(addr, AbstractionLayer.BT_SSP_VARIANT_PASSKEY_ENTRY, accept, 1703 Utils.byteArrayToInt(passkey)); 1704 } 1705 1706 boolean setPairingConfirmation(BluetoothDevice device, boolean accept) { 1707 enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, 1708 "Need BLUETOOTH ADMIN permission"); 1709 DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device); 1710 if (deviceProp == null || deviceProp.getBondState() != BluetoothDevice.BOND_BONDING) { 1711 return false; 1712 } 1713 1714 byte[] addr = Utils.getBytesFromAddress(device.getAddress()); 1715 return sspReplyNative(addr, AbstractionLayer.BT_SSP_VARIANT_PASSKEY_CONFIRMATION, 1716 accept, 0); 1717 } 1718 1719 int getPhonebookAccessPermission(BluetoothDevice device) { 1720 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1721 SharedPreferences pref = getSharedPreferences(PHONEBOOK_ACCESS_PERMISSION_PREFERENCE_FILE, 1722 Context.MODE_PRIVATE); 1723 if (!pref.contains(device.getAddress())) { 1724 return BluetoothDevice.ACCESS_UNKNOWN; 1725 } 1726 return pref.getBoolean(device.getAddress(), false) 1727 ? BluetoothDevice.ACCESS_ALLOWED : BluetoothDevice.ACCESS_REJECTED; 1728 } 1729 1730 boolean setPhonebookAccessPermission(BluetoothDevice device, int value) { 1731 enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, 1732 "Need BLUETOOTH PRIVILEGED permission"); 1733 SharedPreferences pref = getSharedPreferences(PHONEBOOK_ACCESS_PERMISSION_PREFERENCE_FILE, 1734 Context.MODE_PRIVATE); 1735 SharedPreferences.Editor editor = pref.edit(); 1736 if (value == BluetoothDevice.ACCESS_UNKNOWN) { 1737 editor.remove(device.getAddress()); 1738 } else { 1739 editor.putBoolean(device.getAddress(), value == BluetoothDevice.ACCESS_ALLOWED); 1740 } 1741 return editor.commit(); 1742 } 1743 1744 int getMessageAccessPermission(BluetoothDevice device) { 1745 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1746 SharedPreferences pref = getSharedPreferences(MESSAGE_ACCESS_PERMISSION_PREFERENCE_FILE, 1747 Context.MODE_PRIVATE); 1748 if (!pref.contains(device.getAddress())) { 1749 return BluetoothDevice.ACCESS_UNKNOWN; 1750 } 1751 return pref.getBoolean(device.getAddress(), false) 1752 ? BluetoothDevice.ACCESS_ALLOWED : BluetoothDevice.ACCESS_REJECTED; 1753 } 1754 1755 boolean setMessageAccessPermission(BluetoothDevice device, int value) { 1756 enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, 1757 "Need BLUETOOTH PRIVILEGED permission"); 1758 SharedPreferences pref = getSharedPreferences(MESSAGE_ACCESS_PERMISSION_PREFERENCE_FILE, 1759 Context.MODE_PRIVATE); 1760 SharedPreferences.Editor editor = pref.edit(); 1761 if (value == BluetoothDevice.ACCESS_UNKNOWN) { 1762 editor.remove(device.getAddress()); 1763 } else { 1764 editor.putBoolean(device.getAddress(), value == BluetoothDevice.ACCESS_ALLOWED); 1765 } 1766 return editor.commit(); 1767 } 1768 1769 int getSimAccessPermission(BluetoothDevice device) { 1770 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1771 SharedPreferences pref = getSharedPreferences(SIM_ACCESS_PERMISSION_PREFERENCE_FILE, 1772 Context.MODE_PRIVATE); 1773 if (!pref.contains(device.getAddress())) { 1774 return BluetoothDevice.ACCESS_UNKNOWN; 1775 } 1776 return pref.getBoolean(device.getAddress(), false) 1777 ? BluetoothDevice.ACCESS_ALLOWED : BluetoothDevice.ACCESS_REJECTED; 1778 } 1779 1780 boolean setSimAccessPermission(BluetoothDevice device, int value) { 1781 enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, 1782 "Need BLUETOOTH PRIVILEGED permission"); 1783 SharedPreferences pref = getSharedPreferences(SIM_ACCESS_PERMISSION_PREFERENCE_FILE, 1784 Context.MODE_PRIVATE); 1785 SharedPreferences.Editor editor = pref.edit(); 1786 if (value == BluetoothDevice.ACCESS_UNKNOWN) { 1787 editor.remove(device.getAddress()); 1788 } else { 1789 editor.putBoolean(device.getAddress(), value == BluetoothDevice.ACCESS_ALLOWED); 1790 } 1791 return editor.commit(); 1792 } 1793 1794 void sendConnectionStateChange(BluetoothDevice 1795 device, int profile, int state, int prevState) { 1796 // TODO(BT) permission check? 1797 // Since this is a binder call check if Bluetooth is on still 1798 if (getState() == BluetoothAdapter.STATE_OFF) return; 1799 1800 mAdapterProperties.sendConnectionStateChange(device, profile, state, prevState); 1801 1802 } 1803 1804 ParcelFileDescriptor connectSocket(BluetoothDevice device, int type, 1805 ParcelUuid uuid, int port, int flag) { 1806 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1807 int fd = connectSocketNative(Utils.getBytesFromAddress(device.getAddress()), 1808 type, Utils.uuidToByteArray(uuid), port, flag); 1809 if (fd < 0) { 1810 errorLog("Failed to connect socket"); 1811 return null; 1812 } 1813 return ParcelFileDescriptor.adoptFd(fd); 1814 } 1815 1816 ParcelFileDescriptor createSocketChannel(int type, String serviceName, 1817 ParcelUuid uuid, int port, int flag) { 1818 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1819 int fd = createSocketChannelNative(type, serviceName, 1820 Utils.uuidToByteArray(uuid), port, flag); 1821 if (fd < 0) { 1822 errorLog("Failed to create socket channel"); 1823 return null; 1824 } 1825 return ParcelFileDescriptor.adoptFd(fd); 1826 } 1827 1828 boolean configHciSnoopLog(boolean enable) { 1829 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1830 return configHciSnoopLogNative(enable); 1831 } 1832 1833 boolean factoryReset() { 1834 enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH permission"); 1835 return factoryResetNative(); 1836 } 1837 1838 void registerCallback(IBluetoothCallback cb) { 1839 mCallbacks.register(cb); 1840 } 1841 1842 void unregisterCallback(IBluetoothCallback cb) { 1843 mCallbacks.unregister(cb); 1844 } 1845 1846 public int getNumOfAdvertisementInstancesSupported() { 1847 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1848 return mAdapterProperties.getNumOfAdvertisementInstancesSupported(); 1849 } 1850 1851 public boolean isMultiAdvertisementSupported() { 1852 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1853 return getNumOfAdvertisementInstancesSupported() >= MIN_ADVT_INSTANCES_FOR_MA; 1854 } 1855 1856 public boolean isRpaOffloadSupported() { 1857 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1858 return mAdapterProperties.isRpaOffloadSupported(); 1859 } 1860 1861 public int getNumOfOffloadedIrkSupported() { 1862 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1863 return mAdapterProperties.getNumOfOffloadedIrkSupported(); 1864 } 1865 1866 public int getNumOfOffloadedScanFilterSupported() { 1867 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1868 return mAdapterProperties.getNumOfOffloadedScanFilterSupported(); 1869 } 1870 1871 public boolean isPeripheralModeSupported() { 1872 return getResources().getBoolean(R.bool.config_bluetooth_le_peripheral_mode_supported); 1873 } 1874 1875 public int getOffloadedScanResultStorage() { 1876 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1877 return mAdapterProperties.getOffloadedScanResultStorage(); 1878 } 1879 1880 private boolean isActivityAndEnergyReportingSupported() { 1881 enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH permission"); 1882 return mAdapterProperties.isActivityAndEnergyReportingSupported(); 1883 } 1884 1885 private void getActivityEnergyInfoFromController() { 1886 enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH permission"); 1887 if (isActivityAndEnergyReportingSupported()) { 1888 readEnergyInfo(); 1889 } 1890 } 1891 1892 private BluetoothActivityEnergyInfo reportActivityInfo() { 1893 enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH permission"); 1894 BluetoothActivityEnergyInfo info = 1895 new BluetoothActivityEnergyInfo(SystemClock.elapsedRealtime(), mStackReportedState, 1896 mTxTimeTotalMs, mRxTimeTotalMs, mIdleTimeTotalMs, mEnergyUsedTotalVoltAmpSecMicro); 1897 // Read on clear values; a record of data is created with 1898 // timstamp and new samples are collected until read again 1899 mStackReportedState = 0; 1900 mTxTimeTotalMs = 0; 1901 mRxTimeTotalMs = 0; 1902 mIdleTimeTotalMs = 0; 1903 mEnergyUsedTotalVoltAmpSecMicro = 0; 1904 return info; 1905 } 1906 1907 public int getTotalNumOfTrackableAdvertisements() { 1908 enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission"); 1909 return mAdapterProperties.getTotalNumOfTrackableAdvertisements(); 1910 } 1911 1912 public void onLeServiceUp() { 1913 Message m = mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON); 1914 mAdapterStateMachine.sendMessage(m); 1915 } 1916 1917 public void onBrEdrDown() { 1918 Message m = mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_OFF); 1919 mAdapterStateMachine.sendMessage(m); 1920 } 1921 1922 private static int convertScanModeToHal(int mode) { 1923 switch (mode) { 1924 case BluetoothAdapter.SCAN_MODE_NONE: 1925 return AbstractionLayer.BT_SCAN_MODE_NONE; 1926 case BluetoothAdapter.SCAN_MODE_CONNECTABLE: 1927 return AbstractionLayer.BT_SCAN_MODE_CONNECTABLE; 1928 case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: 1929 return AbstractionLayer.BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE; 1930 } 1931 // errorLog("Incorrect scan mode in convertScanModeToHal"); 1932 return -1; 1933 } 1934 1935 static int convertScanModeFromHal(int mode) { 1936 switch (mode) { 1937 case AbstractionLayer.BT_SCAN_MODE_NONE: 1938 return BluetoothAdapter.SCAN_MODE_NONE; 1939 case AbstractionLayer.BT_SCAN_MODE_CONNECTABLE: 1940 return BluetoothAdapter.SCAN_MODE_CONNECTABLE; 1941 case AbstractionLayer.BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE: 1942 return BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE; 1943 } 1944 //errorLog("Incorrect scan mode in convertScanModeFromHal"); 1945 return -1; 1946 } 1947 1948 // This function is called from JNI. It allows native code to set a single wake 1949 // alarm. If an alarm is already pending and a new request comes in, the alarm 1950 // will be rescheduled (i.e. the previously set alarm will be cancelled). 1951 private boolean setWakeAlarm(long delayMillis, boolean shouldWake) { 1952 synchronized (this) { 1953 if (mPendingAlarm != null) { 1954 mAlarmManager.cancel(mPendingAlarm); 1955 } 1956 1957 long wakeupTime = SystemClock.elapsedRealtime() + delayMillis; 1958 int type = shouldWake 1959 ? AlarmManager.ELAPSED_REALTIME_WAKEUP 1960 : AlarmManager.ELAPSED_REALTIME; 1961 1962 Intent intent = new Intent(ACTION_ALARM_WAKEUP); 1963 mPendingAlarm = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 1964 mAlarmManager.setExact(type, wakeupTime, mPendingAlarm); 1965 return true; 1966 } 1967 } 1968 1969 // This function is called from JNI. It allows native code to acquire a single wake lock. 1970 // If the wake lock is already held, this function returns success. Although this function 1971 // only supports acquiring a single wake lock at a time right now, it will eventually be 1972 // extended to allow acquiring an arbitrary number of wake locks. The current interface 1973 // takes |lockName| as a parameter in anticipation of that implementation. 1974 private boolean acquireWakeLock(String lockName) { 1975 if (mWakeLock != null) { 1976 if (!lockName.equals(mWakeLockName)) { 1977 errorLog("Multiple wake lock acquisition attempted; aborting: " + lockName); 1978 return false; 1979 } 1980 1981 // We're already holding the desired wake lock so return success. 1982 if (mWakeLock.isHeld()) { 1983 return true; 1984 } 1985 } 1986 1987 mWakeLockName = lockName; 1988 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, lockName); 1989 mWakeLock.acquire(); 1990 return true; 1991 } 1992 1993 // This function is called from JNI. It allows native code to release a wake lock acquired 1994 // by |acquireWakeLock|. If the wake lock is not held, this function returns failure. 1995 // Note that the release() call is also invoked by {@link #cleanup()} so a synchronization is 1996 // needed here. See the comment for |acquireWakeLock| for an explanation of the interface. 1997 private boolean releaseWakeLock(String lockName) { 1998 synchronized (this) { 1999 if (mWakeLock == null) { 2000 errorLog("Repeated wake lock release; aborting release: " + lockName); 2001 return false; 2002 } 2003 2004 mWakeLock.release(); 2005 mWakeLock = null; 2006 mWakeLockName = null; 2007 } 2008 return true; 2009 } 2010 2011 private void energyInfoCallback (int status, int ctrl_state, 2012 long tx_time, long rx_time, long idle_time, long energy_used) 2013 throws RemoteException { 2014 if (ctrl_state >= BluetoothActivityEnergyInfo.BT_STACK_STATE_INVALID && 2015 ctrl_state <= BluetoothActivityEnergyInfo.BT_STACK_STATE_STATE_IDLE) { 2016 mStackReportedState = ctrl_state; 2017 mTxTimeTotalMs += tx_time; 2018 mRxTimeTotalMs += rx_time; 2019 mIdleTimeTotalMs += idle_time; 2020 // Energy is product of mA, V and ms. If the chipset doesn't 2021 // report it, we have to compute it from time 2022 if (energy_used == 0) { 2023 energy_used = (long)((mTxTimeTotalMs * getTxCurrentMa() 2024 + mRxTimeTotalMs * getRxCurrentMa() 2025 + mIdleTimeTotalMs * getIdleCurrentMa()) * getOperatingVolt()); 2026 } 2027 mEnergyUsedTotalVoltAmpSecMicro += energy_used; 2028 } 2029 2030 debugLog("energyInfoCallback() status = " + status + 2031 "tx_time = " + tx_time + "rx_time = " + rx_time + 2032 "idle_time = " + idle_time + "energy_used = " + energy_used + 2033 "ctrl_state = " + ctrl_state); 2034 } 2035 2036 private int getIdleCurrentMa() { 2037 return getResources().getInteger(R.integer.config_bluetooth_idle_cur_ma); 2038 } 2039 2040 private int getTxCurrentMa() { 2041 return getResources().getInteger(R.integer.config_bluetooth_tx_cur_ma); 2042 } 2043 2044 private int getRxCurrentMa() { 2045 return getResources().getInteger(R.integer.config_bluetooth_rx_cur_ma); 2046 } 2047 2048 private double getOperatingVolt() { 2049 return getResources().getInteger(R.integer.config_bluetooth_operating_voltage_mv) / 1000.0; 2050 } 2051 2052 private void dump(FileDescriptor fd) { 2053 // Collect profile information 2054 StringBuilder sb = new StringBuilder(); 2055 synchronized (mProfiles) { 2056 for (ProfileService profile : mProfiles) { 2057 profile.dump(sb); 2058 } 2059 } 2060 2061 // Dump Java based profiles first 2062 FileWriter fw = null; 2063 try { 2064 fw = new FileWriter(fd); 2065 fw.write(sb.toString()); 2066 } catch (IOException ex) { 2067 errorLog("IOException writing profile status!"); 2068 } finally { 2069 if (fw != null) { 2070 try { 2071 fw.close(); 2072 } catch (IOException ex) { 2073 debugLog("IOException closing a file after writing the profile status"); 2074 } 2075 } 2076 } 2077 2078 // Add native logs 2079 dumpNative(fd); 2080 } 2081 2082 private void debugLog(String msg) { 2083 if (DBG) Log.d(TAG, msg); 2084 } 2085 2086 private void errorLog(String msg) { 2087 Log.e(TAG, msg); 2088 } 2089 2090 private final BroadcastReceiver mAlarmBroadcastReceiver = new BroadcastReceiver() { 2091 @Override 2092 public void onReceive(Context context, Intent intent) { 2093 synchronized (AdapterService.this) { 2094 mPendingAlarm = null; 2095 alarmFiredNative(); 2096 } 2097 } 2098 }; 2099 2100 private native static void classInitNative(); 2101 private native boolean initNative(); 2102 private native void cleanupNative(); 2103 /*package*/ native boolean enableNative(); 2104 /*package*/ native boolean disableNative(); 2105 /*package*/ native boolean setAdapterPropertyNative(int type, byte[] val); 2106 /*package*/ native boolean getAdapterPropertiesNative(); 2107 /*package*/ native boolean getAdapterPropertyNative(int type); 2108 /*package*/ native boolean setAdapterPropertyNative(int type); 2109 /*package*/ native boolean 2110 setDevicePropertyNative(byte[] address, int type, byte[] val); 2111 /*package*/ native boolean getDevicePropertyNative(byte[] address, int type); 2112 2113 /*package*/ native boolean createBondNative(byte[] address, int transport); 2114 /*package*/ native boolean removeBondNative(byte[] address); 2115 /*package*/ native boolean cancelBondNative(byte[] address); 2116 /*package*/ native boolean sdpSearchNative(byte[] address, byte[] uuid); 2117 2118 /*package*/ native int getConnectionStateNative(byte[] address); 2119 2120 private native boolean startDiscoveryNative(); 2121 private native boolean cancelDiscoveryNative(); 2122 2123 private native boolean pinReplyNative(byte[] address, boolean accept, int len, byte[] pin); 2124 private native boolean sspReplyNative(byte[] address, int type, boolean 2125 accept, int passkey); 2126 2127 /*package*/ native boolean getRemoteServicesNative(byte[] address); 2128 /*package*/ native boolean getRemoteMasInstancesNative(byte[] address); 2129 2130 private native int readEnergyInfo(); 2131 // TODO(BT) move this to ../btsock dir 2132 private native int connectSocketNative(byte[] address, int type, 2133 byte[] uuid, int port, int flag); 2134 private native int createSocketChannelNative(int type, String serviceName, 2135 byte[] uuid, int port, int flag); 2136 2137 /*package*/ native boolean configHciSnoopLogNative(boolean enable); 2138 /*package*/ native boolean factoryResetNative(); 2139 2140 private native void alarmFiredNative(); 2141 private native void dumpNative(FileDescriptor fd); 2142 2143 protected void finalize() { 2144 cleanup(); 2145 if (TRACE_REF) { 2146 synchronized (AdapterService.class) { 2147 sRefCount--; 2148 debugLog("finalize() - REFCOUNT: FINALIZED. INSTANCE_COUNT= " + sRefCount); 2149 } 2150 } 2151 } 2152} 2153