SAM Module
This functionality defines operations of built-in SAM 1 and SAM 2 modules.
In order to use the functions listed above you need to have installed myPOS OS version 0.0.8. myPOS OS Version can be checked Only when the device is in “Debug Mode” in “About” submenu in myPOS Terminal App.
1. SAM Module operation
//Build SAM module operation
private static final int SAM_SLOT_1 = 1;
private static final int SAM_SLOT_2 = 2;
private void startSAMTest() {
final Context context = this;
Thread r = new Thread(new Runnable(){
@Override
public void run() {
try {
int slotNumber = SAM_SLOT_1;
int timeoutMs = 1000;
boolean hasCard;
byte[] resp;
byte[] cmd = new byte[] {(byte)0x00,(byte)0xA4,(byte)0x00,(byte)0x00,(byte)0x02, (byte) 0x3f, (byte) 0x00}; // SELECT command for file 0x3F00 (GSM card master file)
hasCard = SAMCard.detect(context, slotNumber, timeoutMs);
if (!hasCard) {
showToast("No SAM card detected in slot " + slotNumber);
return;
}
showToast("SAM card detected in slot " + slotNumber + ". Initializing");
resp = SAMCard.open(context, slotNumber, timeoutMs);
showToast("Initializing SAM successful. Sending command");
resp = SAMCard.isoCommand(context, slotNumber, timeoutMs, cmd);
showToast("Response to SAM command received. Closing SAM");
SAMCard.close(context, slotNumber, timeoutMs);
showToast("SAM module closed");
} catch (Exception e) {
e.printStackTrace();
showToast(e.getMessage());
}
}
});
r.start();
}
public void showToast(final String toast)
{
runOnUiThread(new Runnable() {
public void run()
{
Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();
}
});
}
Print the last transaction receipt
Printing the last transaction receipt is done by sending a broadcast.
1. Send the broadcast
Intent intent = new Intent(MyPOSUtil.PRINT_LAST_RECEIPT_BROADCAST);
// Whether or not a copy for the customer should be printed
intent.putExtra("print_customer_receipt", true);
sendBroadcast(intent);
2. Handle the printing result
When the printing is finished, the Payment core will return a broadcast with intent com.mypos.broadcast.PRINTING_DONE
.
public class PrinterResultBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean printing_started = intent.getBooleanExtra("printing_started", false);
int printer_status = intent.getIntExtra("printer_status", PrinterStatus.PRINTER_STATUS_UNKNOWN_ERROR);
// If the printing has actually started, handle the status
if (printing_started) {
// Handle success and errors
if (printer_status == PrinterStatus.PRINTER_STATUS_SUCCESS) {
Toast.makeText(context, "Printing successful!", Toast.LENGTH_SHORT).show();
// Printing is successful
} else if (printer_status == PrinterStatus.PRINTER_STATUS_OUT_OF_PAPER) {
// Show "missing paper" dialog
Toast.makeText(context, "No paper in the printer", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, String.format("Some error occurred while printing. Status: %d", printer_status), Toast.LENGTH_SHORT).show();
}
// etc.
} else {
// Some other error occurred. Maybe there's no transaction data (when printing last transaction receipt).
Toast.makeText(context, String.format("Error occurred while printing. Status: %d", printer_status), Toast.LENGTH_SHORT).show();
}
}
}
Print a custom receipt
Just like reprinting the last receipt, printing a custom receipt is done by sending a broadcast.
1. Send the print broadcast
The printing broadcast accepts a list of PrinterCommand objects serialized as JSON. Gson can be used to serialize the objects.
A PrinterCommand can be one of the following types:
- HEADER – prints merchant data and time and date. The time and date must be sent with the broadcast;
- LOGO – prints the device’s logo;
- TEXT – prints arbitrary text. Double width and height are available as parameters;
- FOOTER – prints a footer with a “Thank you” message;
- IMAGE - prints a custom bitmap image – e.g. QR Code.
If you decide to use Gson, add it to your project’s build.gradle
file: compile 'com.google.code.gson:gson:2.8.0'
An example print broadcast can look like this:
String json; // The serialized list of commands
List<PrinterCommand> commands = new ArrayList<>();
// Add commands to be sent
commands.add(new PrinterCommand(PrinterCommand.CommandType.TEXT, "Normal row 1\n"));
// [...]
commands.add(new PrinterCommand(PrinterCommand.CommandType.TEXT, "Double height\n\n\n", false, true));
// [...]
commands.add(new PrinterCommand(PrinterCommand.CommandType.FOOTER));
//[...]
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
commands.add(new PrinterCommand(PrinterCommand.CommandType.IMAGE, bitmap));
// Serialize the command list
Gson gson = new Gson();
json = gson.toJson(commands);
System.out.println("Sending print broadcast: " + json);
Intent intent = new Intent(MyPOSUtil.PRINT_BROADCAST);
// Add the commands
intent.putExtra("commands", json);
// Send broadcast
MyPOSAPI.sendExplicitBroadcast(context, intent);
2. Handle the printing result
When the printing is finished, the Payment core will return a broadcast with intent com.mypos.broadcast.PRINTING_DONE
.
public class PrinterResultBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean printing_started = intent.getBooleanExtra("printing_started", false);
int printer_status = intent.getIntExtra("printer_status", PrinterStatus.PRINTER_STATUS_UNKNOWN_ERROR);
// If the printing has actually started, handle the status
if (printing_started) {
// Handle success and errors
if (printer_status == PrinterStatus.PRINTER_STATUS_SUCCESS) {
Toast.makeText(context, "Printing successful!", Toast.LENGTH_SHORT).show();
// Printing is successful
} else if (printer_status == PrinterStatus.PRINTER_STATUS_OUT_OF_PAPER) {
// Show "missing paper" dialog
Toast.makeText(context, "No paper in the printer", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, String.format("Some error occurred while printing. Status: %d", printer_status), Toast.LENGTH_SHORT).show();
}
// etc.
} else {
// Some other error occurred. Maybe there's no transaction data (when printing last transaction receipt).
Toast.makeText(context, String.format("Error occurred while printing. Status: %d", printer_status), Toast.LENGTH_SHORT).show();
}
}
}