Home > other >  ANDROID STUDIO BLE: How do I add the LeDeviceListAdapter class to my BLE project?
ANDROID STUDIO BLE: How do I add the LeDeviceListAdapter class to my BLE project?

Time:06-21

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!

CodePudding user response:

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 :D

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 :D

Also check this answer:

Answer about LeDeviceListAdapter

CodePudding user response:

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 your activity/fragment.

This adapter is using BaseAdapter class.

// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;
    private LayoutInflater mInflator;

    public LeDeviceListAdapter() {
        super();
        mLeDevices = new ArrayList<BluetoothDevice>();
        mInflator = DeviceScanActivity.this.getLayoutInflater();
    }

    public void addDevice(BluetoothDevice device) {
        if(!mLeDevices.contains(device)) {
            mLeDevices.add(device);
        }
    }

    public BluetoothDevice getDevice(int position) {
        return mLeDevices.get(position);
    }

    public void clear() {
        mLeDevices.clear();
    }

    @Override
    public int getCount() {
        return mLeDevices.size();
    }

    @Override
    public Object getItem(int i) {
        return mLeDevices.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);
        final String deviceName = device.getName();
        if (deviceName != null && deviceName.length() > 0)
            viewHolder.deviceName.setText(deviceName);
        else
            viewHolder.deviceName.setText(R.string.unknown_device);
        viewHolder.deviceAddress.setText(device.getAddress());

        return view;
    }
}
  • Related