private void setup() { for (BluetoothDevice device : btadapter.getBondedDevices()) { devicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(discoveryReceiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(discoveryReceiver, filter); }
@Override public void onReceive(Context context, Intent intent) { /* do something with the intent here */ String action = intent.getAction(); if (action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getBondState() != BluetoothDevice.BOND_BONDED) { devicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); } } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { startDiscoveryButton.setEnabled(true); } }