【问题标题】:How to use smart lock API in my application to unlock pattern mode?如何在我的应用程序中使用智能锁 API 来解锁图案模式?
【发布时间】:2017-04-08 09:48:12
【问题描述】:

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

if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) 
// Calling unlock by the SmartLock API

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

【问题讨论】:

  • 如果在您的智能锁设置中将 BLE 设备添加为受信任设备,那么当您的手机与该 BLE 设备连接时,您的设备将自动解锁。你在找什么我还不清楚。如果我在任何地方错了,请详细说明并纠正我。
  • 你是对的。但它还有一个屏幕(滑动屏幕)。那个时候,我们必须滚动屏幕才能解锁。我想忽略这一点。我认为 Smartlock 有这个选项
  • 你检查过这个吗? stackoverflow.com/questions/30246425/…
  • 对不起。它用于滑动模式。我试过了,但代码在智能锁的滑动中不起作用。我认为智能锁的刷卡与普通刷卡模式有一些不同
  • 我在密码同步方面遇到了一些问题。你能看看吗? stackoverflow.com/questions/63830860/…

标签: android bluetooth bluetooth-lowenergy android-bluetooth google-smartlockpasswords


【解决方案1】:

这可能有点复杂,但 Google 已经大量提供了 Docs 关于这种用法的信息。

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

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

CredentialRequest 对象指定您要从中请求凭据的登录系统。使用setPasswordLoginSupported 方法构建CredentialRequest,用于基于密码的登录,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);
        // ...
    }
}

【讨论】:

  • 你以前用过这个功能吗?我混淆了您的代码 API(带密码)和使用受信任设备的智能锁
  • 我在密码同步方面遇到了一些问题。你能看看吗? stackoverflow.com/questions/63830860/…
  • @NirmalSinhRevar 到处发送垃圾邮件并不是一个好主意。
【解决方案2】:

我认为没有 SmartLock API。就像 Pravin 在 cmets 中所说,智能锁会在设备连接时自动禁用图案。

我还没有尝试过,但是一旦禁用该模式,您应该能够通过以下方式绕过锁定屏幕(来自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"/>

【讨论】:

【解决方案3】:

没有用于 SmartLock 的外部 API。您可以查看谷歌Docs 以供参考。

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

【讨论】:

  • 如果我真的明白,你给我的智能锁与我的问题中的智能锁不同。我之前在链接中运行了智能锁,但它仅适用于电子邮件,不适用于模式模式
  • @user8430 嗯,是的。本教程旨在让您了解 Smartlock 的基本概念。与之前的答案一样,您可以使用KeyguardManager 解锁您的设备。
  • 我想你之前没有尝试过。回答前请做实验
  • 我认为带有电子邮件的智能锁与 Android L 中的智能锁不同,如图所示。它只有受信任的设备、受信任的面孔、受信任的位置选项。它没有电子邮件选项
  • 我在密码同步方面遇到了一些问题。你能看看吗? stackoverflow.com/questions/63830860/…
猜你喜欢
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多