Bluetooth discovery not starting on first click

844 views Asked by At

I have few tabs in my activity and one of them is to search for bluetooth devices and display in listview. But I am having strange issue in which when I click on Bluetooth tab first time, it does not start discorvery but when I come back after clicking on any other tab. The search starts and also display the list of bluetooth devices. I could not figure out why it is happening...any help

it is never entering in OnRecieve on first time

Sharing code snippet

public class BluetoothSettings extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_settings);

    listview = (ListView) findViewById(R.id.listbt);
    txtgeo = (TextView) findViewById(R.id.txtbt);
    progress = (ProgressBar) findViewById(R.id.progressbtBar);
    wait_msg = (TextView) findViewById(R.id.txt_wait_msg);

}
@Override protected void onPause()
{
    bluetooth.cancelDiscovery();
    if(bReceiver != null) {
        bReceiver = null;
    }
    super.onPause();
    Log.d(TAG, "onPause");
}


@Override protected void onResume()
{
    super.onResume();
    if(setBt != null){
        setBt.clear();
        listview.setAdapter(null);
    }
    bluetooth.enable();
    progress.setVisibility(View.VISIBLE);
    wait_msg.setVisibility(View.VISIBLE);
    bluetooth = BluetoothAdapter.getDefaultAdapter();
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    bReceiver = new BluetoothReceiver();
    registerReceiver(bReceiver, filter);
    bluetooth.startDiscovery();
    if(!bluetooth.isEnabled()) {
        shouldTurnoffBt = true;
    }
    Log.d(TAG, "onResume");
}

@Override protected void onStart()
{
    super.onStart();
    Log.d(TAG, "onStart");
}

@Override protected void onStop() {
    super.onStop();
    Log.d(TAG, "onStop");
}

class BluetoothReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        action = intent.getAction();
        Log.d(TAG, "SDSDSSSSSSSSSS ");
        if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
        {
            Log.d(TAG, "dkidkididididididi #1 ");
            txtgeo.setVisibility(View.INVISIBLE);
        }
        if(BluetoothDevice.ACTION_FOUND.equals(action))
        {
            Log.d(TAG, "dkidkididididididi #2 ");
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            setBt.add(new Property(device.getName(), device.getAddress(), rssi));
        }

        if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
        {
            BluetoothSettings.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progress.setVisibility(View.INVISIBLE);
                    wait_msg.setVisibility(View.GONE);
                    if(shouldTurnoffBt)
                        bluetooth.disable();
                    if(setBt != null && setBt.size() > 0){
                       Log.d(TAG, "dkidkididididididi #3 ");
                        ArrayList<Property> lt = new ArrayList<Property>(setBt);
                        esWiFiSettings.settingSort(lt);
                        btAdapter = new btAdapter(BluetoothSettings.this, R.layout.bt_adapter, lt, "Bluetooth");
                        listview.setAdapter(btAdapter);
                        bluetooth.cancelDiscovery();
                    } else {
                        txtgeo.setVisibility(View.VISIBLE);
                        txtgeo.setText("No Bluetooth Device Found");
                    }
                }
            });
        }
    }

}
private BluetoothReceiver bReceiver;
public HashSet<Property> setBt = new HashSet<Property>();
String action;
boolean shouldTurnoffBt = false;
ListView listview;
BluetoothAdapter bluetooth;
private TextView txtgeo, wait_msg;
private ProgressBar progress;
1

There are 1 answers

0
hgross On

I can only guess but checking your code and the BluetoothAdapter JavaDoc there is a good chance that your bluetooth.startDiscovery() call returns false. From the JavaDoc:

If Bluetooth state is not STATE_ON, this API will return false. After turning on Bluetooth, wait for ACTION_STATE_CHANGED with STATE_ON to get the updated value.

Check the return value and only start the discovery when you are sure that bluetooth is enabled. If bluetooth.isEnabled() returns false, you have to activate bluetooth (like you already do) but you should catch the cited ACTION_STATE_CHANGED-Event in your receiver afterwards (and therefore register the receiver before you call bluetooth.enable()).

Also, don't forget to call unregisterReceiver(bReceiver) in onPause or onStop. If there is more code that you did not show us, the lines

if(!bluetooth.isEnabled()) { shouldTurnoffBt = true; }

look pretty suspicious regarding your described behaviour ;-) Be aware that the bluetooth discovery is pretty heavyweight and takes up to 12 seconds.

This BroadcastReceiver things can be tricky. I wrote a wrapper class for Blaubot a while ago. Check this code: BlaubotBluetoothDeviceDiscoveryReceiver (you will also need this interface). Or if you simply want to connect multiple Android devices, just use Blaubot with the NFC-Beacon to eliminate the need for the discovery completely.