1

我正在尝试搜索附近的蓝牙设备,为了完成这项任务,我正在使用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还是空的?

4

1 回答 1

4

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

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

请通读本关于 Android 中蓝牙的文档...它将引导您完成设置蓝牙发现应用程序的过程。

于 2012-06-26T15:37:00.883 回答