Jonathan Roth
Jonathan Roth

Reputation: 1271

Android Bluetooth Discovery

When discovering other bluetooth devices I get 2 broadcasts sent for each device found. The first is sent during scan and when finishing scan a broadcast is sent for all found device at once. I am adapting the BluetoothChat sample in the SDK.

Here is my 'BroadcastReceiver':

 private final BroadcastReceiver foundRec = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothDevice.ACTION_FOUND)) {

            Log.e(TAG, "--- device found ---");

            BluetoothDevice dev = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (dev.getBondState() == BluetoothDevice.BOND_BONDED) {
                availableDevices.add(dev.getName() + " (paired)");
            } else {
                availableDevices.add(dev.getName());
            }

        } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){

            Log.d(TAG, "DISCOVERY STARTED");
            findViewById(R.id.lookup).setVisibility(View.VISIBLE);

        }
    }
};

Thank you!

Upvotes: 0

Views: 2784

Answers (2)

nmxprime
nmxprime

Reputation: 1506

Actually ICS and greater devices send two broadcasts, one for inquiry scan and other for page scan. Thats why we are receiving twice!!

But i tested the same code in a 2.3.5 device, in which i received only one broadcast receive!! How come it manages? Which we require, individual broadcast for inquiry and page or a single broadcast!! Can anyone speak on this!!

Upvotes: 0

broody
broody

Reputation: 707

I keep an array of devices. Every time ACTION_FOUND is received I go through the device array to check if it's present. My syntax might not be right, typed in browser... but hopefully u get the idea.

I don't know what you use your availableDevices array for but it might be more useful if you a BluetoothDevice array instead of String array. You can always get the name and check for bonded state outside of onReceive.

private final BroadcastReceiver foundRec = new BroadcastReceiver() {
List<BluetoothDevice> BtDevices = new ArrayList<BluetoothDevice>();
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals(BluetoothDevice.ACTION_FOUND)) {

        Log.e(TAG, "--- device found ---");

        BluetoothDevice dev = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if(!IsBtDevPresent(dev)) {
            BtDevices.add(dev);
            if (dev.getBondState() == BluetoothDevice.BOND_BONDED) {
                availableDevices.add(dev.getName() + " (paired)");
            } else {
                availableDevices.add(dev.getName());
            }
        }
    } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_STARTED)){

        Log.d(TAG, "DISCOVERY STARTED");
        findViewById(R.id.lookup).setVisibility(View.VISIBLE);

    }
}

private boolean IsBtDevPresent(BluetoothDevice dev) {
    int size = BtDevices.size();
    for(int i = 0; i<size; i++) {
         if( BtDevices.get(i).equals(dev)) {
             return true;
         }
    }
    return false;
}

};

Upvotes: 1

Related Questions