【发布时间】:2022-11-12 09:03:43
【问题描述】:
我想开发一个与 BLE 设备通信的应用程序。 我没有使用 Android 开发任何类型的蓝牙应用程序的经验。 如果应用程序的任何示例源代码可以扫描连接并读取某些特征。
很难获得有效的源代码
【问题讨论】:
我想开发一个与 BLE 设备通信的应用程序。 我没有使用 Android 开发任何类型的蓝牙应用程序的经验。 如果应用程序的任何示例源代码可以扫描连接并读取某些特征。
很难获得有效的源代码
【问题讨论】:
我建议使用 Kotlin,您可以完全按照您在 Kotlin 中的要求进行操作。
以下是扫描蓝牙设备的示例:
fun scanForBluetoothDevices() {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
if (!bluetoothAdapter.isEnabled) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
val pairedDevices: Set<BluetoothDevice>? = bluetoothAdapter.bondedDevices
// If there are paired devices
if (pairedDevices?.isNotEmpty() == true) {
// Loop through paired devices
pairedDevices.forEach { device ->
// Add the name and address to an array adapter to show in a ListView
Log.d("Bluetooth", "Device: ${device.name}, ${device.address}")
}
}
}
【讨论】: