Android中使用STARTEK FM220外接指纹设备存储指纹至SQLite的技术问询
Hey there! Let's work through getting your STARTEK FM220 fingerprint data stored in SQLite—you’ve already got the name and email parts sorted, so we just need to tackle the fingerprint storage piece step by step.
First, you’ll need to use STARTEK’s official Android SDK to interact with the FM220. The key here is to capture a fingerprint and generate a binary template (this is what we’ll store, not raw image data—templates are smaller and designed for matching).
Here’s a simplified code snippet based on typical STARTEK SDK patterns (adjust method names to match your SDK version’s docs):
// Initialize and connect to the FM220 device STFingerprint fingerprintDevice = new STFingerprint(getApplicationContext()); boolean isDeviceConnected = fingerprintDevice.connect(); if (isDeviceConnected) { try { // Capture fingerprint and generate a reusable template byte[] fingerprintTemplate = fingerprintDevice.captureAndGenerateTemplate(); if (fingerprintTemplate != null) { // Pass the template to your SQLite save method saveUserWithFingerprint("John Doe", "john@example.com", fingerprintTemplate); } else { // Show error to user: Fingerprint capture failed Toast.makeText(this, "Failed to capture fingerprint. Try again.", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "Device error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } finally { // Always disconnect to avoid resource leaks fingerprintDevice.disconnect(); } } else { Toast.makeText(this, "Could not connect to fingerprint device.", Toast.LENGTH_SHORT).show(); }
Pro tip: Double-check the SDK docs for exact method names—some versions might use getFingerprintTemplate() instead of captureAndGenerateTemplate().
Fingerprint templates are binary data, so we’ll use SQLite’s BLOB type to store them. Update your users table to include this column:
CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, fingerprint_template BLOB NOT NULL );
This BLOB column will safely store the raw byte array from the FM220 template without encoding issues.
Now, write the code to insert the user’s details along with the fingerprint template into your database. Here’s how to do it with Android’s native SQLiteOpenHelper:
private void saveUserWithFingerprint(String name, String email, byte[] fingerprintTemplate) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", name); values.put("email", email); values.put("fingerprint_template", fingerprintTemplate); // Insert the row into the database long newUserId = db.insert("users", null, values); if (newUserId == -1) { Log.e("FingerprintStorage", "Failed to save user with fingerprint"); Toast.makeText(this, "Save failed. Try again.", Toast.LENGTH_SHORT).show(); } else { Log.d("FingerprintStorage", "User saved successfully. ID: " + newUserId); Toast.makeText(this, "Fingerprint saved!", Toast.LENGTH_SHORT).show(); } db.close(); }
If you’re using Jetpack Room (recommended for modern Android apps), define your User entity like this:
@Entity(tableName = "users") data class User( @PrimaryKey(autoGenerate = true) val id: Int = 0, val name: String, val email: String, @ColumnInfo(typeAffinity = ColumnInfo.BLOB) val fingerprintTemplate: ByteArray )
Then add an insert method to your DAO:
@Dao interface UserDao { @Insert suspend fun insertUser(user: User): Long }
- Always Disconnect the Device: Failing to call
disconnect()after use can leave the device locked up for future sessions. Add this to your activity’sonDestroy()method. - Validate Templates Before Storage: Before saving, use the SDK’s built-in verification method to ensure the template is valid (e.g.,
fingerprintDevice.isTemplateValid(fingerprintTemplate)). - Handle Runtime Permissions: Make sure your app has the necessary permissions to access external USB devices (check the SDK docs for required permissions like
android.permission.USB_PERMISSION).
内容的提问来源于stack exchange,提问作者Shubham Tiwari

