2012-08-25 179 views
0

我無法弄清楚如何通過這個代碼獲得名單上我的綁定設備:Android的藍牙獲取綁定設備

bt_scan.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
      // If there are paired devices 
      if (pairedDevices.size() > 0) { 
       // Loop through paired devices 
       for (BluetoothDevice device : pairedDevices) { 
        // Add the name and address to an array adapter to show in a ListView 
        mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
       } 
      } 

                 }});} 

我不知道如何得到它在文本視圖或列表如果可能的話,查看一個相同的佈局,甚至在另一個佈局。 我想連接一個找到的設備,但這是一個完整的其他故事。

(這裏是我完整的代碼,如果有幫助:http://pastie.org/4582924

非常感謝!

+0

如果只想將arrayAdapter調查結果顯示到ListView中,只需將arrayAdapter設置爲listView控件即可。我做了一個關於通過適配器向列表視圖添加項目的簡單視頻,http://www.youtube.com/watch?v = 61WvMzaihwU&feature = plcp,您可以跳到第4分鐘或更少。 – soynerdito

回答

0

是的,我同意Vishwa,藍牙聊天應用程序是非常有幫助的,我都做相同的(像你這樣)樣的發展,這裏是我的代碼,

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:id="@+id/title_new_devices" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:background="#666" android:textColor="#fff" 
     android:paddingLeft="5dp" /> 
    <ListView android:id="@+id/new_devices" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

活性類

private static BluetoothAdapter mBtAdapter; 
private static ArrayAdapter<String> mNewDevicesArrayAdapter; 

private static ArrayList<String> nameList = null; 
private static ArrayList<String> macList = null; 

private static ListView newDevicesListView = null; 



     // Register for broadcasts when a device is discovered 
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     this.registerReceiver(mReceiver, filter); 

     // Register for broadcasts when discovery has finished 
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(mReceiver, filter); 


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

     // Get the local Bluetooth adapter 
     mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 

廣播接收器類

private final BroadcastReceiver mReceiver = new BroadcastReceiver() 
    { 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      try 
      { 
       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); 

        String deviceName = device.getName(); 
        String deviceAddress = device.getAddress(); 

        if (!nameList.contains(deviceName.trim() + " " + deviceAddress.trim())) 
        { 
         nameList.add(deviceName.trim() + " " + deviceAddress.trim()); 
         macList.add(deviceAddress.trim() + "&" + ConstantCodes.TIME + ConstantCodes.EQUALS + DateUtility.getDateTime()); 
        } 

//     System.out.println (device.getName()); 
        mNewDevicesArrayAdapter.notifyDataSetChanged(); 
       } 
       else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) 
       { 
        mNewDevicesArrayAdapter.notifyDataSetChanged(); 
       } 
       else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) 
       { 
//     nameList.clear(); 
       } 
      } 
      catch (Exception e) 
      { 
       System.out.println ("Broadcast Error : " + e.toString()); 
      } 
     } 
    }; 

代碼將搜索活動的藍牙設備並將其顯示在listView中。

0

查看Android SDK中提供的藍牙聊天應用程序,它提供了一種乾淨的方式來顯示您當前與之配對的手機。

它還提供了一個BluetoothChatService類,您可以輕鬆地適應連接找到的設備。