Displaying a notification when bluetooth is disconnected - Android
- by Ryan T
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);
        }
    }
};