[英]Android Kotlin: Display Bluetooth Devices on recyclerview but empty
I want to scan a list of bluetooth devices and display it on a recyclerview but it's still empty .我想扫描蓝牙设备列表并将其显示在 recyclerview 上,但它仍然是空的。 It displays nothing on Recyclerview but It works only on Logcat.
它在 Recyclerview 上不显示任何内容,但仅适用于 Logcat。
In onCreate , binding.recyclerBluetooth.adapter = RecyclerBluetoothAdapter(devices_list, this)在onCreate中, binding.recyclerBluetooth.adapter = RecyclerBluetoothAdapter(devices_list, this)
devices_list is null here and yet I used devices_list.add(device) on onReceive , I don't know why... devices_list 在这里是 null 但我在onReceive上使用了 devices_list.add(device) ,我不知道为什么......
Here is my Activity这是我的活动
class BluetoothActivity() : AppCompatActivity(), BluetoothOnItemClickListener {
private lateinit var binding: FragmentBluetoothBinding
private lateinit var manager: RecyclerView.LayoutManager
private val devices_list: ArrayList<BluetoothDevice> = ArrayList()
private var m_bluetoothAdapter: BluetoothAdapter? = null
val REQUEST_ENABLE_BLUETOOTH = 1
private var bluetoothViewModel: BluetoothViewModel = BluetoothViewModel()
private lateinit var bluetoothOnItemClickListener: BluetoothOnItemClickListener
private val mReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (BluetoothDevice.ACTION_FOUND == action) {
// A Bluetooth device was found
// Getting device information from the intent
val device =
intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
if (device != null) {
devices_list.add(device)
Log.e("DEVICE", "${device.name}")
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
BluetoothUtils.grantPermissions(this)
binding = FragmentBluetoothBinding.inflate(layoutInflater)
setContentView(binding.root)
m_bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title = "Bluetooth"
if (!m_bluetoothAdapter!!.isEnabled) {
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH)
val discoverableIntent: Intent =
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE).apply {
putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300)
}
startActivity(discoverableIntent)
} else {
discoverDevices()
}
binding.toggleBluetooth.setOnCheckedChangeListener { _, state ->
bluetoothViewModel.changeBluetoothState(true)
}
manager = LinearLayoutManager(this)
binding.recyclerBluetooth.adapter = RecyclerBluetoothAdapter(devices_list, this)
binding.recyclerBluetooth.layoutManager = manager
}
private fun discoverDevices() {
if (m_bluetoothAdapter!!.isDiscovering) {
// Bluetooth is already in mode discovery mode, we cancel to restart it again
m_bluetoothAdapter!!.cancelDiscovery()
}
val bool = m_bluetoothAdapter?.startDiscovery()
Log.i("", bool.toString())
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
registerReceiver(mReceiver, filter)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
if (resultCode == Activity.RESULT_OK) {
if (m_bluetoothAdapter!!.isEnabled) {
Toast.makeText(this, "Bluetooth enabled", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Bluetooth disabled", Toast.LENGTH_SHORT).show()
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Bluetooth enabling has been canceled", Toast.LENGTH_SHORT)
.show()
}
}
}
My item: class BluetoothDeviceItem(val address: String?, var name: String?, val device: BluetoothDevice?, var selected: Boolean)
我的项目:
class BluetoothDeviceItem(val address: String?, var name: String?, val device: BluetoothDevice?, var selected: Boolean)
my adapter:我的适配器:
class RecyclerBluetoothAdapter(private val devices : ArrayList<BluetoothDevice>, var clickListener: BluetoothOnItemClickListener): RecyclerView.Adapter<RecyclerBluetoothAdapter.ItemViewHolder>(){
inner class ItemViewHolder(val binding: ItemDeviceBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(device : BluetoothDevice,action: BluetoothOnItemClickListener){
binding.tvName.text = device.name
Log.e("RecyclerView", device.name.toString())
itemView.setOnClickListener {
binding.item?.selected= true
notifyItemChanged(adapterPosition)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val inflater = LayoutInflater.from(parent.context)
val adapterlayout = ItemDeviceBinding.inflate(inflater,parent,false)
return ItemViewHolder(adapterlayout)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(devices[position],clickListener)
holder.binding.executePendingBindings()
}
override fun getItemCount(): Int {
return devices.size
}
}
interface BluetoothOnItemClickListener{
// fun OnClickListener(item : DataBluetooth, position: Int
then my layouts然后我的布局
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="item" type="com.example.bbuhf_lecture.ui.adapters.BluetoothDeviceItem"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="10dp"
android:textSize="@dimen/text_size_medium"
android:background="@{item.selected ? @color/color_secondary : @android:color/transparent}"
android:textColor="@{item.selected ? @android:color/white : @android:color/black}"
android:text="@{item.name}"
tools:text="TP200X-TP203079"
/>
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="@dimen/dimen_size_small"
android:alpha="0.15"
android:background="@color/gray"
app:layout_constraintTop_toBottomOf="@+id/tv_name"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>```
Thank you
Inside the BroadcastReceiver() try this:在 BroadcastReceiver() 里面试试这个:
After updating your list:更新列表后:
if (device != null) {
devices_list.add(device)
Log.e("DEVICE", "${device.name}")
if (binding != null) {
binding.recyclerBluetooth.adapter?.notifyDataSetChanged()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.