2013-02-19 43 views
2

我試圖從列表視圖連接到配對的藍牙設備,但我似乎無法做到正確。我的代碼看起來很好,但是當我在手機上運行它時,它什麼都不做。任何幫助將不勝感激。謝謝。從列表視圖連接到藍牙設備

import java.io.IOError; 
import java.io.IOException; 
import java.io.InvalidObjectException; 
import java.util.Set; 
import java.util.UUID; 
import android.app.ListActivity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class Devices extends ListActivity { 

    private ArrayAdapter<String> btArrayAdapter; 
    private BluetoothAdapter btAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     btArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 
     btAdapter = BluetoothAdapter.getDefaultAdapter(); 
     getPairedDevices(); 
    } 


    private void getPairedDevices() { 

Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices(); 
if(pairedDevices.size()>0){ 
    for (BluetoothDevice device :pairedDevices){ 
     String name = device.getName(); 
     btArrayAdapter.add(name); 
    } 
} 

setListAdapter(btArrayAdapter); 
    } 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

Set<BluetoothDevice> device = btAdapter.getBondedDevices(); 
//System.out.println(device.getClass()); 

Thread ConnectThread = new Thread(); 
ConnectThread.start(); 


    } 

    public class ConnectThread extends Thread{ 

private final BluetoothSocket mmSocket; 
private final BluetoothDevice mmDevice; 
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

public ConnectThread(BluetoothDevice device){ 
    BluetoothSocket tmp = null; 
    mmDevice = device; 

    try{ 
     tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
    } catch (IOException e){} 
    mmSocket = tmp; 
} 

public void run(){ 
    btAdapter.cancelDiscovery(); 

    try{ 
     mmSocket.connect(); 
    }catch (IOException connectException){ 

     try{ 
      mmSocket.close(); 
     }catch (IOException closeException){} 
     return; 

    } 

    //Work to manage connection 
} 

public void cancel(){ 
    try{ 
     mmSocket.close(); 
    }catch (IOException e){} 
} 
} 





} 
+0

很抱歉的代碼如何凌亂是。 – 2013-02-19 16:33:24

+0

我不明白你想要做什麼 – njzk2 2013-02-20 17:28:51

+0

謝謝你問我這個問題的解決方案。 – Aravin 2013-08-23 17:17:19

回答

2

在您的onCreate()方法中,您需要爲您的ListView設置適配器。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    btArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    setListAdapter(btArrayAdapter); 
    getPairedDevices(); 
} 
+0

謝謝。我剛剛嘗試過,但沒有任何區別。在列表視圖中選擇的設備不會連接。 – 2013-02-19 16:42:53

+0

幫助任何人?請?我真的堅持這一點。 :( – 2013-02-19 18:10:52

+0

抱歉,我在代碼中犯了一個錯誤,在添加信息之前應該設置ListAdapter。另外,在getPairedDevices()方法中,在for循環完成後,您應該刷新列表視圖 – jonbonazza 2013-02-20 17:28:12