MSRX Flutter
A Flutter plugin for MSR605X magnetic stripe card reader. This plugin supports Windows, Android, and Linux platforms.
Features
- Connect to MSR605X magnetic stripe card reader
- Read data from magnetic stripe cards
- Write data to magnetic stripe cards
- Monitor device connection status
- Cross-platform support (Windows, Android, Linux)
Installation
Add this package to your Flutter project by adding the following to your pubspec.yaml
file:
dependencies:
msrx_flutter: ^0.1.0
Then run:
flutter pub get
Platform-specific setup
Android
Add USB permissions to your AndroidManifest.xml
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name">
<!-- USB permission -->
<uses-feature android:name="android.hardware.usb.host" />
<application
android:label="Your App Name"
android:icon="@mipmap/ic_launcher">
<!-- USB device filter for MSR605X -->
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</application>
</manifest>
Create a file android/app/src/main/res/xml/device_filter.xml
with the following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- MSR605X device -->
<usb-device vendor-id="2049" product-id="3" />
</resources>
Windows
Make sure you have the HIDAPI library installed. The plugin will automatically link against it.
Linux
Install the required dependencies:
sudo apt-get install libhidapi-dev libhidapi-libusb0
Usage
Basic usage
import 'package:msrx_flutter/msrx.dart';
import 'package:msrx_flutter/track_data.dart';
void main() async {
// Create an instance of the MSRX plugin
final msrx = MSRX();
// Connect to the device
try {
bool connected = await msrx.connect();
print('Connected: $connected');
} catch (e) {
print('Connection error: $e');
}
// Read card data
try {
TrackData data = await msrx.readCard(timeout: 5000);
print('Track 1: ${data.track1}');
print('Track 2: ${data.track2}');
print('Track 3: ${data.track3}');
} catch (e) {
print('Read error: $e');
}
// Write card data
try {
TrackData dataToWrite = TrackData(
track1: '%B4111111111111111^SMITH/JOHN^2511?',
track2: '4111111111111111=2511?',
track3: null,
);
bool success = await msrx.writeCard(dataToWrite);
print('Write success: $success');
} catch (e) {
print('Write error: $e');
}
// Disconnect from the device
await msrx.disconnect();
}
Monitoring connection status
import 'package:msrx_flutter/msrx.dart';
void main() {
final msrx = MSRX();
// Listen for connection status changes
msrx.connectionStatus.listen((connected) {
print('Device connected: $connected');
if (!connected) {
// Handle disconnection
}
});
// Connect to the device
msrx.connect();
}
Track Data Format
The TrackData
class represents the data stored on a magnetic stripe card:
track1
: Contains the cardholder's name and account numbertrack2
: Contains the account number and expiration datetrack3
: Additional data (optional)
Track 1 Format
Track 1 typically follows this format:
%B[Account Number]^[Name]^[Expiration Date][Service Code]?
Example:
%B4111111111111111^SMITH/JOHN^2511?
Track 2 Format
Track 2 typically follows this format:
[Account Number]=[Expiration Date][Service Code]?
Example:
4111111111111111=2511?
Error Handling
The plugin throws exceptions for various error conditions:
- Connection errors
- Read timeouts
- Write errors
- Device not connected errors
Always wrap calls in try-catch blocks to handle these exceptions.
Troubleshooting
Device not found
- Make sure the MSR605X is properly connected to your device
- Check that you have the correct USB permissions (Android)
- Verify that the device is powered on
Read/Write errors
- Make sure the card is swiped correctly
- Check if the card is damaged
- Verify that the card is compatible with the MSR605X
License
This project is licensed under the MIT License - see the LICENSE file for details.