2012-12-06 55 views
1

我试图创建一个程序,它会向用户显示notification,如果蓝牙设备突然超出我的Android设备范围。我目前有以下代码,但没有通知显示。当蓝牙断开连接时显示通知 - Android

我想知道是否有可能我不应该使用ACTION_ACL_DISCONNECTED,因为我相信bluetooth堆栈会期望数据包表明请求断开连接。我的要求声明蓝牙设备将在没有警告的情况下断开连接。

谢谢你的帮助!

BluetoothNotification.java: //这是创建通知的地方。

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(); 
}  
} 

段从OnCreate中://位于Controls.java

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

段从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); 
     } 

    } 

}; 
+0

伟大的工作和wounderful的问题,..和一个有趣的问题..谢谢,如果你有答案的意思,张贴它!..我也试图为瑞安先生 – gowri

回答

0

什么是你的广播接收器和你的活动之间的联系?

没有必要进行此项活动,请将所有内容放入广播接收器中以显示通知。

+0

所以你说没有必要创建BluetoothNotification.java文件?只需在“if”语句中放置通知的所有配置代码? –

+0

是的。活动的重点是什么? – Snicolas

+0

我是Android编程的新手,所以我将其用作示例。 –