接入指纹框架:https://github.com/Tencent/soter
implementation 'com.github.Tencent.soter:soter-wrapper:2.0.9'
1.Application中初始化
class IApplication : Application() {
override fun onCreate() {
super.onCreate()
instance = this
initSort()
}
private fun initSort() {
val param = InitializeParamBuilder()
.setScenes(100)
.build()
SoterWrapperApi.init(this, { result ->
logDebug("$result")
}, param)
}
}
2.准备密钥
SoterWrapperApi.prepareAuthKey(
{ result ->
logDebug("init sort = $result")
if (result.errCode == SoterErrCode.ERR_OK) {
//支持指纹
binding.llFingerPay.visibility = View.VISIBLE
} else {
binding.llFingerPay.visibility = View.GONE
}
}, false, true, 100, null, null
)
也可以通过准备密钥结果来判断是否设备是否支持指纹
3.调起系统指纹进行验证
3.1 请求验证前获取AuthenticationParam
private var canceller: SoterBiometricCanceller? = null
fun getAuthParam(act: AppCompatActivity?, showPassword: Boolean = true): AuthenticationParam {
canceller = SoterBiometricCanceller()
canceller!!.refreshCancellationSignal()
val param = AuthenticationParam.AuthenticationParamBuilder()
.setScene(100)
.setContext(act)
.setBiometricType(ConstantsSoter.FINGERPRINT_AUTH)
.setSoterBiometricCanceller(canceller)
//用于获取挑战因子的网络封装结构体。如果在授权之前已经通过其他模块拿到后台挑战因子,则可以改为调用setPrefilledChallenge
.setPrefilledChallenge("test1")
.setSoterBiometricStateCallback(object : SoterBiometricStateCallback {
override fun onStartAuthentication() {
//开始验证
}
override fun onAuthenticationHelp(
helpCode: Int,
helpString: CharSequence?
) {
}
override fun onAuthenticationSucceed() {
//验证成功
}
override fun onAuthenticationFailed() {
//指纹错误
}
override fun onAuthenticationCancelled() {
//用户主动取消,可能会切换密码
}
override fun onAuthenticationError(
errorCode: Int,
errorString: CharSequence?
) {
//验证异常
}
}).build()
return param
}
3.2 请求验证
SoterWrapperApi.requestAuthorizeAndSign({ result ->
if (result.isSuccess) {
//每个手指的指纹标识
val fid = result.extData.fid
//指纹验证成功
} else {
//指纹不匹配或其他错误
showFingerError(result)
}
}, authParam)
多个指纹,可以使用fid进行判断对比。
最后权限:
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>