Try something in this order:
public void displayDetectedDevices(){ mBluetoothAdapter.startDiscovery(); // Create a BroadcastReceiver for ACTION_FOUND mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When discovery finds a device if(BluetoothDevice.ACTION_FOUND.equals(action)){ BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String deviceName = device.getName(); String deviceAddress = device.getAddress(); String s = " "; unpairedDevices.add(deviceName + s + deviceAddress +" \n"); unpairedDevicesList = new ArrayList<String>(unpairedDevices); Toast.makeText(getActivity(), unpairedDevicesList.toString(), Toast.LENGTH_LONG).show(); } } }; }
Remember in onCreate () declare the following:
public void onCreate(Bundle savedInstance){ super.onCreate(savedInstance); unpairedDevicesList = new ArrayList<String>(); unpairedDevices = new HashSet<String>(); }
Remember # 2 declare the following before onCreate ():
ArrayList<String> unpairedDevicesList; Set<String> unpairedDevices;
Final thoughts:
I used Set and arrayList to locate the detected devices. The set will not contain duplicates. Depending on what you want to do, as both definitions are useful, as they provide different functions. Also here is my github page, which contains more information and a complete working bluetooth application.
As the set of devices fills up, the screen will begin to fill up depending on how you want your user interface to work. I used a simple list.
source share