Search code examples
tapkey

TapKey Mobile SDK can't find a lock


I am developing a app in flutter and I am having a problem with detecting a locks via tapkey mobile sdk. I am login users with Token Exchange method, I have created new Identity Providers. I am creating a new user via a cloud function (Owners/{ownerAccountId}/IdentityProviders/{ipId}/Users however i'm not adding the contact) using the Client Credentials that i have also added to my.tapkey.com as new user and assigned a lock to it.

I can successfully run logInAsync with the token i am receiving via Token Exchange however when i try to find a nearby locks i got {} as a response (i will mention that the lock is next to me).

my code:

import android.Manifest
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import com.tapkey.mobile.TapkeyAppContext
import com.tapkey.mobile.TapkeyEnvironmentConfigBuilder
import com.tapkey.mobile.TapkeyServiceFactory
import com.tapkey.mobile.TapkeyServiceFactoryBuilder
import com.tapkey.mobile.ble.BleLockScanner
import com.tapkey.mobile.concurrent.CancellationToken
import com.tapkey.mobile.concurrent.CancellationTokenSource
import com.tapkey.mobile.manager.UserManager
import io.flutter.app.FlutterApplication
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
import net.tpky.mc.time.ServerClock
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL



  class TapKeyTest : FlutterApplication(), TapkeyAppContext {

     private lateinit var tapkeyServiceFactory: TapkeyServiceFactory

      lateinit var flutterEngine: FlutterEngine


      companion object {
          const val FLUTTER_ENGINE_NAME = "nps_flutter_engine_name"
      }

    override fun onCreate() {
         super.onCreate()

        flutterEngine = FlutterEngine(this)

        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )

        FlutterEngineCache
            .getInstance()
            .put(FLUTTER_ENGINE_NAME, flutterEngine)

        val serverClock = ServerClock()
        val config = TapkeyEnvironmentConfigBuilder()
        config.setBaseUri("https://my.tapkey.com/")

        val b = TapkeyServiceFactoryBuilder(this    )

            .setServerClock(serverClock)
            .setConfig(config.build())

        val sf = b.build()
        tapkeyServiceFactory = sf


     }



      override fun getTapkeyServiceFactory(): TapkeyServiceFactory {
         return tapkeyServiceFactory
     }



     fun login(SECRET_TOKEN: String) {
        val src = CancellationTokenSource()
        val ct: CancellationToken = src.token
        val userManager: UserManager = tapkeyServiceFactory.userManager
        userManager.logInAsync(SECRET_TOKEN, ct)
            .continueOnUi { userId -> scanLocks()}
            .catchOnUi { asyncError -> println(asyncError.cause) } }

    private fun scanLocks( ) {


 if (ActivityCompat.checkSelfPermission(
           this,
           Manifest.permission.BLUETOOTH_SCAN
       ) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
           this,
           Manifest.permission.BLUETOOTH_CONNECT
       ) != PackageManager.PERMISSION_GRANTED
   ) {

     return
   } else {

     tapkeyServiceFactory.bleLockScanner.locksChangedObservable
         .addObserver { locks -> println(locks)}

     println( tapkeyServiceFactory.bleLockScanner.locks)
         println("Permission granted")
   }

    }
  }

Is there a step that i have missed? Also i can't find anywhere new users that i am creating.


Solution

  • It seems, that you don't start the scanning:

    BleLockScanner scanner = tapkeyServiceFactory.getBleLockScanner();
    bleScanObserverRegistration = scanner.startForegroundScan();