dawzaw
dawzaw

Reputation: 429

hasSystemFeature method returns wrong value while checking for BluetoothLE support

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import org.jetbrains.anko.toast

class MainActivity : AppCompatActivity() {

    var deviceBluetoothAdapter : BluetoothAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val bluetoothManager : BluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager

        val check = packageManager.hasSystemFeature("FEATURE_BLUETOOTH_LE")

        deviceBluetoothAdapter = bluetoothManager.adapter

        if (check) toast("BLE supported") else toast("BLE not supported")

    }

}

I'm using phone that supports Bluetooth Low Energy but I get wrong toast - "BLE not supported". I check the output of hasSystemFeature for other peripherials like Camera and it also returns false. What am I doing wrong ?

I have proper configs inside the Manifest:

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature android:name="android.hardware.bluetooth.le" android:required="false"/>

Upvotes: 0

Views: 1204

Answers (1)

triad
triad

Reputation: 21507

You should be using:

hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)

https://developer.android.com/reference/android/content/pm/PackageManager.html#FEATURE_BLUETOOTH_LE

An alternate way to check if the device supports bluetooth:

val btAdapter = BluetoothAdapter.getDefaultAdapter() 
val btSupported = btAdapter != null

And to check if it's turned on:

val btEnabled: Boolean = btAdapter?.isEnabled ?: false

Upvotes: 1

Related Questions