gpt4 book ai didi

android - AsyncTask 中的蓝牙发现

转载 作者:行者123 更新时间:2023-11-29 16:13:34 24 4
gpt4 key购买 nike

我正在尝试搜索附近的蓝牙设备,为了完成此任务,我正在使用 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 函数返回所有设备的数组列表。我的问题是这个列表根本不包含任何对象。我确信我已经启用了可发现的设备任何提示将不胜感激

提前致谢!

编辑

下面是我的 list 文件:

<?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 还是空的?

最佳答案

首先,java没有函数。它有方法

其次,您不应该在这里使用 AsyncTaskAsyncTask 用于在 UI 线程之外执行可能代价高昂的操作。蓝牙发现是异步的,所以你不需要引入多线程来发现其他设备。此外,您在 AsyncTask 中所做的一切似乎都在定义和注册新的 BroadCastReceiver。这将在几毫秒内执行(创建/执行实际的 AsyncTask 甚至可能需要更长的时间)。因此,您的 onPostExecute 方法将在您注册接收器后立即被调用。这可能是您感到困惑的地方。

请通读此documentation在 Android 中的蓝牙上...它将引导您完成设置应用程序以进行蓝牙发现的过程。

关于android - AsyncTask 中的蓝牙发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11210420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com