@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    // if the intent is null, it means the service was restarted after being killed by the system
    if (intent.getAction().equals(INTENT_ACTION_LISTEN_FOR_BLUETOOTH_CONNECTIONS)) {
      reset();
      listen();
    } else if (intent.getAction().equals(INTENT_ACTION_CONNECT_TO_BLUETOOTH_DEVICE)) {
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      reset();
      connect(device);
    }

    // if the system kills the service to reclaim resources, it should restart it later,
    // resending the intent.
    // see http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT
    return START_REDELIVER_INTENT;
  }