Reader SDK: Code Cookbook

Connect a Contactless Reader

Connect Reader SDK to a Square Contactless Reader to accept chip and tap payments.

Android
iOS
Java (Android)

Before you start
Permalink Get a link to this section

  • You need to have installed and configured Reader SDK.

Did you know?

If you are using a Magstripe Reader, you do not need to write explicit code to connect the Reader. If you are using a Contactless + Chip Reader, you must add code to handle pairing.

Step 1: Launch the Card Reader flow
Permalink Get a link to this section

Add code to your Checkout activity to display a button that launches the Square Card Reader settings flow by calling ReaderManager#startReaderSettingsActivity() and passing in the activity:

import com.squareup.sdk.reader.hardware.ReaderManager;
import com.squareup.sdk.reader.hardware.ReaderSettingsErrorCode;

public class CheckoutActivity extends Activity {

  private CallbackReference readerSettingsCallbackRef;

  @Override protected void onCreate(Bundle savedInstanceState) {

    // ...

    View readerSettingsButton = findViewById(R.id.reader_settings_button);
    readerSettingsButton.setOnClickListener(view -> startReaderSettings());

  }

  private void startReaderSettings() {
    ReaderManager readerManager = ReaderSdk.readerManager();
    readerManager.startReaderSettingsActivity(this);
  }

Step 2: Add a Reader Manager callback
Permalink Get a link to this section

Add a callback (onReaderSettingsResult()) to the Reader Manager in the onCreate() method of your checkout activity using ReaderManager.addReaderSettingsActivityCallback:

  @Override protected void onCreate(Bundle savedInstanceState) {

    // ...

    ReaderManager readerManager = ReaderSdk.readerManager();
    readerSettingsCallbackRef = readerManager.addReaderSettingsActivityCallback(
        this::onReaderSettingsResult
    );

    private void onReaderSettingsResult(
        Result<Void, ResultError<ReaderSettingsErrorCode>> result) {
      // TODO: Handle the pairing result
    }
  }
}

Step 3: Handle the Reader Settings result
Permalink Get a link to this section

Implement the onReaderSettingsResult() method by adding code to respond to the pairing result:

private void onReaderSettingsResult(
    Result<Void, ResultError<ReaderSettingsErrorCode>> result) {
  if (result.isError()) {
    ResultError<ReaderSettingsErrorCode> error = result.getError();
      switch (error.getCode()) {
        case SDK_NOT_AUTHORIZED:
          goToAuthorizeActivity();
          break;
        case USAGE_ERROR:
          showErrorDialog(error);
          break;
      }
    }
  }

Note

The result object provided to ReaderSettingsActivityCallback.onResult() does not contain a success value. It only contains a ResultError object in the case of an error.

Step 4: Update onDestroy
Permalink Get a link to this section

Add code to clear the reader settings callback reference in your onDestroy() method to avoid memory leaks:

public class CheckoutActivity extends Activity {

  // ...

  @Override protected void onDestroy() {
    super.onDestroy();
    readerSettingsCallbackRef.clear();
  }
}