2012-06-01 345 views

回答

0

是的,這是非常可能的

第一步你需要創建一個服務

第二步你需要BluetoothDevice.ACTION_FOUND廣播接收機尋找附近的設備。

第3步然後你就可以查詢全部由一個找到的設備之一。

第4步正如你將快速枚舉發現設備轉儲文件裏面的信息。

下面是廣播接收機

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       // Add the name and address to an array adapter to show in a ListView 
       // You will log this information into file. 
       mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 
     } 
    }; 

註冊的廣播接收器的意圖行動如下

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy 

希望這有助於。

1

是的,你的服務可以偵聽新的藍牙設備,如VIPUL沙阿描述,但真正的問題是你如何使你的設備找到擺在首位其它藍牙設備。當遠程設備發現期間找到

ACTION_FOUND被髮送。您可以撥打BluetoothAdapter.startDiscovery()來啓動發現過程,但問題是通常可發現的設備非常少。幾年前,通常設備始終保持可發現狀態,但現在,用戶需要根據需要暫時發現設備以配對設備。

所以,它沒有任何意義有周期性確實發現一個服務(和監聽ACTION_FOUND),不僅因爲它消耗了大量的電池,並因爲你不會找到任何東西。

如果你知道你正在尋找的,那麼你可以嘗試連接到這些設備的藍牙地址,但我認爲不是這樣的。