Tutorial Fingerprint - Sử dụng cảm biến vân tay



Kể từ phiên bản 6.0, Android đã hỗ trợ thêm chức năng bảo mật bằng vân tay.
Hôm nay mình xin hướng tạo một ứng dụng đơn giản để xem cảm biến vân tay hoạt đông như thế nào!
- Chuẩn bị một máy Android chạy hệ điều hành 6.0 trở lên và đương nhiên có hỗ trợ cảm biến vân tay.
- Nếu không có thiết bị thật thì ta có thể sử dụng máy ảo ủa Android Studio
 + Tạo một máy ảo API 23, mở nó lên.
 + Vào Settings -> Security -> Fingerprint  

Để setup một fingerprint


Ở màn hình này, đương nhiên ta không thể dùng tay chạm lên đó được :D
ta sẽ dùng lệnh để giả lập hành động chạm vào cảm biến.
adb -e emu finger touch 
finger_id là 1 dãy số id, vd: adb -e emu finger touch 123456

Sau khi đó nhận id,  ta chọn Done. Nếu máy chưa cài password thì nó sẽ bắt cài rồi mới setup fingerprint.

Vậy là chúng ta đã chuẩn bị xong máy Android để test ứng dụng.

- Bây giờ chúng ta tạo mới một project
trong file manifest.xml, ta thêm quyền:
<uses-permission android:name="android.permission.USE_FINGERPRINT" />

MainActivity.java
package com.androidtmc.fingerprint; 
 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.app.KeyguardManager; 
import android.hardware.fingerprint.FingerprintManager; 
import android.os.Build; 
import android.os.CancellationSignal; 
import android.security.keystore.KeyGenParameterSpec; 
import android.security.keystore.KeyProperties; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
 
import java.security.Signature; 
 
@SuppressWarnings("ResourceType") 
public class MainActivity extends AppCompatActivity { 
 
 
    private Signature signature; 
    private KeyguardManager keyguardManager; 
    private FingerprintManager mFingerprintManager; 
    private Button btnListen; 
 
    @TargetApi(Build.VERSION_CODES.M) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        //create key 
       signature = CreateKeyHelper.create(this); 
 
        //check has ready set fingerprint on device 
         keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE); 
        mFingerprintManager = (FingerprintManager) getSystemService(Activity.FINGERPRINT_SERVICE); 
        TextView tvMessage  = (TextView) findViewById(R.id.tvMessage); 
        btnListen  = (Button) findViewById(R.id.btnListen); 
        if(!mFingerprintManager.isHardwareDetected()) 
            tvMessage.setText("This device is not support!"); 
        if(!keyguardManager.isKeyguardSecure()){ 
 
            tvMessage.setText("Secure lock screen hasn't set up.\n" + "\" + \"Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint"); 
        } 
 
        else if(!mFingerprintManager.hasEnrolledFingerprints()){ 
           tvMessage.setText("Go to 'Settings -> Security -> Fingerprint' and register at least one fingerprint"); 
        } 
        else{ 
            tvMessage.setText("Please put your finger on device's finger sensor"); 
            listenTouchSenor(); 
        } 
 
        btnListen.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
            listenTouchSenor(); 
            } 
        }); 
 
 
    } 
 
    @TargetApi(Build.VERSION_CODES.M) 
    public void listenTouchSenor(){ 
        //listen action user touch on finger sensor 
        FingerprintManager.CryptoObject cryptObject = new FingerprintManager.CryptoObject(signature); 
        CancellationSignal cancellationSignal = new CancellationSignal(); 
        FingerprintManager fingerprintManager = 
                this.getSystemService(FingerprintManager.class); 
        fingerprintManager.authenticate(cryptObject, cancellationSignal, 0, new FingerprintManager.AuthenticationCallback() { 
            @Override 
            public void onAuthenticationError(int errorCode, CharSequence errString) { 
                super.onAuthenticationError(errorCode, errString); 
                Toast.makeText(MainActivity.this, "Error: "+errString.toString(), Toast.LENGTH_SHORT).show(); 
            } 
 
            @Override 
            public void onAuthenticationHelp(int helpCode, CharSequence helpString) { 
                super.onAuthenticationHelp(helpCode, helpString); 
            } 
 
            @Override 
            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { 
                super.onAuthenticationSucceeded(result); 
                btnListen.setVisibility(View.VISIBLE); 
                Toast.makeText(MainActivity.this, "Success: "+result.toString(), Toast.LENGTH_SHORT).show(); 
            } 
 
            @Override 
            public void onAuthenticationFailed() { 
                super.onAuthenticationFailed(); 
                Toast.makeText(MainActivity.this, "fail ", Toast.LENGTH_SHORT).show(); 
            } 
        }, null); 
    } 
} 
package com.androidtmc.fingerprint; 
 
import android.annotation.TargetApi; 
import android.content.Context; 
import android.os.Build; 
import android.security.KeyPairGeneratorSpec; 
import android.security.keystore.KeyGenParameterSpec; 
import android.security.keystore.KeyProperties; 
 
import java.io.IOException; 
import java.math.BigInteger; 
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.KeyPairGenerator; 
import java.security.KeyStore; 
import java.security.KeyStoreException; 
import java.security.NoSuchAlgorithmException; 
import java.security.NoSuchProviderException; 
import java.security.PrivateKey; 
import java.security.Signature; 
import java.security.UnrecoverableKeyException; 
import java.security.cert.CertificateException; 
import java.security.spec.ECGenParameterSpec; 
import java.util.Calendar; 
 
import javax.security.auth.x500.X500Principal; 
 
/** 
 * Created by cuong on 20/01/2016. 
 */ 
public class CreateKeyHelper { 
 
    private static final String KEY_NAME = "my_key"; 
 
    @TargetApi(Build.VERSION_CODES.M) 
    public static Signature create(Context context){ 
 
        KeyPairGenerator keyPairGenerator = null; 
        try { 
            keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore"); 
        } catch (NoSuchAlgorithmException e) { 
            e.printStackTrace(); 
        } catch (NoSuchProviderException e) { 
            e.printStackTrace(); 
        } 
        try { 
            Calendar start = Calendar.getInstance(); 
            Calendar end = Calendar.getInstance(); 
            end.add(Calendar.YEAR, 1); 
            KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context) 
                    .setAlias(KEY_NAME) 
                    .setSubject(new X500Principal("CN=HealthECard Name, O=Minh Cuong")) 
                    .setSerialNumber(BigInteger.ONE) 
                    .setStartDate(start.getTime()) 
                    .setEndDate(end.getTime()) 
                    .build(); 
            keyPairGenerator.initialize(spec); 
        } catch (InvalidAlgorithmParameterException e) { 
            e.printStackTrace(); 
        } 
        keyPairGenerator.generateKeyPair(); 
 
        Signature signature = null; 
        try { 
            signature = Signature.getInstance("SHA256withRSA");//command for linux: keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android 
 
        } catch (NoSuchAlgorithmException e) { 
            e.printStackTrace(); 
        } 
        KeyStore keyStore = null; 
        try { 
            keyStore = KeyStore.getInstance("AndroidKeyStore"); 
        } catch (KeyStoreException e) { 
            e.printStackTrace(); 
        } 
        try { 
            keyStore.load(null); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } catch (NoSuchAlgorithmException e) { 
            e.printStackTrace(); 
        } catch (CertificateException e) { 
            e.printStackTrace(); 
        } 
        PrivateKey key = null; 
        try { 
            key = (PrivateKey) keyStore.getKey(KEY_NAME, null); 
        } catch (KeyStoreException e) { 
            e.printStackTrace(); 
        } catch (NoSuchAlgorithmException e) { 
            e.printStackTrace(); 
        } catch (UnrecoverableKeyException e) { 
            e.printStackTrace(); 
        } 
        try { 
            signature.initSign(key); 
        } catch (InvalidKeyException e) { 
            e.printStackTrace(); 
        } 
        return signature; 
    } 
} 

Download SourceCode





SHARE

GivesTech

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

14 nhận xét:

  1. Cập nhật link vào nhà cái B52 mới nhất không bị chặn. Hướng dẫn tải app B52 cho điện thoại iOS và Android mượt mà, không giật lag. Khám phá thế giới cá cược casino trực tuyến đỉnh cao.
    Website: https://b52z.org/
    Email: support@b52club.net
    Số điện thoại: 0936 525 252
    Địa chỉ: 236 Lê Thánh Tôn, Phường Bến Thành, Hồ Chí Minh 70000, Vietnam

    Trả lờiXóa
  2. 123bet là lựa chọn hàng đầu cho người yêu thích cá cược thể thao và casino trực tuyến. Hệ thống bảo mật cao, hỗ trợ 24/7 cùng nhiều chương trình khuyến mãi hấp dẫn mỗi tuần.
    Website: https://123bett.io/
    Địa chỉ: 86-549 Đ. Vĩnh Khánh, Khánh Hội, Hồ Chí Minh, Vietnam
    Điện thoại: 0938493847
    Email: 123bettio@gmail.com
    Hashtag: #123bet #123betoi #game123bet #trangchu123bet #link123bet

    Trả lờiXóa
  3. Sunwin mang đến không gian cá cược đẳng cấp quốc tế. Khám phá các sảnh bài live casino casino sống động, tỷ lệ ăn thưởng cao nhất thị trường và dịch vụ hỗ trợ khách hàng chuyên nghiệp 24/7.
    Website: https://sun-win.eu.com/
    Địa chỉ: 1/Đội Cấn/43 343, Tổ 5A, Ngọc Hà, Hà Nội, Vietnam
    Điện thoại: 0937836878
    Email: sunwineucom@gmail.com
    Hashtag: #sunwin #taigamesunwin #sunwineucom #linksunwin #trangchusunwin

    Trả lờiXóa
  4. Hitclub là cổng game bài đổi thưởng đỉnh cao châu Á. Đăng ký tham gia ngay hôm nay để nhận code tân thủ cực giá trị và trải nghiệm kho game slot, tài xỉu xanh chín, bảo mật tuyệt đối.
    Website: https://hitclub1.dev/
    Địa chỉ: 2 Trần Khắc Chân, Tân Định, Hồ Chí Minh, Vietnam
    Điện thoại: 0938938293
    Email: hitclub1dev@gmail.com
    Hashtag: #hitclub #hitclub1dev #taiapphitclub #apphitclub #trangchuhitclub

    Trả lờiXóa
  5. game bài đổi thưởng uy tín 2026. Cập nhật link tải chính thức iOS/Android của 10+ cổng game xanh chín. Hệ thống bảo mật thông tin, rút tiền qua ngân hàng, ví điện tử cực nhanh trong 5 phút. Xem ngay!
    Website: https://gamebaidoithuong88.dev/
    Địa chỉ: Hẻm 263 Thạch Lam, Phú Thạnh, Hồ Chí Minh, Vietnam
    Điện thoại: 0937839489
    Email: gamebaidoithuong88dev@gmail.com
    Hashtag: #gamebaidoithuong #gamebaidoithuong88dev #topgamebaidoithuong #topgamedoithuong

    Trả lờiXóa
  6. go88 khẳng định vị thế cổng game bài đổi thưởng đẳng cấp quốc tế. Hệ thống bảo mật tối tân, nạp rút siêu tốc 1:1 và dịch vụ hỗ trợ khách hàng 24/7 mang lại trải nghiệm giải trí đỉnh cao.
    Website: https://go88bet.in.net/
    Địa chỉ: 30 Tân Sơn Nhì, Hồ Chí Minh, Vietnam
    Điện thoại: 0947 379 830
    Email: go88betinnet@gmail.com
    Hashtag: #go88 #go88betinnet #linktaigo88 #trangchugo88 #taigo88

    Trả lờiXóa
  7. sunwin Play cập nhật link tải app Sunwin cho Android và iOS mới nhất. Sân chơi cá cược đẳng cấp hoàng gia với hệ thống bảo mật tuyệt đối, nạp rút 1:1 nhanh chóng trong 3 phút.
    Website: https://sunwin20.health/
    Địa chỉ: 299 Phan Bội Châu, Bàn Thạch, Đà Nẵng, Việt Nam
    Điện thoại: 0901 031 205
    Email: contact@sunwin20.health
    Hashtag: #sunwin #conggamesunwin #conggamedoithuongsunwin #sunwin20health #sunwin20 #sunwincom #taisunwin

    Trả lờiXóa
  8. Cập nhật link vào Luck8 chính thức mới nhất không bị chặn. Truy cập Luck8 để tham gia cá độ thể thao, trực tuyến mượt mà, tốc độ cao cùng hệ thống nạp rút tiền tự động siêu nhanh trong 2 phút.
    Website: https://luck8.contact/
    Địa chỉ: 26 Đường số 1, Khu dân cư Trung Sơn, Bình Hưng, Hồ Chí Minh, Việt Nam
    Điện thoại: 0985512678
    Email: luck8contact@gmail.com
    Hashtag: #Luck8 #luck8contact #tailuck8

    Trả lờiXóa
  9. 789win cập nhật liên kết truy cập an toàn, mượt mà trên cả điện thoại và máy tính. Khám phá hàng ngàn trò chơi cá cược xanh chín, minh bạch cùng dịch vụ hỗ trợ khách hàng chuyên nghiệp 24/7.
    Website: https://789win-s1.com/
    Địa chỉ: 201 Lê Văn Duyệt, Tân Định, Hồ Chí Minh 700000, Vietnam
    Điện thoại: 0947 893 900
    Email: 789wins1com@gmail.com
    Hashtag: #789win #789wins1com #trangchu789win #conggame789win

    Trả lờiXóa
  10. EE88 cung cấp đường link truy cập chính thức không lo bị chặn. Trải nghiệm sảnh casino live đỉnh cao, cá cược thể thao uy tín và hàng ngàn quà tặng giá trị dành cho thành viên mới của EE88.
    Website: https://ee88wl.com/
    Địa chỉ: 106 Nguyễn An Ninh, TT. Cần Giuộc, Cần Giuộc, Long An, Việt Nam
    Điện thoại: 0986 868 386
    Email: ee88wlcom@gmail.com
    Hashtag: #ee88 #ee_88 #Tai_ee88 #Trang_chu_ee88 #ee88wl

    Trả lờiXóa
  11. Đăng ký Thabet chính thức ngay để nhận code trải nghiệm 100K. Sân chơi cá cược an toàn, bảo mật tuyệt đối với giao diện hiện đại, hỗ trợ người chơi 24/7. Click tham gia Thabet ngay!
    Website: https://thabet.city/
    Địa chỉ: 11 Huỳnh Đình Hai, Phường 14, Bình Thạnh, Tp. Hồ Chí Minh, Việt Nam
    Điện thoại: 0942 675 381
    Email: hotro@thabet.city
    Hashtag: #thabet #nhacaithabet #thabetcasino #trangchuthabet #dangkythabet #thabetvip #thabetonline #linkthabet #thabetuytin #thabetbest

    Trả lờiXóa
  12. HITCLUB cung cấp link tải ứng dụng chính thức cho iOS và Android. Khám phá không gian giải trí đẳng cấp với hệ thống bảo mật hiện đại, giao diện mượt mà cùng dịch vụ hỗ trợ khách hàng chuyên nghiệp 24/7.
    Website: https://hitclubb.actor/
    Địa chỉ: 72/1 D. Vạn Hạnh 3, Bà Điểm, Hồ Chí Minh, Vietnam
    Điện thoại: 0938 802 890
    Email: rikvipnavy@gmail.com
    Hashtag: #hitclub #hitclubactor #trangchuhitclub #hitclub #conggamehitclub #taihitclub

    Trả lờiXóa
  13. Go88 - Link tải app chính thức cập nhật mới nhất, giúp người chơi dễ dàng truy cập và trải nghiệm mượt mà trên mọi thiết bị. Cổng game cam kết bảo vệ thông tin cá nhân tối đa, giao dịch minh bạch 1:1 không qua trung gian. Đăng ký tài khoản ngay hôm nay để nhận trọn bộ quà tặng khởi nghiệp cực giá trị.
    Website: https://go88s.it.com/
    Địa chỉ: 237 Đ. Đặng Nguyên Cẩn, Phường 14, Phú Lâm, Hồ Chí Minh, Việt Nam
    Điện thoại: 0968 663 965
    Email: go88sitcom@gmail.com
    Hashtag: #go88 #go88sitcom #trangchugo88 #conggamego88 #taigamego88

    Trả lờiXóa
  14. Kèo trực tuyến tổng hợp thông tin tỷ lệ đặt cược chi tiết cho từng trận đấu đang diễn ra. Với tốc độ cập nhật dữ liệu nhanh chóng, giao diện thân thiện và dễ tra cứu, đây là nền tảng tin cậy giúp bạn theo dõi biến động bảng kèo và cập nhật tình hình bóng đá một cách chính xác.
    Website: https://keotructuyen.uk.com/
    Địa chỉ: 52 P. Nguyễn Hy Quang, Chợ Dừa, Đống Đa, Hà Nội, Việt Nam
    Điện thoại: 0972 948 004
    Email: keotructuyen.uk.com@gmail.com
    Hashtag: #Keotructuyen #Tylekeo #Keonhacai #Soikeo #TyLeBongDa

    Trả lờiXóa