sevare sertere
sevare sertere

Reputation: 106

Android Bluetooth Discovery in a sequential method?


I am new to Android programming. I want to make a library (a JAR file) that contains the Bluetooth discovery functionality.
Since it is a library, the method inside should be a sequential method (start the Bluetooth discovery, wait for an amount of time, and return the result).
I made a code below, but it does not work. In the LogCat, I could see the intents, but the BroadcastReceiver could not catch the intents.
Is there something wrong with the code?.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    mSendButton = (Button) findViewById(R.id.button1);
    mSendButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            doDiscovery();
        }
    });
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.e("device_name", device.getName());
            Log.e("device_add", device.getAddress());
            numberOfDevice++;
            Found_Device = true;
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if(numberOfDevice == 0){
                Log.e("DISCOVERY", "No Device found");
            }
            Log.e("DISCOVERY", "Number of device :" + numberOfDevice);
            Discovery_Finish = true;
        }
    }
};

public void doDiscovery(){
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(mReceiver, filter);

    Discovery_Finish = false;
    Found_Device = false;
    long counter = 0;

    if (mBluetoothAdapter.isDiscovering()) {
        mBluetoothAdapter.cancelDiscovery();
    }
    mBluetoothAdapter.startDiscovery();
    while((Found_Device == false) || (Discovery_Finish == false)){
        counter++;
        if (counter >= 1000000){
            break;
        }
    }
    // Add a code to check the number of device

    unregisterReceiver(mReceiver);
    mBluetoothAdapter.cancelDiscovery();
}

Is there any way to check the intent without using BroadcastReceiver?
Thank you for your help.

Upvotes: 2

Views: 562

Answers (2)

Daniel
Daniel

Reputation: 43

You are overwriting your IntentFilter when you do this:

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

You should do it like this:

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothDevice.ACTION_FOUND);

Upvotes: 0

7heViking
7heViking

Reputation: 7587

I think you are missing to start the discovering the other devices.

Upvotes: 0

Related Questions