我正在尝试搜索附近的蓝牙设备,为了完成这项任务,我正在使用AsyncTask
我的 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);
}
}
如您所见,该doInBackground
函数返回所有设备的 Arraylist。我的问题是这个列表根本不包含任何对象。我确定我已经启用了Discoverable
任何提示的设备将不胜感激
提前致谢!
编辑
下面是我的清单文件:
<?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>
编辑#2
我试图将一个虚拟对象添加到列表中,而不是下面的代码行
devices.add(device.getName().toString() + "\n" + device.getAddress().toString());
我试过
devices.add("Test");
但是我调试的时候,arraylist还是空的?