@Override
 protected void onDestroy() {
   super.onDestroy();
   if (mReceiverRegistered) {
     unregisterReceiver(mReceiver);
     mReceiverRegistered = false;
   }
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i = getIntent();
    String action = i.getAction();
    if (!action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST)) {
      Log.e(
          TAG,
          "Error: this activity may be started only with intent "
              + "ACTION_CONNECTION_ACCESS_REQUEST");
      finish();
      return;
    }

    mDevice = i.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    mReturnPackage = i.getStringExtra(BluetoothDevice.EXTRA_PACKAGE_NAME);
    mReturnClass = i.getStringExtra(BluetoothDevice.EXTRA_CLASS_NAME);
    mRequestType =
        i.getIntExtra(
            BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE,
            BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS);

    if (DEBUG) Log.i(TAG, "onCreate() Request type: " + mRequestType);

    if (mRequestType == BluetoothDevice.REQUEST_TYPE_PROFILE_CONNECTION) {
      showDialog(getString(R.string.bluetooth_connection_permission_request), mRequestType);
    } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_PHONEBOOK_ACCESS) {
      showDialog(getString(R.string.bluetooth_phonebook_request), mRequestType);
    } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_MESSAGE_ACCESS) {
      showDialog(getString(R.string.bluetooth_map_request), mRequestType);
    } else if (mRequestType == BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
      showDialog(getString(R.string.bluetooth_sap_request), mRequestType);
    } else {
      Log.e(TAG, "Error: bad request type: " + mRequestType);
      finish();
      return;
    }
    registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL));
    mReceiverRegistered = true;
  }