Home > Net >  How do you provide "context" in an "Object" or "Singleton" class in An
How do you provide "context" in an "Object" or "Singleton" class in An

Time:02-10

I have this warning:

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException

The thing is when I use what they want me to use like here with the warning:

  if (operation is Connect) {
            with(operation) {
                Timber.w("Connecting to ${device.name} | ${device.address}")
                device.connectGatt(context, false, gattCallback)
                isConnected.value = true
            }
            return
        }

And after I select the "Add Permission Check":

 if (operation is Connect) {
        with(operation) {
            Timber.w("Connecting to ${device.name} | ${device.address}")
            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED
            ) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return
            }
            device.connectGatt(context, false, gattCallback)
            isConnected.value = true
        }
        return
    }

"this" is underlined in red as an error expecting context but found "Connect". What am I supposed to do in this situation?

enter image description here EDIT: This one too enter image description here

This error is in a few parts of the code, they started appearing after I updated Android Studio and Gradle. I'm not gonna lie, I'm not that experienced with Kotlin nor with singleton classes. But the class itself was based on a tutorial that used an "Object" class in the sense of it being instantiated only once, I believe that's what's called a singleton class though I am not sure if I am using it correctly.

CodePudding user response:

You are passing this which is a reference to Connect not Context. Just pass context.

Screenshot

  •  Tags:  
  • Related