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"/>
答案 0 :(得分:0)
您应该使用:
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