У меня есть SettingsActivity
, позволяющий пользователю выбирать из списка сопряженных устройств Bluetooth.
Тем не менее, кажется, что с Bluetooth Low Energy происходит что-то странное, когда устройства не соединяются нормально: устройство, к которому я пытаюсь подключиться, не будет соединяться ни с одним из моих устройств Android, и я замечаю, что мой Fitbit не работает. Это список сопряженных устройств на моем телефоне, хотя кажется, что он работает. ВТФ?
В любом случае, вопрос: как мне добавить в список соответствующих устройств список устройств BLE?
(Я просмотрел https://developer.android.com/guide/topics/connectivity/bluetooth-le#find, но это просто разрозненные фрагменты необъяснимого кода; в нем не сказано, куда их поместить, как их вызвать или как они сочетаются друг с другом, и если я скопирую его потом вылезает куча ошибок)
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// get bluetooth devices
var btDevices: Array<CharSequence> = arrayOf("")
try {
val bt: BluetoothManager =
getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager;
val bta: BluetoothAdapter = bt.adapter;
// Get normal Bluetooth devices.
val pairedDevices: Set<BluetoothDevice> = bta.bondedDevices
// Get Bluetooth Low Energy devices.
// HOW?!
btDevices = pairedDevices.map { z -> z.name }.toTypedArray()
}
catch(e:Exception) {}
// Start the fragment
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment(cs))
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
// Slap the toolbar.
val toolbar = findViewById<Toolbar>(R.id.settings_toolbar) // Must be after setContentView or else it returns null.
setSupportActionBar(toolbar)
toolbar.setNavigationOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
finish()
}
})
}
class SettingsFragment(adapters: Array<CharSequence>) : PreferenceFragmentCompat() {
private var adapters: Array<CharSequence> = adapters
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
val p:ListPreference? = findPreference<ListPreference>("bluetoothName")
p?.setEntries(adapters)
p?.setEntryValues(adapters)
}
}
}
bta.bluetoothLeScanner.startScan(bleScanCallback)
`// ждем сканирования` `//добавляем имена устройств в список`, гдеbleScanCallback : ScanCallback
- person Richard Barraclough   schedule 16.06.2020