12

I am using Android 5.0. The version provides the SmartLock function which allows unlocking the password/pattern by connecting with a trusted device. I have a bluetooth low energy (BLE) device which registered as trusted device. I want to use the BLE to unlock (pattern mode) the phone. It will unlock the phone when the BLE and phone are connected and data is available by the event

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

If anyone who worked with SmartLock, please give me some guidance to do it? I did not find any SmartLock API to do it. enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jame
  • 3,746
  • 6
  • 52
  • 101
  • If BLE device added as trusted device in your smart lock settings then your device will automatically unlock when your phone is connected with that BLE device. What you are looking for is still unclear to me. Please elaborate on this and correct me if I am wrong anywhere. – Pravin Divraniya Dec 07 '16 at 12:09
  • You are right. But it has one more screen (swipe screen). It that time, we must scroll the screen to unlock. I want to ignore this. I think Smartlock has that option – Jame Dec 07 '16 at 12:21
  • Have you checked this? http://stackoverflow.com/questions/30246425/turning-on-screen-from-receiver-service/30365638#30365638 – Pravin Divraniya Dec 08 '16 at 10:27
  • Sorry. It is for swipe mode. I tried it but the code does not work in swipe of smartlock. I think swipe of smartlock has something different with normal swipe mode – Jame Dec 08 '16 at 10:32
  • I am facing some issue in password sync. can you please have a look? https://stackoverflow.com/questions/63830860/app-and-website-saved-password-is-not-sync – Nirmal Sinh Revar Sep 17 '20 at 06:50

3 Answers3

1

There is no external API as such for SmartLock. You can check Google Docs for reference on it.

You can check this sample out on GitHub and here you will find the tutorial on how to integrate smart lock API in your app.

AlphaQ
  • 656
  • 8
  • 18
  • If I trully understand, the smartlock which you give me is different with smartlock in my question. I ran the smartlock in the link before but it is only for email, not for pattern mode – Jame Dec 09 '16 at 02:22
  • @user8430 Well, yes. This tutorial is to give you a basic idea of the Smartlock. Like previous answers, you can use the `KeyguardManager` to unlock your device. – AlphaQ Dec 09 '16 at 09:35
  • I think you did not try it before. Please have an experiment before answer – Jame Dec 09 '16 at 09:43
  • I think smartlock with email is different with smartlock in Android L, such I show in the image. It only has trusted device, trusted face, trusted location options. It has no option for email – Jame Dec 09 '16 at 09:47
  • I am facing some issue in password sync. can you please have a look? https://stackoverflow.com/questions/63830860/app-and-website-saved-password-is-not-sync – Nirmal Sinh Revar Sep 17 '20 at 06:50
-1

It might be a little bit complicated, but Google already huge provided Docs about this usage.

To request stored credentials, you must create an instance of GoogleApiClient configured to access the Credentials API.

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

A CredentialRequest object specifies the sign-in systems from which you want to request credentials. Build a CredentialRequest using the setPasswordLoginSupported method for password-based sign-in, and the setAccountTypes() method for federated sign-in services such as Google Sign-In.

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

After you have created GoogleApiClient and CredentialRequest objects, pass them to the CredentialsApi.request() method to request credentials stored for your app.

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

On a successful credential request, use the resulting Credential object to complete the user's sign-in to your app. Use the getAccountType() method to determine the type of retrieved credentials, then complete the appropriate sign-in process.

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);
        // ...
    }
}
GensaGames
  • 5,538
  • 4
  • 24
  • 53
  • Do you use the feature before? I am confusing your code API (with password) and smartlock which used trusted device – Jame Dec 05 '16 at 11:02
  • I am facing some issue in password sync. can you please have a look? https://stackoverflow.com/questions/63830860/app-and-website-saved-password-is-not-sync – Nirmal Sinh Revar Sep 17 '20 at 06:50
  • @NirmalSinhRevar Spamming your question everywhere is not a good idea. – GensaGames Sep 18 '20 at 08:32
-1

I don't think there is a SmartLock API. Like Pravin said in the comments, smart lock will automatically disable the pattern when the device is connected.

I haven't tried this, but once the pattern is disabled you should be able to bypass the lock screen with the following (from this answer):

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

You will need to add a permission to your manifest:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
Community
  • 1
  • 1
Stephen Tuso
  • 87
  • 1
  • 5