2012-09-03 34 views
0

我試圖建立Android和藍牙模塊之間的連接。 「發現」後,我按照http://developer.android.com/guide/topics/connectivity/bluetooth.html 中的說明將找到的設備放入ListView中,該ListView的註冊號爲OnItemClickListener從設置獲取BluetoothDevices

我現在的問題是,我需要點擊條目中的BluetoothDevice來建立下一步的連接。我所有的是ListView中的位置ID和Set<BluetoothDevice>。但我不知道如何從Set中提取特定的BluetoothDevice

這是我的代碼到目前爲止。 謝謝!

ProgressBar spinWheel; 
ListView devList; 
BluetoothAdapter btAdapter; 
ArrayAdapter<String> lvAdapter; 
BroadcastReceiver mReceiver; 
Set<BluetoothDevice> pairedDevices; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.searchlist); 
    spinWheel = (ProgressBar)findViewById(R.id.progressBar1); 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    devList = (ListView)findViewById(R.id.devicelist); 
    lvAdapter = new ArrayAdapter<String>(getApplicationContext(), 
      R.layout.simplerow, R.id.simpleRow); 
    devList.setAdapter(lvAdapter); 
    devList.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parentView, View childView, int position, 
       long id) { 
      //establish Connection, need the clicked BluetoothDevice 
     } 
    }); 

    //gepaarte Geräte in die ListView 
    pairedDevices = btAdapter.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 
      lvAdapter.add(device.getName() + "\n"+ "#"+ device.getAddress()); 
     } 
    } 

    // Create a BroadcastReceiver for ACTION_FOUND 
     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 
       lvAdapter.add(device.getName() + "\n" + device.getAddress()); 
       pairedDevices.add(device); 
      } 
     } 
    }; 
    // Register the BroadcastReceiver 
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy 
    btAdapter.startDiscovery(); 
    } 

@Override 
public void onDestroy(){ 
    super.onDestroy(); 
    unregisterReceiver(mReceiver); 
} 

} 

回答

1

我自己解決了這個問題,但我想發佈它來幫助別人。 我使用了函數BluetoothAdapter.getRemoteDevice(String adress)。 作爲地址插入ListView中的字符串,分割爲「#」。

public void onItemClick(AdapterView<?> parentView, View childView, int position, 
       long id) { 
      String[] separated = lvAdapter.getItem(position).split("#"); 
      if(btAdapter.checkBluetoothAddress(separated[1])==true){ 
      devtoconnect = btAdapter.getRemoteDevice(separated[1]); 
      } 
     } 
    }); 

不過感謝您在其他問題上的幫助!