Edwin Dijkstra
Edwin Dijkstra

Reputation: 31

Turn on bluetooth through notification button

I'm currently developing an android app and bluetooth is essential for that app. I run a background service. One thing the background service does, is to check if bluetooth is enabled or not. If it's not, there will be a new notification. When you click on it, I want bluetooth to be enabled and the user to be able to resume with whatever he was doing. Just that.

Right now, when I click the notification, it starts a new activity and a new view and bluetooth enables. I don't want that new view to be there. I tried using finish() after enabling bluetooth, but that brings me back to the last activity in my app, instead of the app that I was using before pressing the notification.

So I'm reading a news app, click the notification and bluetooth enables but also brings me to my own app instead of the news app that I was using.

This is the new notification being sent:

if (!mBluetoothAdapter.isEnabled())
{
            NotificationCompat.Builder mBuilder;

            mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.bluetooth)
                            .setContentTitle("Bluetooth disabled")
                            .setContentText("Click here to enable bluetooth")
                            .setOngoing(true);
            Intent resultIntent = new Intent(this, TurnOnBluetooth.class);
            resultIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);

            int mNotificationId = 159;

            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                            this,
                            0,
                            resultIntent,
                            PendingIntent.FLAG_UPDATE_CURRENT
                    );
            mBuilder.setContentIntent(resultPendingIntent);

            mNotifyMgr.notify(mNotificationId, mBuilder.build());

        }
    }

And this is the onCreate() in TurnOnBluetooth.java:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_turn_on_bluetooth);
        NotificationManager mNotifyMgr =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.enable();
        mNotifyMgr.cancel(159);
        finish();
    }

Notice that I commented the setContentView(); Because I don't want a new view, I just want bluetooth to be turned on and get rid of the notification after and nothing else. And finish() brings me back to my app instead of the app I was using before pressing the notification. How can I accomplish this?

Upvotes: 1

Views: 530

Answers (1)

Edwin Dijkstra
Edwin Dijkstra

Reputation: 31

I was able to figure this out myself. Instead of starting a new activity, I'm using a broadcastreceiver that does the trick.

broadcastreceiver's onReceive():

public void onReceive(Context context, Intent intent) {
        NotificationManager mNotifyMgr =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mBluetoothAdapter.enable();
        mNotifyMgr.cancel(159);
    }

Receiver in Manifest:

<receiver
            android:name="com.novioscan.service.NotificationReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="NotService"/>
            </intent-filter>
        </receiver>

New notification declaration:

if(!mBluetoothAdapter.isEnabled()){
            NotificationCompat.Builder mBuilder;

            mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.bluetooth)
                            .setContentTitle("Bluetooth disabled")
                            .setContentText("Click here to enable bluetooth")
                            .setOngoing(true);
            // Intent resultIntent = new Intent(this, TurnOnBluetooth.class);
            Intent resultIntent = new Intent("NotService");

            //resultIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);

            // Sets an ID for the notification
            int mNotificationId = 159;

            PendingIntent resultPendingIntent =
                    PendingIntent.getBroadcast(
                            this,
                            0,
                            resultIntent,
                            0
                    );
            mBuilder.setContentIntent(resultPendingIntent);

            // Builds the notification and issues it.
            mNotifyMgr.notify(mNotificationId, mBuilder.build());
        }

It all works really well now :)

Upvotes: 1

Related Questions