0

我正在尝试在 android 智能手机上创建一个应用程序,以通过蓝牙将数据发送到板上。当我尝试从 ArrayAdapter 调用对象时发生错误。

package com.example.btfinddevices;


import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class BtFind extends ListActivity {

    private static final int REQUEST_ENABLE_BT = 1;
    private ConnectThread mConnectThread;
    private BluetoothDevice mbluetoothdevice;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        final ArrayAdapter<BluetoothDevice> btArrayAdapter 
                = new ArrayAdapter<BluetoothDevice>(getApplicationContext(),
                android.R.layout.simple_list_item_1);

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device does not support Bluetooth
            Toast.makeText(this, "Bluetooth not supported", Toast.LENGTH_SHORT).show();
        }
        else{
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }

                mBluetoothAdapter.startDiscovery();
                BroadcastReceiver btReceiver = 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
                    StringBuilder b = new StringBuilder();
                    b.append(device.getAddress());
                    String s = b.toString();

                    if(s.equals("00:06:66:44:E6:00")){
                        btArrayAdapter.add(device);
                    }

                }
            }
        };
        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(btReceiver, filter); // Don't forget to unregister during onDestroy
        setListAdapter(btArrayAdapter);
        }
    }   



    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        //make the connetcion and start an intent
        Log.e("Onitemclick", "1");
        final ArrayAdapter<BluetoothDevice> btArrayAdapter2
        = new ArrayAdapter<BluetoothDevice>(getApplicationContext(),
        android.R.layout.simple_list_item_1);
        Log.e("Onitemclick", "2");
        //mbluetoothdevice= btArrayAdapter2.getItem(position);
        Log.e("Onitemclick", "3");
        mConnectThread=new ConnectThread(btArrayAdapter2.getItem(position));
        Log.e("Onitemclick", "4");
        mConnectThread.run();
        Log.e("Onitemclick", "4");

    }

}

该程序可以毫无问题地创建数组,但是当我在 onListItemClick 函数上调用一个对象时,程序失败了......知道可能是什么吗?谢谢

4

0 回答 0