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

时间:2016-11-24 07:38:36

标签: 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

如果有人使用SmartLock,请给我一些指导吗?我没有找到任何SmartLock API来做到这一点。 enter image description here

3 个答案:

答案 0 :(得分:1)

SmartLock没有外部API。您可以查看Google Docs以获取参考资料。

您可以在GitHub上查看this示例,here您将找到有关如何在您的应用中集成智能锁API的教程。

答案 1 :(得分:-1)

可能有点复杂,但谷歌已经提供Docs这个用法。

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

mCredentialsApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .enableAutoManage(this, this)
    .addApi(Auth.CREDENTIALS_API)
    .build();

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

mCredentialRequest = new CredentialRequest.Builder()
    .setPasswordLoginSupported(true)
    .setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.TWITTER)
    .build();

创建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());
            }
        }
    });

在成功的凭据请求中,使用生成的凭据对象完成用户对您应用的登录。使用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);
        // ...
    }
}

答案 2 :(得分:-1)

我认为没有SmartLock API。就像Pravin在评论中所说,智能锁将在设备连接时自动禁用模式。

我没有尝试过这个,但是一旦模式被禁用,您应该可以使用以下(来自this answer)绕过锁定屏幕:

KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

您需要为清单添加权限:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>