Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:29:47+00:00 2026-06-17T22:29:47+00:00

I have an app, which on button (toggle button) press scans and displays discovered

  • 0

I have an app, which on button (toggle button) press scans and displays discovered bluetooth devices in a custom listview, and on pressing the same button again the scanning is stopped.

Now the problem shows up when I press the button again (2nd time) to start scanning, the same device is shown twice. After I stop and start scanning (3rd time), the same device is shown thrice. And the discovered devices were never paired with my android phone.

There is a similar question, but the answer didn’t help me. Please let me know where I have gone wrong.

Below is the code

        btOnOff.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(btOnOff.isChecked()){

                btAdapter.startDiscovery();
                setProgressBarIndeterminateVisibility(true);

                    bcReceiver = new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            // TODO Auto-generated method stub
                            String action = intent.getAction();
                            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                                deviceName = device.getName();
                                currentDateTime = DateFormat.getDateTimeInstance().format(new Date());

                                Custom data = new Custom(deviceName, currentDateTime);
                                fetch.add(data);

                                lv.setAdapter(cAdapter);

                            }else if(btAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                                btAdapter.startDiscovery();
                            }
                        }
                    };

                    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                    registerReceiver(bcReceiver, filter);
            } else {
                btAdapter.cancelDiscovery();
            }
        }

    });

I have two more classes which does the customization of listview, could there be anything to avoid the duplicate entries. Below is the code of first class file

 public class CustomAdapter extends ArrayAdapter<Custom>{

private ArrayList<Custom> entries;
private Activity activity;

public CustomAdapter(Activity a, int textViewResourceId, ArrayList<Custom> entries) {
    super(a, textViewResourceId, entries);
    // TODO Auto-generated constructor stub
    this.entries = entries;
    this.activity = a;
}

public static class ViewHolder{
    public TextView tv1;
    public TextView tv2;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    ViewHolder holder;
    if(v == null){
        LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.show_devices, null);
        holder = new ViewHolder();
        holder.tv1 = (TextView) v.findViewById(R.id.tv1);
        holder.tv2 = (TextView) v.findViewById(R.id.tv2);

        v.setTag(holder);
    }else{
        holder = (ViewHolder)v.getTag();
    }

    final Custom custom = entries.get(position);
    if(custom != null){
        holder.tv1.setText(custom.getFirst());
        holder.tv2.setText(custom.getSecond());
    }
    return v;
}

}

The second class file

 public class Custom {
private String text1;
private String text2;

public Custom(String string1, String string2){
    this.text1 = string1;
    this.text2 = string2;
}

public String getFirst(){
    return text1;
}

public void setFirst(String text1){
    this.text1 = text1;
}

public String getSecond(){
    return text2;
}

public void setSecond(String text2){
    this.text2 = text2;
}
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:29:48+00:00Added an answer on June 17, 2026 at 10:29 pm

    Customizing the listview with the kind of logic used above has created redundancy and duplicate entries are created. So instead of writing custom listview with two additional classes, we could go for the ArrayAdapter with simple_list_item_1.

    In the Button onclick event ArrayAdapter object would clear the duplicate entries, and the simple_list_item_1 would provide two rows listview similar to the custom listview.

    SImple implementation is show here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app which at current will on the press of a button
I have an app, which have toggle button in action menu item, though i'm
I have a page X in my grails app which has a button to
I have made an iPhone App which has a loginwithFacebook button , the login
I have a small data collection app which has a single button to start
In my c# winform app I have a button which when clicked, it performs
I am working on WPF app in which I have a textbox, Button, Label
I have app which has actionbar containing 1) up button which opens side bar
I currently have an app which, when a button is pressed, and after a
I have a WebServerService which I would like to start when a toggle button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.