如何在我的应用程序中使用智能锁API来解锁模式模式?

Jam*_*ame 12 android bluetooth bluetooth-lowenergy android-bluetooth google-smartlockpasswords

我使用的是Android 5.0.该版本提供SmartLock功能,允许通过连接可信设备来解锁密码/模式.我有一个蓝牙低功耗(BLE)设备,注册为可信设备.我想用BLE解锁(模式模式)手机.当BLE和手机连接并且事件可用数据时,它将解锁手机

if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) 
// Calling unlock by the SmartLock API
Run Code Online (Sandbox Code Playgroud)

如果有人使用SmartLock,请给我一些指导吗?我没有找到任何SmartLock API来做到这一点. 在此输入图像描述

Gen*_*mes -1

它可能有点复杂,但谷歌已经提供了大量有关此用法的文档

要请求存储的凭据,您必须创建一个GoogleApiClient配置为访问凭据 API 的实例。

mCredentialsApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .enableAutoManage(this, this)
    .addApi(Auth.CREDENTIALS_API)
    .build();
Run Code Online (Sandbox Code Playgroud)

CredentialRequest 对象指定您要从中请求凭据的登录系统。CredentialRequest使用setPasswordLoginSupported基于密码的登录方法和setAccountTypes()联合登录服务(例如 Google Sign-In)构建方法。

mCredentialRequest = new CredentialRequest.Builder()
    .setPasswordLoginSupported(true)
    .setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.TWITTER)
    .build();
Run Code Online (Sandbox Code Playgroud)

创建GoogleApiClientCredentialRequest对象后,将它们传递给CredentialsApi.request()方法以请求为您的应用程序存储的凭据。

Auth.CredentialsApi.request(mCredentialsClient, mCredentialRequest).setResultCallback(
    new ResultCallback<CredentialRequestResult>() {
        @Override
        public void onResult(CredentialRequestResult credentialRequestResult) {
            if (credentialRequestResult.getStatus().isSuccess()) {
                // See "Handle successful credential requests"
                onCredentialRetrieved(credentialRequestResult.getCredential());
            } else {
                // See "Handle unsuccessful and incomplete credential requests"
                resolveResult(credentialRequestResult.getStatus());
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

凭据请求成功后,使用生成的 Credential 对象完成用户登录您的应用程序。使用该getAccountType()方法确定检索到的凭据的类型,然后完成相应的登录过程。

private void onCredentialRetrieved(Credential credential) {
    String accountType = credential.getAccountType();
    if (accountType == null) {
        // Sign the user in with information from the Credential.
        signInWithPassword(credential.getId(), credential.getPassword());
    } else if (accountType.equals(IdentityProviders.GOOGLE)) {
        // The user has previously signed in with Google Sign-In. Silently
        // sign in the user with the same ID.
        // See https://developers.google.com/identity/sign-in/android/
        GoogleSignInOptions gso =
                new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestEmail()
                        .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .setAccountName(credential.getId())
                .build();
        OptionalPendingResult<GoogleSignInResult> opr =
                Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)