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(); } }