Android蓝牙连接

时间:2014-07-10 09:32:45

标签: java android import bluetooth android-studio

我试图在蓝牙上列出找到的设备,但我坚持这些错误,有人可以帮助我吗?

package com.yast;


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.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class ConnectionScreen extends ActionBarActivity {
    //Declaration of components
    private static final int REQUEST_ENABLE_BT = 1;
    private Button buttonSearch;
    private CheckBox checkBoxAutoConnect;
    private ListView listOfDevices;
    private TextView statusId;
    private TextView statusConnection;
    private BluetoothAdapter bluetoothadapt;
    private Set<BluetoothDevice> pairedDevices;
    private ArrayAdapter<String> arrayAdapter;

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

    linkViewToResources();

    bluetoothadapt=BluetoothAdapter.getDefaultAdapter();
    if(bluetoothadapt==null){
        statusConnection.setText("Not supported");
        Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",Toast.LENGTH_LONG).show();
        buttonSearch.setEnabled(false);
    } else{
        buttonSearch.setEnabled(true);
    }


    //ListView
    List<String> arrayListOfDevices = new ArrayList<String>();
    arrayListOfDevices.add("unu");
    arrayListOfDevices.add("doi");

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayListOfDevices);
    listOfDevices.setAdapter(arrayAdapter);


}

private void linkViewToResources(){
    buttonSearch = (Button) findViewById(R.id.searchButton);
    buttonSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            find(view);
        }
    });
    checkBoxAutoConnect = (CheckBox) findViewById(R.id.checkBoxAutoconnect);
    listOfDevices = (ListView) findViewById(R.id.listOfDevices);
}

final BroadcastReceiver bReceiver = 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 the MAC address of the object to the arrayAdapter
            arrayAdapter.add(device.getName() + "\n" + device.getAddress());
            arrayAdapter.notifyDataSetChanged();
        }
    }
};

public void list(View view){
    // get paired devices
    pairedDevices = bluetoothadapt.getBondedDevices();

    // put it's one to the adapter
    for(BluetoothDevice device : pairedDevices)
        arrayAdapter.add(device.getName()+ "\n" + device.getAddress());

    Toast.makeText(getApplicationContext(),"Show Paired Devices",
            Toast.LENGTH_SHORT).show();

}

public void find(View view) {
    if (bluetoothadapt.isDiscovering()) {
        // the button is pressed when it discovers, so cancel the discovery
        bluetoothadapt.cancelDiscovery();
    }
    else {
        arrayAdapter.clear();
        bluetoothadapt.startDiscovery();`enter code here`

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

我在方法find()中有java.lang.NullPointerException,当我尝试清除arrayAdapter时

1 个答案:

答案 0 :(得分:1)

您正在初始化局部变量arrayAdapter而不使用它。但是,类变量arrayAdapter未初始化。取代

ArrayAdapter<String> arrayAdapter = new ...
使用

在onCreate()中

arrayAdapter = new ...
相关问题