@Override
 public void onDestroy() {
   super.onDestroy();
   if (DBG) log("Stopping BluetoothHeadsetService");
   unregisterReceiver(mBluetoothReceiver);
   mBtHandsfree.onBluetoothDisabled();
   mAg.stop();
   sHasStarted = false;
   if (getCurrentDevice() != null) {
     setState(
         getCurrentDevice(),
         BluetoothHeadset.STATE_DISCONNECTED,
         BluetoothHeadset.RESULT_CANCELED,
         BluetoothHeadset.LOCAL_DISCONNECT);
   }
 }
 @Override
 public void onStart(Intent intent, int startId) {
   if (mAdapter == null) {
     Log.w(TAG, "Stopping BluetoothHeadsetService: device does not have BT");
     stopSelf();
   } else {
     if (!sHasStarted) {
       if (DBG) log("Starting BluetoothHeadsetService");
       if (mAdapter.isEnabled()) {
         mAg.start(mIncomingConnectionHandler);
         mBtHandsfree.onBluetoothEnabled();
       }
       sHasStarted = true;
     }
   }
 }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

          BluetoothDevice currDevice = getCurrentDevice();
          int state = BluetoothHeadset.STATE_DISCONNECTED;
          if (currDevice != null) {
            state = mRemoteHeadsets.get(currDevice).mState;
          }

          if ((state == BluetoothHeadset.STATE_CONNECTED
                  || state == BluetoothHeadset.STATE_CONNECTING)
              && action.equals(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED)
              && device.equals(currDevice)) {
            try {
              mBinder.disconnectHeadset(currDevice);
            } catch (RemoteException e) {
            }
          } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            switch (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
              case BluetoothAdapter.STATE_ON:
                adjustPriorities();
                mAg.start(mIncomingConnectionHandler);
                mBtHandsfree.onBluetoothEnabled();
                break;
              case BluetoothAdapter.STATE_TURNING_OFF:
                mBtHandsfree.onBluetoothDisabled();
                mAg.stop();
                if (currDevice != null) {
                  setState(
                      currDevice,
                      BluetoothHeadset.STATE_DISCONNECTED,
                      BluetoothHeadset.RESULT_FAILURE,
                      BluetoothHeadset.LOCAL_DISCONNECT);
                }
                break;
            }
          } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
            int bondState =
                intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
            switch (bondState) {
              case BluetoothDevice.BOND_BONDED:
                if (getPriority(device) == BluetoothHeadset.PRIORITY_UNDEFINED) {
                  setPriority(device, BluetoothHeadset.PRIORITY_ON);
                }
                break;
              case BluetoothDevice.BOND_NONE:
                setPriority(device, BluetoothHeadset.PRIORITY_UNDEFINED);
                break;
            }
          } else if (action.equals(AudioManager.VOLUME_CHANGED_ACTION)) {
            int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
            if (streamType == AudioManager.STREAM_BLUETOOTH_SCO) {
              mBtHandsfree.sendScoGainUpdate(
                  intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0));
            }

          } else if (action.equals(BluetoothDevice.ACTION_UUID)) {
            if (device.equals(mDeviceSdpQuery) && device.equals(currDevice)) {
              // We have got SDP records for the device we are interested in.
              getSdpRecordsAndConnect(device);
            }
          }
        }