Displays notification when Bluetooth is turned off - Android

I am trying to create a program that will display a notification user if the Blue tooth device unexpectedly goes beyond my Android device. I currently have the following code but no notification is displayed.

I was wondering if it is possible that I should not use ACTION_ACL_DISCONNECTED , because I believe that the bluetooth stack expects packets that claim to be disconnected to be requested. My requirements indicate that the Bluetooth device will turn off without warning.

Thanks for the help!

BluetoothNotification.java: // A notification is generated here.

 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); } } }; 
+1
source share
1 answer

What is the relationship between your BroadCastReceiver and your activity?

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

0
source

All Articles