Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Android with Kotlin, can't get the BluetoothAdapter

I follow the instructions in https://developer.android.com/develop/connectivity/bluetooth/setup , trying to setup Bluetooth. When I am trying to get the BluetoothAdapter,

val bluetoothManager: BluetoothManager? = 
getSystemService(BluetoothManager::class.java)
val bluetoothAdapter: BluetoothAdapter? = bluetoothManager?.getAdapter()
if (bluetoothAdapter == null) {
    // Device doesn't support Bluetooth
}

I get the error

Type mismatch: inferred type is Class<BluetoothManager> but Context was expected

How can I work it around. Anyone?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are using the getSystemService incorrectly

It must be done like this:

val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager

Then you can get the adapter like this:

val adapter = bluetoothManager.adapter
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading