2014-07-10 315 views
1

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

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); 
} 
} 

,我已经在方法查找()和显示java.lang.NullPointerException当我试图清除一个ArrayAdapter

+0

更换

ArrayAdapter<String> arrayAdapter = new ... 
中的onCreate()

确保您arrayAdapter不为空... –

+0

请张贴logcat中,让观众可以帮助你 – LMK

+0

这是logcat的HTTP://贴.org/73529 –

回答

1

您intializing局部变量arrayAdapter和不使用它。然而,类变量arrayAdapter没有初始化。与

arrayAdapter = new ... 
+0

非常感谢! –