I have a bluetooth device discovery code.
I get to the option BluetoothAdapter.ACTION_DISCOVERY_STARTED
and BluetoothAdapter.ACTION_DISCOVERY_FINISHED
but it does not discover devices having the device of another mobile device and that of the pc visible.
This is the code:
// Instanciamos un BroadcastReceiver que se encargara de detectar cuando
// un dispositivo es descubierto.
private final BroadcastReceiver bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Cada vez que se descubra un nuevo dispositivo por Bluetooth, se ejecutara
// este fragmento de codigo
switch (intent.getAction()) {
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
Toast.makeText(getBaseContext(), "Comienza la busqueda", Toast.LENGTH_SHORT).show();
break;
case BluetoothDevice.ACTION_FOUND:
// Acciones a realizar al descubrir un nuevo dispositivo
// Si el array no ha sido aun inicializado, lo instanciamos
if (arrayDevices == null)
arrayDevices = new ArrayList<BluetoothDevice>();
// Extraemos el dispositivo del intent mediante la clave BluetoothDevice.EXTRA_DEVICE
BluetoothDevice dispositivo = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Añadimos el dispositivo al array
arrayDevices.add(dispositivo);
// Le asignamos un nombre del estilo NombreDispositivo [00:11:22:33:44]
String descripcionDispositivo = dispositivo.getName() + " [" + dispositivo.getAddress() + "]";
// Mostramos que hemos encontrado el dispositivo por el Toast
Toast.makeText(getBaseContext(), "Se ha detectado un nuevo dispositivo" + ": " + descripcionDispositivo, Toast.LENGTH_SHORT).show();
break;
// Codigo que se ejecutara cuando el Bluetooth finalice la busqueda de dispositivos.
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
// Acciones a realizar al finalizar el proceso de descubrimiento
// Instanciamos un nuevo adapter para el ListView mediante la clase que acabamos de crear
ArrayAdapter arrayAdapter = new BluetoothDeviceArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_2, arrayDevices);
lvDispositivos.setAdapter(arrayAdapter);
Toast.makeText(getBaseContext(), "Fin de la búsqueda", Toast.LENGTH_SHORT).show();
break;
}
}
/* private void registrarEventosBluetooth() {
// Registramos el BroadcastReceiver que instanciamos previamente para
// detectar las distintos acciones que queremos recibir
IntentFilter filtro = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filtro.addAction(BluetoothDevice.ACTION_FOUND);
//this.registerReceiver(bReceiver, filtro);
}*/
};
private Button btnNewPairing;
private BluetoothAdapter bAdapter;
private ArrayList<BluetoothDevice> arrayDevices;
private ListView lvDispositivos;
private BluetoothDeviceArrayAdapter deviceArrayAdapter;
private boolean btn = true;
private final static int REQUEST_ENABLE_BT = 1;
private BluetoothManager mBluetoothManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
this.bAdapter = mBluetoothManager.getAdapter();
setContentView(R.layout.activity_note);
if (!bAdapter.isEnabled()) {
// El Bluetooth está apagado, solicitamos permiso al usuario para iniciarlo
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
// REQUEST_ENABLE_BT es un valor entero que vale 1
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
btnNewPairing = (Button) findViewById(R.id.buttonNewPairing);
btnNewPairing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onStartButtonClick(btnNewPairing);
}
});
lvDispositivos = (ListView) findViewById(R.id.lvDispositivos);
// lvDispositivos.setAdapter(cadidateListAdapter);
// ADAPTER
//deviceArrayAdapter = new BluetoothDeviceArrayAdapter(this, BluetoothDevice);
/*deviceCandidatesView.setAdapter(cadidateListAdapter);*/
IntentFilter bluetoothIntents = new IntentFilter(BluetoothDevice.ACTION_FOUND);
bluetoothIntents.addAction(BluetoothDevice.ACTION_UUID);
bluetoothIntents.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
bluetoothIntents.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
bluetoothIntents.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
bluetoothIntents.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(bReceiver, bluetoothIntents);
//startDiscovery();
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// Se ha encontrado el dispositivo. Podemos obtener la información
String descripcionDispositivo = device.getName() + " [" + device.getAddress() + "]";
Toast.makeText(getBaseContext(), "Se ha detectado un nuevo dispositivo" + ": " + descripcionDispositivo, Toast.LENGTH_SHORT).show();
}
};
public void onStartButtonClick(View button) {
if (arrayDevices != null)
arrayDevices.clear();
if (isScanning()) {
stopDiscovery();
btnNewPairing.setText("START PAIRING");
} else {
btnNewPairing.setText("STOP PAIRING");
startDiscovery();
}
}
private void stopDiscovery() {
bAdapter.cancelDiscovery();
}
private boolean isScanning() {
return bAdapter.isDiscovering();
}
public boolean startDiscovery() {
bAdapter.startDiscovery();
return true;
}
}
In the AndroidManifest I have the following permissions:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I am using version 6 of Android. Does anyone see any error?