I'm trying to set up an application that searches for bluetooth devices and add them in in ListView.
At the moment I managed to turn on and off bluetooth, show previously linked devices and start searching for devices, the problem is that it does not detect any device.
At the time of doing the Debug and watching the BroadcastReceiver function I realize that if you recognize the actions ACTION_DISCOVERY_STARTED and ACTION_DISCOVERY_FINISHED but you never find anything.
My function to turn Bluetooth on:
public void funcion_Bluetooth(){
if (mBluetoothAdapter == null) {
Toast.makeText(this,"Dispositivo Sin Bluetooth ",Toast.LENGTH_SHORT).show();
}else{
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
}
}
Scan function
public void scanBluetooth(){
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}else if(!mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.startDiscovery();
}
}
BroadcastReciver broadcast
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ArrayList mArrayList = new ArrayList();
final String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
mArrayAdapter = new ArrayAdapter(BluetoothActivity.this,android.R.layout.simple_list_item_1, mArrayList);
ListView_Device.setAdapter(mArrayAdapter);
}
if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
Toast.makeText(BluetoothActivity.this,"Iniciando Busqueda ",Toast.LENGTH_SHORT).show();
((Button) findViewById(R.id.button_scan)).setText(R.string.detenerBusqueda);
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
Toast.makeText(BluetoothActivity.this,"Busqueda Detenida ",Toast.LENGTH_SHORT).show();
((Button) findViewById(R.id.button_scan)).setText(R.string.buscar);
}
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
final int estado = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (estado) {
// Apagado
case BluetoothAdapter.STATE_OFF:
{
((Button)findViewById(R.id.button_Conect)).setText(R.string.ActivarBluetooth);
Toast.makeText(BluetoothActivity.this, "Bluetooth Apagado", Toast.LENGTH_SHORT).show();
break;
}
// Encendido
case BluetoothAdapter.STATE_ON:
{
pairedDevicesList();
((Button)findViewById(R.id.button_Conect)).setText(R.string.DesactivarBluetooth);
break;
}
default:
break;
}
}
}
};
A Function to call the event filter
private void registrar_EventosBluetooth() {
// Registramos el BroadcastReceiver que instanciamos previamente para
// detectar los distintos eventos que queremos recibir
IntentFilter filtro = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
filtro.addAction(BluetoothDevice.ACTION_FOUND);
filtro.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filtro.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver , filtro);
}
and its onDestroy method
@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(mReceiver );
}
Finally this is the function that shows the devices already linked
private void pairedDevicesList() {
pairedDevices = mBluetoothAdapter.getBondedDevices();
ArrayList mArrayList = new ArrayList();
if (pairedDevices.size()>0) {
for(BluetoothDevice bt : pairedDevices) {
mArrayList.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address
}
}
else {
Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show();
}
mArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, mArrayList);
ListView_Device.setAdapter(mArrayAdapter);
}