I want to display the list of bonded devices with the Android Bluetooth API, but I don't know why in this code when I run it on my device no toast is displayed? By the way, the toast will contain the name of each device.
package com.example.fatma.cst;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Set;
import java.util.Vector;
public class MainActivity extends AppCompatActivity {
TextView txt = null;
private static final int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
final Vector mArrayAdapter = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
Toast t=new Toast(this);
t.setText("Sorry your phone do not support Bluetooth");
t.show();
}
else
{
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
if(REQUEST_ENABLE_BT!=0) {
/* bluetoothAdapter.startDiscovery();
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Finding devices
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};*/
//IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// registerReceiver(mReceiver, filter);
Set<BluetoothDevice> devices;
devices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice blueDevice : devices) {
Toast.makeText(this, "Device = " + blueDevice.getName(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
Here's a method I use (and it's nothing special) to iterate and log or toast paired bluetooth devices. Your code appears to be doing the same things.
void iterateBluetoothDevices() {
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter != null) {
Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
Log.i(LOGTAG, String.format("found %d bluetooth devices", (pairedDevices != null) ? pairedDevices.size() : 0));
if (pairedDevices.size() > 0) {
for (BluetoothDevice d: pairedDevices) {
String btID = String.format("bluetooth device [%s] type: %s", d.getName(), d.getType());
Log.i(LOGTAG, btID);
Toast.makeText(this, btID, Toast.LENGTH_SHORT).show();
}
}
}
}
Try this code by adding Toast in for loop.
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
You can refer to this also Bluetooth Android Developer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.