0

Hello I am trying to make an application that recognizes nearby Bluetooth devices, my code works fine but it shows the devices it finds repeated.

I tried the contains method, but it doesn't work.

Below is the response I'm getting with duplicate values.

enter image description here

a little help would be very useful since I'm stuck. Thanks

 private static final String TAG = "MainActivity";
 Button scanButton;
 ListView scanListView;
 ArrayList<String> stringArrayList = new ArrayList<String>();
 ArrayAdapter<String> arrayAdapter;
 BluetoothAdapter myAdapter = BluetoothAdapter.getDefaultAdapter();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     scanButton=(Button) findViewById(R.id.scanButton);
     scanListView=(ListView) findViewById(R.id.scanListView);
     ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_SMS}, PackageManager.PERMISSION_GRANTED);

    scanButton.setOnClickListener(new  View.OnClickListener() {
         @RequiresApi(api = Build.VERSION_CODES.M)
         @Override
         public void onClick(View v) {

             arrayAdapter.clear();
             checkBTPermissions();
             myAdapter.startDiscovery();
         }
     });

     IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
     registerReceiver(myReceiver,intentFilter);

     arrayAdapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,stringArrayList);
     scanListView.setAdapter(arrayAdapter);

 }

 @Override
 protected void onDestroy() {
     unregisterReceiver(myReceiver);
     super.onDestroy();

 }    

 BroadcastReceiver myReceiver = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
         int Distance =0;
         String Mac="22:22:4A:91:0F:6C";
         //String Mac = "A0:D6:84:56:8D:65";
         String action = intent.getAction();
         if (BluetoothDevice.ACTION_FOUND.equals(action)){
             BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
             if (stringArrayList.contains(device)){

             }
             else{
                 stringArrayList.add("Name:"+device.getName()+ "\n"+" MAC:"+device.getAddress());
                 Log.d(TAG,"Name:"+device.getName()+ "\n"+" MAC:"+device.getAddress());
             }
         }
     }

 };


Shreyas Sanil
  • 480
  • 1
  • 6
  • 18
  • Hi Camila..You can get unique devises from the stringArrayList while parsing it.Check this solution https://stackoverflow.com/a/13429151/6826629 . – Shreyas Sanil Jan 11 '22 at 17:37

0 Answers0