Reputation: 33
I'm new in android and I'm going to work with Bluetooth. I have the code below but the problem is that it not display list of devices when I run the application in my phone. Other buttons turn on, turn off and get visible works without problem.
public class MainActivity extends Activity {
private Button On,Off,Visible,list;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
On = (Button)findViewById(R.id.button1);
Off = (Button)findViewById(R.id.button2);
Visible = (Button)findViewById(R.id.button3);
list = (Button)findViewById(R.id.button4);
lv = (ListView)findViewById(R.id.listView1);
BA = BluetoothAdapter.getDefaultAdapter();
}
//Button turn on
public void on(View view){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(),"Turned on"
,Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Already on",
Toast.LENGTH_LONG).show();
}
}
//Button list devices
public void list(View view){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(),"Showing Paired Devices",
Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter
(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
//Button turn off
public void off(View view){
BA.disable();
Toast.makeText(getApplicationContext(),"Turned off" ,
Toast.LENGTH_LONG).show();
}
//Button get visible
public void visible(View view){
Intent getVisible = new Intent(BluetoothAdapter.
ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
@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;
}
}
Upvotes: 0
Views: 258
Reputation: 276
why don't you implement the 'onActivityResult' method? if your code above was refer to any tutorial, please double check it. i think that you missed to put 'onActivityResult' method.
EDIT:
try to add this code at the end of 'onCreate' method:
@Override
onCreate(...){
...
...
list.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View view){
list();
}
});
}
i think you missed to call each method when the button is clicked.
Upvotes: 0
Reputation: 6857
Do you have permissions in your manifest file?
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Upvotes: 1