The search works, but it works every other time, it does not find all the devices, the search may not start at all. ACTION_DISCOVERY_STARTED event ACTION_DISCOVERY_STARTED not always occur, I can not understand what is the matter. Help. Here is the code

 public class Find extends AppCompatActivity { private ListView myListDevices; private static final int ID_RESULT = 1; private ArrayAdapter<String> BTArrayAd; private ArrayList<BluetoothDevice> mDeviceList = new ArrayList<BluetoothDevice>(); private BluetoothAdapter myBluetoothAdapter; private boolean choice_pir = false; final String LOG_TAG = "myLogs"; private Parcelable[] uuidExtra = null; private LinkedHashSet<Parcelable> uuidsExtraFindDevices = new LinkedHashSet<>(); private ProgressDialog mProgressDlg; private final BroadcastReceiver bReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch (action) { case BluetoothAdapter.ACTION_DISCOVERY_STARTED: { mProgressDlg.show(); } break; // When discovery finds a device case BluetoothDevice.ACTION_FOUND: { Log.d(LOG_TAG, "Устройство Π½Π°ΠΉΠ΄Π΅Π½ΠΎ"); // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // add the name and the MAC address of the object to the arrayAdapter mDeviceList.add(device); BTArrayAd.add(device.getName() + "\n" + device.getAddress()); //BTArrayAd.notifyDataSetChanged(); } break; case BluetoothAdapter.ACTION_DISCOVERY_FINISHED: { mProgressDlg.dismiss(); Log.d(LOG_TAG, "Поиск Π·Π°ΠΊΠΎΠ½Ρ‡Π΅Π½"); // discovery has finished, give a call to fetchUuidsWithSdp on first device in list. if (!mDeviceList.isEmpty()) { BluetoothDevice device = mDeviceList.remove(0); boolean result = device.fetchUuidsWithSdp(); } } break; case BluetoothDevice.ACTION_UUID: { Log.d(LOG_TAG, "Вставка UUID"); // This is when we can be assured that fetchUuidsWithSdp has completed. // So get the uuids and call fetchUuidsWithSdp on another device in list BluetoothDevice deviceExtra = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID); Log.d(LOG_TAG, "DeviceExtra address - " + deviceExtra.getAddress()); if (uuidExtra != null) { for (Parcelable p : uuidExtra) { Log.d(LOG_TAG, "uuidExtra - " + p); } uuidsExtraFindDevices.add(uuidExtra[uuidExtra.length - 1]); } else { Log.d(LOG_TAG, "uuidExtra is still null"); } if (!mDeviceList.isEmpty()) { BluetoothDevice device = mDeviceList.remove(0); boolean result = device.fetchUuidsWithSdp(); } } break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find); myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mProgressDlg = new ProgressDialog(this); mProgressDlg.setMessage("Scanning..."); mProgressDlg.setCancelable(false); mProgressDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); myBluetoothAdapter.cancelDiscovery(); } }); myListDevices = (ListView) findViewById(R.id.listFind); BTArrayAd = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); BTArrayAd.setNotifyOnChange(true); myListDevices.setAdapter(BTArrayAd); // ΠΎΡ‚ΠΌΠ΅Π½Π° сканирования BTArrayAd.clear(); IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothDevice.ACTION_UUID); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); registerReceiver(bReceiver, filter); myBluetoothAdapter.startDiscovery(); Log.d(LOG_TAG,"Поиск Π·Π°ΠΏΡƒΡ‰Π΅Π½"); // ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° события Π½Π° ΠΊΠ»ΠΈΠΊ ΠΏΠΎ элСмСнту списка myListDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(); String address = String.valueOf(parent.getAdapter().getItem(position)); int num = address.indexOf('\n'); address = address.substring(num + 1); String [] data = new String[2]; data[0] = address; Iterator<Parcelable> itr = uuidsExtraFindDevices.iterator(); int count = 0; while (itr.hasNext()) { if(count == position) data[1] = itr.next().toString(); count++; } intent.putExtra("find",data); if (choice_pir) setResult(RESULT_CANCELED, intent); else setResult(RESULT_OK, intent); finish(); } }); } @Override public void onStart() { Log.d(LOG_TAG,"Активити Π²ΠΈΠ΄Π½ΠΎ"); super.onStart(); } @Override public void onResume() { Log.d(LOG_TAG,"Активити ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ фокус"); super.onResume(); } @Override public void onPause() { Log.d(LOG_TAG,"Активити Π² ΠΏΠ°ΡƒΠ·Π΅"); super.onPause(); } @Override public void onStop() { Log.d(LOG_TAG,"Активити остановлСно"); super.onStop(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_CANCELED) choice_pir = true; } @Override protected void onDestroy() { unregisterReceiver(bReceiver); Log.d(LOG_TAG,"Активити ΡƒΠ½ΠΈΡ‡Ρ‚ΠΎΠΆΠ΅Π½ΠΎ"); super.onDestroy(); } } 
  • depends on the specific device. Tried on different? - Vladyslav Matviienko
  • No, at the moment HTC One. Well, I'll try. Android version 4.4.2. Well, I would like it to work here as well))) but in general, ideally, it is necessary that it works on all versions of the android above API 19 - Oleg Martynov
  • Bluetooth in Android is a solid trash. Each manufacturer has its own implementation. In addition, it is full of different radio modules, and even the Bluetooth standard itself is not finalized. I have been doing this rubbish for 2 years already, and at least once a month there is such an android device, on which everything is implemented in a new way, and does not work as it should. In the hands of the HTC One Mini, and he is just terrible in terms of Bluetooth. - Vladyslav Matviienko
  • From personal experience, I can say that Samsung flagship models work more or less according to the standard, Nexus-line, Sony is also not bad. - Vladyslav Matviienko
  • Tin) And what can you do to make the norms work at least on this device?) I just saw a slightly different implementation, everywhere I have something similar like that. Well, I would not want to redo everything, especially since I am not as experienced as if you touch something so that you will have to redo a whole lot. Just now it works terribly, it rarely finds all devices, it happens bypassing the ACTION_DISCOVERY_STARTED event, ACTION_FOUND and ACTION_UUID immediately. - Oleg Martynov

1 answer 1

For example, you could not finish the scan last time, and register the receiver, and start the scan, which is already started. In this case, ACTION_DISCOVERY_STARTED will not. If you ACTION_DISCOVERY_STARTED need ACTION_DISCOVERY_STARTED , then before you start scanning, check whether it is coming now, and if it is already underway - stop, register the receiver, and only then start again.

For devices that are not:

You can try to scan by intervals, for example for 3 seconds, and add the devices found to the general list. It is difficult to explain, but in my practice for 2 sets of 3 seconds, the probability of finding a device is much more than one set of 6 seconds.

  • And if so try when found the device, cancel the scan and run it again? And is it necessary to do this unregisterReceiver (bReceiver); - Oleg Martynov
  • @ Oleg Martynov, well, try it. It can be done in such a way that it is not necessary unregisterReceiver - Vladyslav Matviienko
  • Damn, I do not understand how to organize such a search for three seconds, it is how to set a timer and cancel scanning every three seconds and then turn it on? The fact is that in this case the ACTION_DISCOVERY_STARTED event does not occur, I tried a little differently, well, it seems. And here if (! MyBluetoothAdapter.isDiscovering ()) {registerReceiver (bReceiver, filter); myBluetoothAdapter.startDiscovery (); } can cancel the scan and restart if it is already underway. - Oleg Martynov