2012-07-06 57 views
1

IM在我的最後一個項目的工作爲我的拼貼和IM卡住尋找一個android.i藍牙通知代碼藍牙通知代碼所需要的代碼來通知我,每當我的藍牙打開或關閉爲Android

+0

沒有ü嘗試做一個谷歌搜索呢? – 2012-07-06 06:14:28

+0

是啊我google了但沒有出現 – 2012-07-06 06:16:39

回答

1

您需要在您的代碼中添加廣播接收器,以便在您的活動課程中開啓或關閉藍牙。供大家參考:

public void onCreate() { 
... 
    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); 
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED); 
    IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED); 
    this.registerReceiver(mReceiver, filter1); 
    this.registerReceiver(mReceiver, filter2); 
    this.registerReceiver(mReceiver, filter3); 
} 

//The BroadcastReceiver that listens for bluetooth broadcasts 

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_FOUND.equals(action)) { 
     ... //Device found 
    } 
    else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) { 
     ... //Device is now connected 
    } 
    else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
     ... //Done searching 
    } 
    else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) { 
     ... //Device is about to disconnect 
    } 
    else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) { 
     ... //Device has disconnected 
    }   
} 
+0

感謝您的代碼,但我需要它做的是在通知欄中添加一條聲明,說藍牙處於開啓或關閉狀態或正在使用中 – 2012-07-06 06:34:05

+0

確定那麼一旦您知道了是它打開或關閉,你可以添加通知狀態..對嗎?或者你不知道如何添加通知欄本身? – 2012-07-06 06:36:48

+0

實際上,我們正在接受第四年的基礎知識教育,現在我們基本上必須依靠網絡獲取代碼和信息 – 2012-07-06 07:10:05

2
OK then do it like that when you bluetooth is on or off 


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_FOUND.equals(action)) { 

int icon = R.drawable.nishansahib1;  // icon from resources 
      CharSequence tickerText = "SatShreeAkal";    // ticker-text 
      long when = System.currentTimeMillis();   // notification time 
      //Context context = getApplicationContext();  // application Context 
      CharSequence contentTitle = ""; // message title 
//   

      CharSequence contentText= "YPUR BLUETOOTH IS ON OR OFF";  // message text 
      final int NOTIF_ID = 1234; 
      Intent notificationIntent = new Intent(context, your classname);//if u want to call a class 

      notificationIntent.putExtra("DISPLAY",contentText);//if u want to pass intent 
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
      NotificationManager notofManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE); 
      // the next two lines initialize the Notification, using the configurations above 
      Notification notification = new Notification(icon, tickerText, when); 
      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

     notification.defaults = Notification.DEFAULT_SOUND; 
     notofManager.notify(NOTIF_ID,notification); 
}