2013-02-18 113 views
0

我對Java很新,目前正在開發一個項目。我設法打開和關閉我的應用程序內的藍牙。我也有一個配對設備列表的列表視圖。我創建了一個「onListItemClick」方法。但是,我似乎無法弄清楚如何與配對設備之一建立連接。任何幫助,將不勝感激。謝謝。建立設備之間的藍牙連接

import java.util.Set; 
import android.app.ListActivity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class Devices extends ListActivity { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    ArrayAdapter<String> btArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 
    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    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) { 

    super.onListItemClick(l, v, position, id); 

     //Here, I want to establish a connection with the device chosen by the user 

} 


} 

回答

0

我做例子給你:

public class MainActivity extends Activity{ 
     private BluetoothAdapter mBluetoothAdapter; 
     private ArrayAdapter<String> mPairedDevicesArrayAdapter; 
     private ListView PairedListView; 
     private BluetoothService mBluetoothService = null; 

     public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout); 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     // Initialize ArrayAdapter for paired devices 
     mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name); 
     PairedListView = (ListView)findViewById(R.id.listView_paired_devices); 
     PairedListView.setAdapter(mPairedDevicesArrayAdapter); 
     PairedListView.setOnItemClickListener(this); 
     getPairedDevices(); 
     } 

     private void getPairedDevices(){ 
      // Get a set of currently paired devices 
      Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
      // If there are paired devices, add each one to the ArrayAdapter 
       if (pairedDevices.size() > 0) { 

        for (BluetoothDevice device : pairedDevices) { 
         // Add paired devices to ArrayAdapter 
         // ArrayAdapter will be connected to LisView 
         mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
         } 
       } else { 
        String noDevices = getResources().getText(R.string.title_none_paired).toString(); 
        mPairedDevicesArrayAdapter.add(noDevices); 
       } 


      } 


    } 

@Override 
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) { 
    // Cancel discovery because it's costly and we're about to connect 
    mBluetoothAdapter.cancelDiscovery(); 
    // Get the device MAC address, which is the last 17 chars in the View 
    String info = ((TextView) v).getText().toString(); 
    String adress = info.substring(info.length() -17); 

    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(adress); 
    // Attempt to connect to the given bluetooth device in separate thread 
    // Reason for this is because connecting and sending bytes are blocking calls 
    mBluetoothService.connect(device); 

} 


public class BluetoothService { 
    // Unique UUID for this application 
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
private ConnectThread mConnectThread; 
    private ConnectedThread mConnectedThread; 
    private BluetoothAdapter mBluetoothAdapter; 

    public synchronized void connect(BluetoothDevice device){ 
     // Start ConnectThread to connect to given device 
     mConnectThread = new ConnectThread(device); 
     mConnectThread.start(); 

    } 
     private class ConnectThread extends Thread { 
     private final BluetoothSocket mBluetoothSocket; 
     private final BluetoothDevice mBluetoothDevice; 

     public ConnectThread(BluetoothDevice device){ 
      // Use a temporary object that is later assigned to mmSocket, 
      // because mmSocket is final 
      mBluetoothDevice = device; 
      BluetoothSocket tmp = null; 
      // Get a BluetoothSocket to connect with the given BluetoothDevice 
      try{ 
       // MY_UUID is the app's UUID string 
       tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
      } catch (IOException e){ 

      } 
      mBluetoothSocket = tmp; 
     } 

     public void run(){ 
      // Because BuluetoothAdapter.discovery() is 
      // heavy weight procedure, cancel any on 
      // going discovery before attempting to connect 
      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
      mBluetoothAdapter.cancelDiscovery(); 
      // Connect the device through the socket. This is blocking call so it 
      // will return on a successful connection or an exception 

      // Make a connection to the BluetoothSocket 
      try { 
       // Connect the device through the socket. This is blocking call so it 
       // will return on a successful connection or an exception 
       mBluetoothSocket.connect(); 
      } catch (IOException e) { 
       try { 
        // Unable to connect, close the socket and get out 
        mBluetoothSocket.close(); 
        setState(STATE_CONNECTION_FAILED); 
       } catch (IOException e1) { 
        e1.printStackTrace(); 
       } 
      } 
      // Reset the ConnectThread because we're done 
      synchronized (BluetoothService.this) { 
       mConnectThread = null; 
      } 
      // Do work to manage the connection (in a separate thread) 
      if(mBluetoothSocket.isConnected()){ 
      connected(mBluetoothSocket, mBluetoothDevice); } 
     } 

     public void cancel(){ 
     // This method will cancel an in-progress connection and close the socket 
      try { 
       mBluetoothSocket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

非常感謝。我只是在嘗試,但它一直給出一個錯誤,說「BluetoothService無法解析爲某種類型」。 – 2013-02-18 16:12:34