Question

Can someone explain me why this code give me back always only one bluetooth device, even if i'm working with two galaxy one next to the other one? This code is running on a Samsung Galaxy Tab and i'm using for tests a Samsung Galaxy Gio both with the right Bluetooth activation. If i check on the default research it works ..but not with this code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    out = new TextView(this);
    setContentView(out);
    // Getting the Bluetooth adapter
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    out.append("\nAdapter: " + adapter);

    // Check for Bluetooth support in the first place
    // Emulator doesn't support Bluetooth and will return null
    if (adapter == null) {
        out.append("\nBluetooth NOT supported. Aborting.");
        return;
    }

    // Starting the device discovery
    out.append("\nStarting discovery...");
    adapter.startDiscovery();
    out.append("\nDone with discovery...");

    // Listing paired devices out.append("\nDevices Paired:");
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    for (BluetoothDevice device : devices) {
        out.append("\nFound device: " + device);
    }
}
Was it helpful?

Solution

I think you are misunderstanding what you are doing.

On one hand by calling this ...

Set devices = adapter.getBondedDevices(); for (BluetoothDevice device : devices) { out.append("\nFound device: " + device); }

... you are looking up the already paired devices. If you only get one the reason is simple, you have only one paired. Take into account that this will return all the paired devices, no matter if they are live or not.

On the other hand you are starting a discovery with ...

adapter.startDiscovery();

... However you have not registered a broadcast receiver to process the *BluetoothDevice.ACTION_FOUND* intents that you will receive with each discoverable Bluetooth device seen. Discoverable is key here because by default Android devices are not discoverable and they only allow a maximum time of typical 120 seconds.

OTHER TIPS

Look at the API

http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()

startDiscovery is asynchronous - it will scan for about 12 seconds and then get the device names of any addresses found in the scan. You don't wait for discovery to finish so it's not surprising that not all devices in range are discovered by the time you check the results.

do you say out.append("\nAdapter: " + adapter);

but if do you are work in eclipse with xml OR INTELLIJ

TextView txt;
String text;

....

text += ("Adapter:" +  adapter);
txt.setText(text);

Do you see the error?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top