Deepak Joshi
Deepak Joshi

Reputation: 1

Android: Broadcast Receiver does not receive BluetoothDevice.ACTION_ACL_CONNECTED on restarting the application

I want my app to auto-connect to already connected bluetooth device on restarting the app. Below is procedure I am performing:-

  1. [Initially] Bluetooth device is 'ON': Then on starting the app.
    [Behavior]--> Bluetooth device gets paired and connected successfully ( Intent 'ACTION_ACL_CONNECTED' is received)

  2. Bluetooth device is 'ON': Closed the app, then started the app again.
    [Behavior]--> Even though it is connected as displayed on Bluetooth setting, and Broadcast Receiver does not receive Intent 'ACTION_ACL_CONNECTED'.

Note:- On closing the app, it does not disconnect the bluetooth connection. So, on successful connection app straightaway goes to the HomeScreen. Otherwise, the app goes to a screen having button that takes it to Bluetooth setting(onClickListener present in the code below)

I am new to android development, so I really don't know where am I going wrong. I looked up the solutions for similar issues and applied them, but to no effect.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
        registerReceiver(mReceiver, filter);
        IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        this.registerReceiver(mReceiver, filter1);
        m_app = (BtApp) getApplication();
        imagebt = (ImageView) this.findViewById(R.id.imagebt);

        imagebt.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                final Toast tag = Toast.makeText(getApplicationContext(), "Connect to device", Toast.LENGTH_LONG);
                tag.show();

                new CountDownTimer(1000, 1000)
                {

                    public void onTick(long millisUntilFinished) {tag.show();}
                    public void onFinish() {
                        //tag.show();
                    }

                }.start();

                if(mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()){
                    mBluetoothAdapter.startDiscovery();
                }

                Intent intentBluetooth = new Intent();
                intentBluetooth.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
                startActivity(intentBluetooth);

                }
        });

    }

private BroadcastReceiver   mReceiver   = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ( BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                m_app.m_main.setupCommPort();
                device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                m_app.m_device = device;
                isconnected = true;
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if ( m_app.m_main.m_BtService != null && m_app.m_main.m_BtService.getState() != BluetoothRFCommService.STATE_CONNECTED ) {
                            m_app.m_main.m_BtService.connect(device, false);
                        }
                    }
                }, 3500);

            } else if ( BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action) ) {
                isconnected = false;
                m_app.m_main.tabHost.setCurrentTab(0);
            }

        }
    };


@Override
    protected void onStop()
    {
        unregisterReceiver(mReceiver);
        super.onStop();
    }

Upvotes: 0

Views: 2011

Answers (1)

Victor Nidens
Victor Nidens

Reputation: 474

You won't get BluetoothDevice.ACTION_ACL_CONNECTED event since the device is still connected. The event is fired only on changing of device state from disconnected to connected.

You have 2 options.

  1. You can put your BroadcastReceiver with BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED filters into the Service and track the device connection state in the background. On your app startup you can ask the service to give you the current state of the device.

  2. You can check if some of the Bluetooth profiles contains your device name in the list of connected devices.

For API 18+ you can use BluetoothManager#getConnectedDevices() for API below 18 you can use the following snippet (for each Bluetooth profile)

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        for (BluetoothDevice device : proxy.getConnectedDevices()) {
            if (device.getName().contains("DEVICE_NAME")) {
                deviceConnected = true;
             }
        }

        if (!deviceConnected) {
            Toast.makeText(getActivity(), "DEVICE NOT CONNECTED", Toast.LENGTH_SHORT).show();
        }
        mBluetoothAdapter.closeProfileProxy(profile, proxy);

    }

    public void onServiceDisconnected(int profile) {
        // TODO
    }
};

mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.A2DP);

Upvotes: 2

Related Questions