import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private Button bstpButton;
private ListView mListView;
private ArrayAdapter<string> BTArrayAdapter;
private Set<bluetoothdevice> pairedDevices;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
if(!mBluetoothAdapter.isEnabled()){
Toast.makeText(getApplicationContext(), "Turn Bluetooth ON & pair with the device", Toast.LENGTH_LONG).show();
}
bstpButton=(Button)findViewById(R.id.bootstrap);
bstpButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listPaired(v);
}
});
mListView=(ListView)findViewById(R.id.listView1);
BTArrayAdapter= new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1);
mListView.setAdapter(BTArrayAdapter);
}
public void listPaired(View view){
pairedDevices= mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if(pairedDevices.size()>0){
// Loop through paired devices and add them to BTArrayAdapter
for(BluetoothDevice device: pairedDevices){
BTArrayAdapter.add(device.getName()+"\n"+ device.getAddress());
}
}else{
Toast.makeText(getApplicationContext(), "NO DEVICES ARE CURRENTLY PAIRED", Toast.LENGTH_LONG).show();
}
}
}