2013-02-27 235 views
7

我剛開始通過我的Android手機連接到嵌入式BT設備。它連接良好,但我正面臨問題,因爲我沒有正確斷開連接。我的意思是先關閉socket然後打開任何I/O流。從Android設備連接到嵌入式藍牙設備

但是,當我在設備中突然關閉藍牙時,我將如何知道藍牙正在斷開連接。是否有任何方式在APP中一直接收藍牙斷開監聽器。

任何想法....? 感謝

mmSocket= device.createRfcommSocketToServiceRecord(uuidToTry); 
try 
{ 
    mmSocket.connect(); 
} 
catch (IOException e) 
{ 
    mmSocket.close(); 
} 

回答

1

你可以像這 - 在代碼中創建一個廣播接收器就像這樣:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 
      BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
      if (!mBluetoothAdapter.isEnabled()) { 
       // BT is turened off. 
      } 
      else{ 
       // BT is turened on. 
       } 
     } 
} 
}; 

並註冊的BroadcastReceiver爲以下意圖過濾器:

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mReceiver, filter);

+0

感謝shridutt我正在尋找這個。非常感謝 – praveen 2013-02-27 10:36:07

+0

@praveen歡迎好友... – 2013-02-27 10:38:36