### SAM Manager Operations for NFC Authentication (Java) Source: https://context7.com/zebradevs/emdk-android-samples/llms.txt This Java code snippet demonstrates the core functionality of the SAM Manager for interacting with Secure Access Modules (SAM) to authenticate NFC cards. It includes initializing the EMDK, enumerating SAMs, connecting to a specific SAM type, sending APDU commands, and handling responses. It relies on the EMDK library and Android's NFC APIs. ```java public class SAMOperations extends Activity implements EMDKListener { private SAMManager samManager = null; private HashMap samList = new HashMap<>(); private NfcAdapter nfcAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize NFC adapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter == null) { Log.e("SAM", "NFC not supported on this device"); return; } EMDKResults results = EMDKManager.getEMDKManager(getApplicationContext(), this); } @Override public void onOpened(EMDKManager emdkManager) { // Get SAMManager instance samManager = (SAMManager) emdkManager.getInstance(EMDKManager.FEATURE_TYPE.SAM); // Enumerate available SAMs try { List sams = samManager.enumerateSAMs(); if (sams != null && sams.size() > 0) { for (SAM sam : sams) { samList.put(sam.getSamIndex(), sam); Log.d("SAM", "Found SAM: " + sam.getSamType() + " (Slot " + sam.getSamIndex() + ")"); } } } catch (SAMException e) { Log.e("SAM", "Enumeration error: " + SAMResults.getErrorDescription(e.getResult())); } } public void performSAMOperation(SAMType samType) { // Get SAM of specified type SAM sam = null; for (SAM s : samList.values()) { if (s.getSamType() == samType) { sam = s; break; } } if (sam == null) { Log.e("SAM", "SAM type " + samType + " not found"); return; } try { // Connect to SAM if (!sam.isConnected()) { sam.connect(); Log.d("SAM", "Connected to " + sam.getSamType() + " (Slot " + sam.getSamIndex() + ")"); } // Send GetVersion APDU command byte[] getVersionAPDU; if (samType == SAMType.MIFARE) { getVersionAPDU = new byte[]{ (byte) 0x80, (byte) 0x60, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; } else if (samType == SAMType.FELICA) { getVersionAPDU = new byte[]{ (byte) 0xA0, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xE6, (byte) 0x00, (byte) 0x00 }; } else { Log.e("SAM", "Unsupported SAM type"); return; } // Transceive APDU command byte[] response = sam.transceive(getVersionAPDU, (short) 0, false); if (response != null) { StringBuilder sb = new StringBuilder(); for (byte b : response) { sb.append(String.format("0x%02x ", b)); } Log.d("SAM", "Version response: " + sb.toString()); } else { Log.e("SAM", "No response from SAM"); } // Disconnect from SAM if (sam.isConnected()) { sam.disconnect(); Log.d("SAM", "Disconnected from SAM"); } } catch (SAMException e) { Log.e("SAM", "Operation error: " + SAMResults.getErrorDescription(e.getResult())); } } // Handle NFC tag detection @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // Determine compatible SAM type for the tag SAMType compatibleSAM = detectCompatibleSAM(tag); if (compatibleSAM != SAMType.UNKNOWN) { Log.d("SAM", "Tag detected - Compatible with: " + compatibleSAM); performSAMOperation(compatibleSAM); } else { Log.w("SAM", "No compatible SAM for detected tag"); } } } private SAMType detectCompatibleSAM(Tag tag) { // Check for MIFARE Classic MifareClassic mifareClassic = MifareClassic.get(tag); if (mifareClassic != null) { return SAMType.MIFARE; } // Check for FeliCa // ... (FeliCa detection logic would go here) return SAMType.UNKNOWN; } } ``` -------------------------------- ### Java Barcode Scanning Basic Implementation EMDK Android Source: https://context7.com/zebradevs/emdk-android-samples/llms.txt This Java code snippet demonstrates a basic implementation of barcode scanning using the EMDK Android library. It initializes the EMDK Manager, configures the barcode scanner with specific decoders (EAN8, EAN13, Code39, Code128), enables scanning, and processes received barcode data. Error handling for scanner exceptions and lifecycle management (onDestroy) for releasing resources are included. ```java import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.symbol.emdk.EMDKManager; import com.symbol.emdk.EMDKResults; import com.symbol.emdk.barcode.BarcodeManager; import com.symbol.emdk.barcode.ScanDataCollection; import com.symbol.emdk.barcode.Scanner; import com.symbol.emdk.barcode.ScannerConfig; import com.symbol.emdk.barcode.ScannerException; import com.symbol.emdk.barcode.ScannerInfo; import com.symbol.emdk.barcode.ScannerResults; import com.symbol.emdk.EMDKManager.FEATURE_TYPE; import com.symbol.emdk.barcode.StatusData; import com.symbol.emdk.barcode.StatusData.ScannerStates; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity implements EMDKManager.EMDKListener, ScanDataCollection.DataListener, StatusData.StatusListener { private EMDKManager emdkManager = null; private BarcodeManager barcodeManager = null; private Scanner scanner = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Request EMDK Manager EMDKResults results = EMDKManager.getEMDKManager(getApplicationContext(), this); if (results.statusCode != EMDKResults.STATUS_CODE.SUCCESS) { updateStatus("EMDKManager object request failed!"); } } @Override public void onOpened(EMDKManager emdkManager) { this.emdkManager = emdkManager; // Get BarcodeManager instance barcodeManager = (BarcodeManager) emdkManager.getInstance(FEATURE_TYPE.BARCODE); if (barcodeManager != null) { barcodeManager.addConnectionListener(this); } // Enumerate and select scanner List deviceList = barcodeManager.getSupportedDevicesInfo(); if (deviceList != null && deviceList.size() > 0) { scanner = barcodeManager.getDevice(deviceList.get(0)); scanner.addDataListener(this); scanner.addStatusListener(this); try { scanner.enable(); // Configure decoders ScannerConfig config = scanner.getConfig(); config.decoderParams.ean8.enabled = true; config.decoderParams.ean13.enabled = true; config.decoderParams.code39.enabled = true; config.decoderParams.code128.enabled = true; scanner.setConfig(config); // Start scanning scanner.read(); } catch (ScannerException e) { updateStatus("Scanner error: " + e.getMessage()); } } } @Override public void onData(ScanDataCollection scanDataCollection) { if (scanDataCollection != null && scanDataCollection.getResult() == ScannerResults.SUCCESS) { ArrayList scanData = scanDataCollection.getScanData(); for (ScanDataCollection.ScanData data : scanData) { String labelType = data.getLabelType().toString(); String barcodeData = data.getData(); Log.d("Barcode", labelType + ": " + barcodeData); // Process barcode data processBarcode(labelType, barcodeData); } } } @Override public void onStatus(StatusData statusData) { ScannerStates state = statusData.getState(); switch(state) { case IDLE: // Scanner ready - submit read if (!scanner.isReadPending()) { try { scanner.read(); } catch (ScannerException e) { Log.e("Scanner", e.getMessage()); } } break; case WAITING: Log.d("Scanner", "Waiting for trigger press"); break; case SCANNING: Log.d("Scanner", "Scanning in progress"); break; case DISABLED: Log.w("Scanner", "Scanner disabled"); break; } } @Override protected void onDestroy() { super.onDestroy(); if (scanner != null) { try { scanner.disable(); scanner.removeDataListener(this); scanner.removeStatusListener(this); scanner.release(); } catch (Exception e) { Log.e("Scanner", "Cleanup error: " + e.getMessage()); } } if (emdkManager != null) { emdkManager.release(); } } // Placeholder for status update and barcode processing private void updateStatus(String message) { Log.i("Status", message); // Update UI or handle status message } private void processBarcode(String labelType, String barcodeData) { Log.i("BarcodeData", "Type: " + labelType + ", Data: " + barcodeData); // Process the scanned barcode data as needed } } ``` -------------------------------- ### Write and Read Serial Command in Android Source: https://context7.com/zebradevs/emdk-android-samples/llms.txt This snippet demonstrates writing a command to a serial port and reading the response. It includes a delay to allow the device to respond and a timeout for the read operation. Proper error handling for interruptions and null responses is included. The primary dependency is the Android SDK for threading and logging. ```java String command = "AT+VERSION\r\n"; if (writeData(command)) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } String response = readData(5000); if (response != null) { Log.d("Serial", "Device response: " + response); } } closeSerialPort(); ``` -------------------------------- ### Control Device Notifications with Java Source: https://context7.com/zebradevs/emdk-android-samples/llms.txt This Java code snippet demonstrates how to control LED, beeper, and vibrator notifications on Zebra devices. It requires the EMDK for Android library. The code initializes the NotificationManager, retrieves supported notification devices, enables a default device, and provides methods to send success and error notifications with customizable patterns. It also includes a method to cancel ongoing notifications. ```java // Control device notifications (LED, Beeper, Vibrator) public class NotificationControl extends Activity implements EMDKListener { private NotificationManager notificationManager = null; private NotificationDevice notificationDevice = null; @Override public void onOpened(EMDKManager emdkManager) { // Get NotificationManager instance notificationManager = (NotificationManager) emdkManager.getInstance(FEATURE_TYPE.NOTIFICATION); // Enumerate available notification devices List deviceList = notificationManager.getSupportedDevicesInfo(); if (deviceList != null && deviceList.size() > 0) { // Get default notification device for (DeviceInfo info : deviceList) { if (info.isDefaultDevice()) { try { notificationDevice = notificationManager.getDevice(info); notificationDevice.enable(); Log.d("Notification", "Device: " + info.getFriendlyName()); Log.d("Notification", "LED supported: " + info.isLEDSupported()); Log.d("Notification", "Beep supported: " + info.isBeepSupported()); Log.d("Notification", "Vibrate supported: " + info.isVibrateSupported()); } catch (NotificationException e) { Log.e("Notification", e.getMessage()); } break; } } } } public void sendSuccessNotification() { if (notificationDevice != null && notificationDevice.isConnected()) { try { Notification notification = new Notification(); // Configure LED (green color, 4 short blinks) notification.led.color = 0x00FF00; // Green notification.led.onTime = 250; // 250ms on notification.led.offTime = 250; // 250ms off notification.led.repeatCount = 3; // 4 blinks total // Configure Beeper (high frequency, short beeps) Beep[] beepPattern = new Beep[4]; for (int i = 0; i < 4; i++) { beepPattern[i] = new Beep(); beepPattern[i].frequency = 2000; // 2000 Hz beepPattern[i].time = 250; // 250ms } notification.beep.pattern = beepPattern; // Configure Vibrator (short vibration) notification.vibrate.time = 250; // 250ms vibration // Send notification notificationDevice.notify(notification); } catch (NotificationException e) { Log.e("Notification", "Error: " + e.getMessage()); } } } public void sendErrorNotification() { if (notificationDevice != null && notificationDevice.isConnected()) { try { Notification notification = new Notification(); // Red LED, 2 long blinks notification.led.color = 0xFF0000; // Red notification.led.onTime = 2550; // 2.5 seconds on notification.led.offTime = 1000; // 1 second off notification.led.repeatCount = 1; // 2 blinks total // Low frequency, long beeps Beep[] beepPattern = new Beep[2]; beepPattern[0] = new Beep(); beepPattern[0].frequency = 1000; // 1000 Hz beepPattern[0].time = 2500; // 2.5 seconds beepPattern[1] = new Beep(); beepPattern[1].frequency = 0; // Silent beepPattern[1].time = 1000; // 1 second pause notification.beep.pattern = beepPattern; // Long vibration pattern long[] vibratePattern = new long[] {1000, 2550, 1000, 2550}; notification.vibrate.pattern = vibratePattern; notificationDevice.notify(notification); } catch (NotificationException e) { Log.e("Notification", "Error: " + e.getMessage()); } } } public void cancelNotification() { if (notificationDevice != null && notificationDevice.isConnected()) { try { notificationDevice.cancelNotification(); } catch (NotificationException e) { Log.e("Notification", "Cancel error: " + e.getMessage()); } } } } ``` -------------------------------- ### Configure and Use Multi-Barcode Scanning in Java Source: https://context7.com/zebradevs/emdk-android-samples/llms.txt This Java code configures the Zebra EMDK scanner to capture multiple barcodes simultaneously. It sets the scan mode to MULTI_BARCODE, specifies the desired number of barcodes, and handles the callback for processing the scanned data. It includes error handling for unsupported features and scanner exceptions. ```java public class MultiBarcodeSample extends Activity implements EMDKListener, DataListener { private Scanner scanner = null; private int barcodeCount = 5; // Number of barcodes to scan private void setMultiBarcodeConfig() { if (scanner != null && scanner.isEnabled()) { try { ScannerConfig config = scanner.getConfig(); // Enable multi-barcode mode for imager config.readerParams.readerSpecific.imagerSpecific.scanMode = ScannerConfig.ScanMode.MULTI_BARCODE; // Enable multi-barcode mode for camera config.readerParams.readerSpecific.cameraSpecific.scanMode = ScannerConfig.ScanMode.MULTI_BARCODE; // Set how many barcodes to capture config.multiBarcodeParams.barcodeCount = barcodeCount; // Apply configuration scanner.setConfig(config); // Check if multi-barcode is supported if (!config.isParamSupported("config.multiBarcodeParams.barcodeCount")) { Log.w("MultiBarcode", "Multi-barcode feature not supported"); } } catch (ScannerException e) { Log.e("MultiBarcode", "Config error: " + e.getMessage()); } } } @Override public void onData(ScanDataCollection scanDataCollection) { if (scanDataCollection != null && scanDataCollection.getResult() == ScannerResults.SUCCESS) { ArrayList scanData = scanDataCollection.getScanData(); Log.d("MultiBarcode", "Scanned " + scanData.size() + " barcodes"); // Process all scanned barcodes List results = new ArrayList<>(); for (ScanData data : scanData) { BarcodeResult result = new BarcodeResult(); result.type = data.getLabelType().toString(); result.data = data.getData(); result.timestamp = System.currentTimeMillis(); results.add(result); Log.d("MultiBarcode", "Type: " + result.type + ", Data: " + result.data); } // Display or process results displayMultiBarcodeResults(results); } } public void startMultiBarcodeScan() { if (scanner != null && scanner.isEnabled()) { try { // Set trigger type for continuous scanning scanner.triggerType = TriggerType.SOFT_ALWAYS; // Submit scan request scanner.read(); Log.d("MultiBarcode", "Multi-barcode scan started"); } catch (ScannerException e) { Log.e("MultiBarcode", "Scan start error: " + e.getMessage()); } } } } ``` -------------------------------- ### Serial Communication with Android EMDK Source: https://context7.com/zebradevs/emdk-android-samples/llms.txt Manages serial port operations including opening, configuring, reading, and writing data. It requires the EMDK library and handles available serial ports and their settings. Errors during communication are logged. ```java import android.app.Activity; import android.util.Log; import com.symbol.emdk.EMDKListener; import com.symbol.emdk.EMDKManager; import com.symbol.emdk.EMDKResults; import com.symbol.emdk.serialcomm.SerialComm; import com.symbol.emdk.serialcomm.SerialCommException; import com.symbol.emdk.serialcomm.SerialCommManager; import com.symbol.emdk.serialcomm.SerialCommResults; import com.symbol.emdk.serialcomm.SerialPortInfo; import java.util.HashMap; import java.util.List; public class SerialCommunication extends Activity implements EMDKListener { private SerialCommManager serialCommManager = null; private SerialComm serialPort = null; private HashMap availablePorts = new HashMap<>(); @Override public void onOpened(EMDKManager emdkManager) { // Get SerialCommManager instance serialCommManager = (SerialCommManager) emdkManager.getInstance(EMDKManager.FEATURE_TYPE.SERIALCOMM_EX); if (serialCommManager != null) { // Enumerate available serial ports List portList = serialCommManager.getSupportedPorts(SerialPortInfo.COMM_TYPE.ALL); if (portList != null) { for (SerialPortInfo portInfo : portList) { String portName = portInfo.getFriendlyName(); availablePorts.put(portName, portInfo); Log.d("Serial", "Available port: " + portName); } } } } public boolean openSerialPort(String portName, int baudRate) { SerialPortInfo portInfo = availablePorts.get(portName); if (portInfo == null) { Log.e("Serial", "Port not found: " + portName); return false; } try { // Get SerialComm instance for the port serialPort = serialCommManager.getSerialPort(portInfo); if (serialPort != null) { // Configure port settings serialPort.config.baudRate = SerialComm.BAUD_RATE.valueOf("BAUD_" + baudRate); serialPort.config.dataBits = SerialComm.DATA_BITS.DATA_8; serialPort.config.parity = SerialComm.PARITY.NONE; serialPort.config.stopBits = SerialComm.STOP_BITS.STOP_1; serialPort.config.flowControl = SerialComm.FLOW_CONTROL.NONE; // Enable the port serialPort.enable(); Log.d("Serial", "Port opened: " + portName + " @ " + baudRate + " baud"); return true; } } catch (SerialCommException e) { Log.e("Serial", "Open error: " + SerialCommResults.getErrorDescription(e.getResult())); } return false; } public boolean writeData(String data) { if (serialPort == null || !serialPort.isEnabled()) { Log.e("Serial", "Port not open"); return false; } try { byte[] dataBytes = data.getBytes(); // Write data to serial port SerialCommResults result = serialPort.write(dataBytes, dataBytes.length, 5000); if (result == SerialCommResults.SUCCESS) { Log.d("Serial", "Wrote " + dataBytes.length + " bytes: " + data); return true; } else { Log.e("Serial", "Write failed: " + SerialCommResults.getErrorDescription(result)); } } catch (Exception e) { Log.e("Serial", "Write exception: " + e.getMessage()); } return false; } public String readData(int timeoutMs) { if (serialPort == null || !serialPort.isEnabled()) { Log.e("Serial", "Port not open"); return null; } try { byte[] buffer = new byte[1024]; // Read data from serial port int bytesRead = serialPort.read(buffer, buffer.length, timeoutMs); if (bytesRead > 0) { String data = new String(buffer, 0, bytesRead); Log.d("Serial", "Read " + bytesRead + " bytes: " + data); return data; } else if (bytesRead == 0) { Log.w("Serial", "Read timeout - no data received"); } else { Log.e("Serial", "Read error: " + bytesRead); } } catch (Exception e) { Log.e("Serial", "Read exception: " + e.getMessage()); } return null; } public void closeSerialPort() { if (serialPort != null) { try { serialPort.disable(); serialPort.release(); serialPort = null; Log.d("Serial", "Port closed"); } catch (Exception e) { Log.e("Serial", "Close error: " + e.getMessage()); } } } // Example: Complete read/write operation public void performSerialOperation() { // Open port at 9600 baud if (openSerialPort("Serial Port 1", 9600)) { ``` -------------------------------- ### Detect FeliCa Card using NFCF in Android Source: https://context7.com/zebradevs/emdk-android-samples/llms.txt This code snippet retrieves the NFCF tag from a given Android tag object and checks its manufacturer parameters to determine if it is a FeliCa card. It returns SAMType.FELICA if it's a FeliCa, otherwise SAMType.UNKNOWN. This functionality requires the Android NFC API and Zebra EMDK. ```java NfcF nfcF = NfcF.get(tag); if (nfcF != null) { byte[] pmm = nfcF.getManufacturer(); if ((pmm[0] == 0x01 && pmm[1] == 0x20) || (pmm[0] == 0x03 && pmm[1] == 0x32)) { return SAMType.FELICA; } } return SAMType.UNKNOWN; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.