Cannot find all bluetooth android devices

I try to constantly display bluetooth devices and show them on the screen, but it never shows me all the devices, not just one at a time. I cannot find what I am doing wrong. here is my code, maybe you can find some problems in it. Thanks

class monitorBluetooth extends monitor { private ListView mLvDevices; private ArrayList<String> mDeviceList = new ArrayList<String>(); public monitorBluetooth(service service) { super(service); bluetooth = BluetoothAdapter.getDefaultAdapter(); this.bReceiver = new BluetoothReceiver(); } public void finalize() throws Throwable { super.finalize(); } public void run() { IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); service.registerReceiver(this.bReceiver, filter); if(service != null) { bluetooth = BluetoothAdapter.getDefaultAdapter(); bluetooth.startDiscovery(); } } class BluetoothReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices(); String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); String uuid = intent.getStringExtra(BluetoothDevice.EXTRA_UUID); int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); mDeviceList.add(device.getAddress() + ", " + device.getName()); // get mac address ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList); mLvDevices.setAdapter(adapter); } } } } BluetoothAdapter bluetooth; private BluetoothReceiver bReceiver; 
+5
source share
3 answers

Your code seems to work. I wrote LogCat what you added to mDeviceList, and got several devices.

Perhaps this is how you show the names, or maybe there are no more than one visible BT device?

EDIT: Code Added

 public class MonitorBluetooth extends Thread{ BluetoothAdapter bluetooth; private BluetoothReceiver bReceiver; private Context mContext; public MonitorBluetooth(Context context){ bluetooth=BluetoothAdapter.getDefaultAdapter(); this.bReceiver=new BluetoothReceiver(); this.mContext=context; } public void finalize() throws Throwable{ super.finalize(); } public void run(){ { IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); mContext.registerReceiver(this.bReceiver,filter); } { IntentFilter filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); mContext.registerReceiver(this.bReceiver,filter); } { IntentFilter filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mContext.registerReceiver(this.bReceiver,filter); } bluetooth=BluetoothAdapter.getDefaultAdapter(); bluetooth.startDiscovery(); } class BluetoothReceiver extends BroadcastReceiver{ public void onReceive(Context context,Intent intent){ String action=intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)){ BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d("BluetoothReceiver","found "+device.getAddress()+", "+device.getName()); } else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){ Log.d("BluetoothReceiver","discovery started"); } else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ Log.d("BluetoothReceiver","discovery finished"); context.unregisterReceiver(this); } } } } 

With this code, I get all devices by calling

 MonitorBluetooth monitor=new MonitorBluetooth(this.getApplicationContext()); monitor.start(); 

from Activity.onCreate (Bundle)

+1
source

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.

+1
source

I had the same problem with bluetooth headsets. I had two bluetooth headsets, and both of them were turned on, but they were not detected using blueToothAdapter.startDiscovery() .

Later, I discovered that in the case of bluetooth headsets, you need to long press the β€œStart” button to activate their pairing mode in order to detect Bluetooth detection.

Here's a good example of finding nearby Bluetooth devices from developer.android.com .

0
source

Source: https://habr.com/ru/post/1203857/


All Articles