Click here to Skip to main content
15,885,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}
}
}

Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900