2013-02-19 5 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

죄송합니다. –

+0

나는 당신이하려고하는 것을 얻지 못한다 – njzk2

+0

나는 당신의 질문에서 해결책을 얻었습니다. – Aravin

답변

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

감사합니다. 방금 시도했지만 차이는 없습니다. 목록보기에서 선택한 장치가 연결되지 않습니다. –

+0

누구를 도와주세요? 부디? 나는 이것을 정말로 고수했다. : –

+0

죄송합니다, 코드에서 실수를했습니다. 정보를 추가하기 전에 ListAdapter를 설정하고 for 루프가 완료된 후 getPairedDevices() 메서드에서 목록보기를 새로 고쳐야합니다. – jonbonazza

관련 문제