1

I am trying to create a program that will display a notification to the user if a Blue tooth device suddenly comes out of range from my Android device. I currently have the following code but no notification is displayed.

I was wondering if it was possible I shouldn't use ACTION_ACL_DISCONNECTED because I believe the bluetooth stack would be expecting packets that state a disconnect is requested. My requirements state that the bluetooth device will disconnect without warning.

Thank you for any assistance!

BluetoothNotification.java: //This is where the notification is created.

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;


public class BluetoothNotification extends Activity
{
public static final int NOTIFICATION_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Define configuration for our notification */
    int icon = R.drawable.logo;
    CharSequence tickerText = "This is a sample notification";
    long when = System.currentTimeMillis();
    Context context = getApplicationContext();
    CharSequence contentTitle = "Sample notification";
    CharSequence contentText = "This notification has been generated as a result of BT Disconnecting";
    Intent notificationIntent = new Intent(this, BluetoothNotification.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    /** Initialize the Notification using the above configuration */
    final Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    /** Retrieve reference from NotificationManager */

    String ns = Context.NOTIFICATION_SERVICE;
    final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);


    mNotificationManager.notify(NOTIFICATION_ID, notification);

    finish();
}    
}

Snippet from OnCreate: //Located in Controls.java

IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);

Snippet from Controls.java:

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

        if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
               //Device has disconnected
            NotificationManager nm = (NotificationManager) 
                    getSystemService(NOTIFICATION_SERVICE);
        }

    }

};
Miral Dhokiya
  • 1,720
  • 13
  • 26
Ryan T
  • 21
  • 1
  • 4
  • Great job and wounderful question,.. And an interesting question.. Thanks, and if u got the answer means, post it!.. I too trying for that Mr. Ryan – gowri Dec 06 '12 at 05:05

1 Answers1

0

What is the link between your BroadCastReceiver and your Activity ?

There is no need for this activity, put everything in your broadcast receiver to display the notification.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • So you're saying there's no need to create the BluetoothNotification.java file? Simply place all configuration code for notification in the "if" statement? – Ryan T Dec 06 '12 at 05:06
  • I am new to Android programming so I was using this as a sample. – Ryan T Dec 06 '12 at 05:13
  • Under "getActivity" I receive an error "The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (new BroadcastReceiver(){}, int, Intent, int)" All I have done is copy over what is done in BluetoothNotification.java into the if statement. – Ryan T Dec 06 '12 at 05:21
  • What do you want to do when your notification is clicked ? That's what you describe using a PendingIntent. Do you want to go to an activity or use a service ? Usually you will open an activity, google an example. – Snicolas Dec 06 '12 at 05:51
  • I would like upon the notification to be selected, the user to be returned to Controls.java – Ryan T Dec 06 '12 at 05:59
  • What is Control. Please try something by yourself at this point – Snicolas Dec 06 '12 at 06:03
  • You asked a question and I answered it :) I got rid of the errors but not sure if it works. Will have to verify tomorrow. – Ryan T Dec 06 '12 at 06:13
  • This is example is not real life enough, you always need to make the activity respond to this. I can't see the link between receiver and activity.. it seems impossible to me. Android Java is very hard to program for. in windows I would just have an event on my activity... done, simple.And in windows I can access all the properties of the activity from my receiver event. This is waay over complicated. – hamish Jun 02 '14 at 02:49