Je suis en train de rechercher les appareils Bluetooth à proximité, pour accomplir cette tâche je utilise un AsyncTask
Le code pour mon AsyncTask est sousdécouverte Bluetooth dans un AsyncTask
public class DeviceScan extends AsyncTask<String, Void, ArrayList<String>> {
ProgressDialog dialog;
Context _context;
BluetoothAdapter adapter;
final BroadcastReceiver blueToothReceiver;
/**
* The constructor of this class
* @param context The context of whatever activity that calls this AsyncTask. For instance MyClass.this or getApplicationContext()
*/
public DeviceScan(Context context) {
_context = context;
dialog = new ProgressDialog(_context);
adapter = BluetoothAdapter.getDefaultAdapter();
blueToothReceiver = null;
}
protected void onPreExecute() {
dialog.setTitle("Please Wait");
dialog.setMessage("Searching for devices..");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
/*
* Operations "behind the scene", do not interfere with the UI here!
* (non-Javadoc)
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected ArrayList<String> doInBackground(String... params) {
//This arraylist should contain all the discovered devices.
final ArrayList<String> devices = new ArrayList<String>();
// Create a BroadcastReceiver for ACTION_FOUND
final BroadcastReceiver blueToothReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// 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);
// Add the name and address to an array adapter to show in a ListView
devices.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
_context.registerReceiver(blueToothReceiver, filter);
return devices;
}
/*
* After the background thread has finished, this class will be called automatically
* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
protected void onPostExecute(ArrayList<String> result) {
dialog.dismiss();
Toast.makeText(_context, "Finished with the discovery!", Toast.LENGTH_LONG).show();
}
protected void onDestory() {
_context.unregisterReceiver(blueToothReceiver);
}
}
Comme vous pouvez le voir la fonction doInBackground
retourne un Arraylist de tous les appareils. Mon problème est que cette liste ne contient aucun objet du tout. Je suis sûr que j'ai activé les appareils qui sont Discoverable
Des conseils seront appréciés
Merci d'avance!
EDIT
Sous mon fichier Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.com.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
EDIT # 2
J'ai essayé d'ajouter un objet factice dans la liste, au lieu de ligne de code suivante
devices.add(device.getName().toString() + "\n" + device.getAddress().toString());
J'ai essayé
devices.add("Test");
Mais quand je debug, le arraylist est encore vide?
assurez-vous que vous avez autorisations dans manifast –
Cest pas problème, voir ma mise à jour :) –