0

我正在开发一个必须连接到蓝牙设备的应用程序。

当我点击 btn_discover 时Button,它会调用BroadcastReceiver并扫描新的蓝牙设备并显示在TextView.

当我使用 时TextView,我可以在 Textview 上看到蓝牙设备。

现在我想将其更改TextViewListView.

但它总是崩溃。在BroadcastReceiver'sonReceive()中,我写道:

newDevicelistArrayAdapter.add(device.getName());

这是我的 Java 代码:

package com.example.bttest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import com.example.bttest.R.menu;

import android.os.Bundle;
import android.app.Activity;
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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {


    public BluetoothAdapter mBluetoothAdapter;
    private static final int REQUEST_SELECT_DEVICE = 1;
    public Button btn_scan;
    public Button btn_discover;
    public TextView pair_list;
    public TextView scan_list;
    public ListView scan_list_1;
    public Set<BluetoothDevice> pairedDevices;
    public ArrayAdapter<String> newDevicelistArrayAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        btn_scan = (Button)findViewById(R.id.btn_scan);
        btn_discover = (Button)findViewById(R.id.btn_discover);
        pair_list = (TextView)findViewById(R.id.pair_list);
        scan_list = (TextView)findViewById(R.id.scan_list);


        newDevicelistArrayAdapter = new ArrayAdapter<String>(this, R.layout.main);
        scan_list_1 = (ListView)findViewById(R.id.scan_list_1);
        scan_list_1.setAdapter(newDevicelistArrayAdapter);



        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null)
        {
            Toast.makeText(this, "No support bluetooth", Toast.LENGTH_SHORT).show();
            return;
        }else if(mBluetoothAdapter != null)     {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_SELECT_DEVICE);
        }


        //******************scan按鈕動作-將已配對過的藍芽裝置列出來
        btn_scan.setOnClickListener(new OnClickListener() {     
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                pairedDevices = mBluetoothAdapter.getBondedDevices();
                if(pairedDevices.size()>0)  {
                    for(BluetoothDevice bDevice : pairedDevices)  {
                        pair_list.append(bDevice.getName() + "\n" + bDevice.getAddress() + "\n" + bDevice.getBondState() + "\n" );
                    }
                }
            }
        });
        //******************scan按鈕動作結束


        btn_discover.setOnClickListener(new OnClickListener() {     
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                registerReceiver(mReceiver, filter);
                filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
                registerReceiver(mReceiver, filter);
                mBluetoothAdapter.startDiscovery(); 

            }
        });




    }





    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                scan_list.append(device.getName() + "\n" + device.getAddress() + "\n");
                newDevicelistArrayAdapter.add(device.getName());

            }
        }
    };

    protected void onDestroy() {

        super.onDestroy();
        if (mBluetoothAdapter != null) {
            mBluetoothAdapter.cancelDiscovery();
        }
        unregisterReceiver(mReceiver);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

使用 ListView 和 ArrayAdapter 有什么问题吗?

4

1 回答 1

0

用这个替换onReceive()BroadcastReceiver的:

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    String action = intent.getAction();
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        scan_list.append(device.getName() + "\n" + device.getAddress() + "\n");
        newDevicelistArrayAdapter.notifyDataSetChanged();
    }
}
于 2013-09-29T11:53:36.960 回答