I am new to Android Studio and to BLE. I am working on a program that will scan for a beacon to read information. I am following this tutorial and the closest solution to my problem I found so far was this post. I am trying to get my app to respond to a button press by showing a list of devices that are available to connect through BLE. I keep running into this error from the LeDeviceListAdapter variable initialization…
Unresolved Reference: LeDeviceListAdapter
I figure that I need to add a class to my code, which I have tried to do directly from this source, but I was unsuccessful.
My question is, how do I add the class to my project if that’s the solution and which file in the solution do I add it to? If that’s not the solution, can someone help me pinpoint what it is?
Also, could it have something to do with the Android API I’m using for the project?
Here is my code…
view.findViewById<Button>(R.id.button_first).setOnClickListener {
val bluetoothManager: BluetoothManager = activity!!.getSystemService(BluetoothManager::class.java)
val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
val text = "This device does not support BlueTooth"
val duration = Toast.LENGTH_SHORT
Toast.makeText(context, text, duration)
return@setOnClickListener
}
if (bluetoothAdapter?.isEnabled == false) {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
val bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
var scanning = false
val handler = Handler()
// Stops scanning after 10 seconds.
val SCAN_PERIOD: Long = 10000
val leDeviceListAdapter = LeDeviceListAdapter()
// Device scan callback.
val leScanCallback: ScanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
super.onScanResult(callbackType, result)
leDeviceListAdapter.addDevice(result.device)
leDeviceListAdapter.notifyDataSetChanged()
}
}
fun scanLeDevice() {
if (!scanning) { // Stops scanning after a pre-defined scan period.
handler.postDelayed({
scanning = false
bluetoothLeScanner.stopScan(leScanCallback)
}, SCAN_PERIOD)
scanning = true
bluetoothLeScanner.startScan(leScanCallback)
} else {
scanning = false
bluetoothLeScanner.stopScan(leScanCallback)
}
}
}
}
Thank you in advance!
2
Answers
I don’t know if I understood correctly the question, but you need to create a new class file with the LeDeviceListAdapter class definition. Then you need to import it inside the file you are using it.
For some info about BLE you can check my question:
QUESTION
And my answer:
ANSWER
In my answer you can find a class (TagBLE and DataTagBLE) to manage the bluetooth device you find ^^ You can use it and implement a list of them to manage the results from your scan then use a RecyclerView with specific RecyclerView Adapter to show all the devices 😀
If you need any help just ask me, I can provice you the code I’m using and explain it to you (: For example I can give you the services and activities I’m using to scan and retrieve BLE 😀
Also check this answer:
Answer about LeDeviceListAdapter
I had the code for a service that do the background scan for the BLE and a Base Activity that manage getting the TAG INFO. You can use this for references or import it in your project. It will use a service to scan for the BLE and get the results in the activity
BaseTagsScanService.java:
TagsScanService.java:
BaseTagsScanActivity.java:
Rest of code you have the classes in my answer above and the message handlers in this answer:
Message Handlers between service and activity
Looks like you haven’t implemented correctly,
You first need to create a new class file with the name
"LeDeviceListAdapter"
in the same package.Once you created this class, you need to
import
this class in youractivity/fragment
.This adapter is using BaseAdapter class.
Layout File listitem_device.xml
ViewHolder class inside the Adapter class file