2013-12-09 61 views
1

有沒有辦法在Android中ping通藍牙設備?藍牙設備未在Android中連接或配對,但我事先知道設備的MAC地址和PIN。我試圖實現的是ping一個MAC地址列表,看是否有任何設備在範圍內。Ping Android中的藍牙設備

+0

我不認爲MAC地址是適用於藍牙。一般的硬件地址似乎是。我現在只是想大聲點。 – keyser

+0

你描述的內容聽起來很像藍牙發現。當您收到有關可發現設備的信息時,會得到名稱和MAC地址。如果您提前知道MAC地址,則可嘗試連接到該地址,但每個設備都會有延遲。 – BitBank

+0

我正在考慮比連接它更快的方式,就像一個簡單的ping。 – alexb

回答

3

已解決:我所做的是查詢設備上可用的服務(可用的UUID)。如果收到UUID,則設備處於範圍內。

所以步驟是:

  • 註冊爲UUID行動

    String action = "android.bluetooth.device.action.UUID"; 
    IntentFilter filter = new IntentFilter(action); 
    registerReceiver(mReceiver, filter); 
    
  • 創建基於遠程地址藍牙設備並獲取它的UUID

    BluetoothDevice bd = bluetoothAdapter.getRemoteDevice(address); 
    bd.fetchUuidsWithSdp(); 
    
  • 接收到廣播
  • 創建一個廣播接收器,它攜帶的設備E地址,能告訴我,那

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
        public void onReceive(Context context, Intent intent) { 
         //deviceExtra is our in range device 
         deviceExtra = 
         intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE"); 
         Parcelable[] uuidExtra = 
         intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID"); 
    }}}; 
    
+1

我試過這個,但即使對於不在線但已配對的設備,我也會獲得uuids – Calvin

+1

正如Calvin注意到的,這不起作用。如果連接失敗,緩存的uuids將與意圖一起發送。 http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#fetchUuidsWithSdp() –