How to find devices in range using bluetooth?

I am new to android. I want to develop an application to search for devices in a range using Bluetooth software. If anyone has an idea, please give me a code example.

+6
source share
3 answers

Find The Devices in the Range by using Bluetooth programmatically.

Yes, you can do it using BroadcastReceiver , see below code, this will help you.

Initial search

 mBluetoothAdapter.startDiscovery(); mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); 

Device search

  if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Add the name and address to an array adapter to show in a ListView mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } }; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, filter); 
+7
source

Create the following broadcast receiver and add device information

  private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); ArrayList<HashMap<String, String> arl = new ArrayList<HashMap<String, String>(); // When discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // If it already paired, skip it, because it been listed already HashMap<String, String> deviceMap = new HashMap<String, String>(); deviceMap.put(device.getName(), device.getAddress()); arl.add(deviceMap); // When discovery is finished, change the Activity title } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { setProgressBarIndeterminateVisibility(false); setTitle(R.string.select_device); if (mNewDevicesArrayAdapter.getCount() == 0) { String noDevices = getResources().getText(R.string.none_found).toString(); mNewDevicesArrayAdapter.add(noDevices); } } } }; 
+1
source

you can use the startDiscovery () method.

I don't have a code sample right now, but you can see: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery%28%29

Hope this helps!

+1
source

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


All Articles