hasSystemFeature方法在检查BluetoothLE支持时返回错误的值

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") } } 

我正在使用支持蓝牙低功耗的手机,但是我得到了错误的敬酒 – “BLE不支持”。 我检查hasSystemFeature的其他外围设备如Camera的输出,它也返回false。 我究竟做错了什么 ?

在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"/> 

你应该使用:

 hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) 

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

检查设备是否支持蓝牙的另一种方法是:

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

并检查是否打开:

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