2015-02-09 227 views
1

我想将Nexus 7设备连接到Nexus 4设备,稍后我想将Nexus 7设备连接到微控制器。 我是否必须知道我的设备的UUID才能使用蓝牙连接它们?蓝牙:我需要知道设备的UUID吗?

当我配对设备时UUID是否被交换?

如果是:为什么在android例子中有定义的UUID?

public class BluetoothService extends Thread { 
    private static final String TAG = BluetoothService.class.getSimpleName(); 
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    public static final int STATE_NONE = 0; 
    public static final int STATE_LISTEN = 1; 
    public static final int STATE_CONNECTING = 2; 
    public static final int STATE_CONNECTED = 3; 
    private BluetoothAdapter bluetoothAdapter = null; 
    private Handler handler = null; 
    private ConnectThread connectThread = null; 
    private ConnectedThread connectedThread = null; 
    private int bluetoothState = STATE_NONE; 

    public BluetoothService(Handler handler) { 
     this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     this.bluetoothState = STATE_NONE; 
     this.handler = handler; 
    } 

    public synchronized void startConnection() { 
     Log.d(TAG, "start"); 

     if (this.connectThread != null) { 
      this.connectThread.cancel(); 
      this.connectThread = null; 
     } 

     if (this.connectedThread != null) { 
      this.connectedThread.cancel(); 
      this.connectedThread = null; 
     } 

     this.setBluetoothState(STATE_LISTEN); 
    } 

    public synchronized void connect(BluetoothDevice device) { 
     if (this.bluetoothState == STATE_CONNECTING) { 
      if (this.connectThread != null) { 
       this.connectThread.cancel(); 
       this.connectThread = null; 
      } 
     } 

     if (this.connectedThread != null) { 
      this.connectedThread.cancel(); 
      this.connectedThread = null; 
     } 

     this.connectThread = new ConnectThread(device); 
     this.connectThread.start(); 

     this.setBluetoothState(STATE_CONNECTING); 
    } 

    public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) { 
     if (this.connectThread != null) { 
      this.connectThread.cancel(); 
      this.connectThread = null; 
     } 

     if (this.connectedThread != null) { 
      this.connectedThread.cancel(); 
      this.connectedThread = null; 
     } 

     this.connectedThread = new ConnectedThread(socket); 
     this.connectedThread.start(); 

     Message msg = this.handler.obtainMessage(Globals.MESSAGE_DEVICE_NAME); 
     Bundle bundle = new Bundle(); 

     bundle.putString(Globals.DEVICE_NAME, device.getName()); 
     msg.setData(bundle); 

     this.handler.sendMessage(msg); 
     this.setBluetoothState(STATE_CONNECTED); 
    } 

    public synchronized void stopConnection() { 
     if (this.connectThread != null) { 
      this.connectThread.cancel(); 
      this.connectThread = null; 
     } 

     if (this.connectedThread != null) { 
      this.connectedThread.cancel(); 
      this.connectedThread = null; 
     } 

     this.setBluetoothState(STATE_NONE); 
    } 

    public void write(byte[] out) { 
     ConnectedThread connectedThread = null; 

     synchronized (this) { 
      if (this.bluetoothState != STATE_CONNECTED) { 
       return; 
      } 

      connectedThread = this.connectedThread; 
     } 

     connectedThread.write(out); 
    } 

    private void connectionFailed() { 
     Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST); 

     Bundle bundle = new Bundle(); 
     bundle.putString(Globals.TOAST, "Unable to connect device"); 

     msg.setData(bundle); 

     this.handler.sendMessage(msg); 

     BluetoothService.this.startConnection(); 
    } 

    private void connectionLost() { 
     Message msg = this.handler.obtainMessage(Globals.MESSAGE_TOAST); 
     Bundle bundle = new Bundle(); 

     bundle.putString(Globals.TOAST, "Device connection was lost"); 
     msg.setData(bundle); 

     this.handler.sendMessage(msg); 

     BluetoothService.this.startConnection(); 
    } 

    public synchronized int getBluetoothState() { 
     return this.bluetoothState; 
    } 

    private synchronized void setBluetoothState(int bluetoothState) { 
     this.bluetoothState = bluetoothState; 
    } 

    private class ConnectThread extends Thread { 
     private BluetoothSocket bluetoothSocket = null; 
     private BluetoothDevice bluetoothDevice = null; 

     public ConnectThread(BluetoothDevice bluetoothDevice) { 
      this.bluetoothDevice = bluetoothDevice; 

      BluetoothSocket tempBluetoothSocket = null; 

      try { 
       tempBluetoothSocket = this.bluetoothDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
      } catch (IOException e) { 
       Log.e(TAG, "Socket Type: " + "create() failed", e); 
      } 

      this.bluetoothSocket = tempBluetoothSocket; 
     } 

     public void run() { 
      Log.i(TAG, "BEGIN mConnectThread"); 

      this.setName("ConnectThread"); 

      bluetoothAdapter.cancelDiscovery(); 

      try { 
       this.bluetoothSocket.connect(); 
      } catch (IOException e) { 
       Log.e(TAG, e.getMessage(), e); 

       connectionFailed(); 

       try { 
        this.bluetoothSocket.close(); 
       } catch (IOException e2) { 
        Log.e(TAG, "unable to close() socket during connection failure", e2); 
       } 

       return; 
      } 

      synchronized (BluetoothService.this) { 
       connectThread = null; 
      } 

      connected(this.bluetoothSocket, this.bluetoothDevice); 
     } 

     public void cancel() { 
      try { 
       this.bluetoothSocket.close(); 
      } catch (IOException e) { 
       Log.e(TAG, "close() of connect socket failed", e); 
      } 
     } 
    } 

    private class ConnectedThread extends Thread { 
     private BluetoothSocket bluetoothSocket = null; 
     private InputStream inputStream = null; 
     private OutputStream outputStream = null; 

     public ConnectedThread(BluetoothSocket bluetoothSocket) { 
      Log.d(TAG, "create ConnectedThread"); 

      this.bluetoothSocket = bluetoothSocket; 

      InputStream tempInputStream = null; 
      OutputStream tempOutputStream = null; 

      try { 
       tempInputStream = this.bluetoothSocket.getInputStream(); 
       tempOutputStream = this.bluetoothSocket.getOutputStream(); 
      } catch (IOException e) { 
       Log.e(TAG, "temp sockets not created", e); 
      } 

      this.inputStream = tempInputStream; 
      this.outputStream = tempOutputStream; 
     } 

     public void run() { 
      byte[] buffer = new byte[1024]; 
      int bytes = 0; 

      while (true) { 
       try { 
        bytes = this.inputStream.read(buffer); 

        handler.obtainMessage(Globals.MESSAGE_READ, bytes, -1, buffer).sendToTarget(); 
       } catch (IOException e) { 
        Log.e(TAG, "disconnected", e); 

        connectionLost(); 

        BluetoothService.this.start(); 

        break; 
       } 
      } 
     } 

     public void write(byte[] buffer) { 
      try { 
       this.outputStream.write(buffer); 

       handler.obtainMessage(Globals.MESSAGE_WRITE, -1, -1, buffer).sendToTarget(); 
      } catch (IOException e) { 
       Log.e(TAG, "Exception during write", e); 
      } 
     } 

     public void cancel() { 
      try { 
       this.bluetoothSocket.close(); 
      } catch (IOException e) { 
       Log.e(TAG, "close() of connect socket failed", e); 
      } 
     } 
    } 
} 

如果否:如何交换UUID以连接设备?

+0

是对于配对而言,我无法自动完成工作。您必须在应用程序周围显示可见的蓝牙设备列表,用户需要从中选择一个。 – sUndeep 2015-02-09 12:24:46

+0

所以配对确保uuid的交换发生? – Mulgard 2015-02-09 12:25:46

回答

2

在谷歌样品/示例中定义的UUID必须由服务器和客户端是已知的:

  • 服务器创建一个RFCOMM的ServerSocket用于侦听与此UUID传入连接
  • 客户端创建RFcomm bluetoothsocket将尝试连接到服务器插座

如果uuids匹配连接建立。

配对保存有关远程设备(名称,ADRESS,...等)的信息,这样,当你想再次连接,你不必搜索设备,让他们做=)

+0

好吧,但比我不明白我可以如何连接到不同的设备。我的Nexus 7平板电脑正在打开连接(只有!不会有其他设备试图打开连接),nexus 7服务器或客户端也是如此?后来我想控制两个不同的微控制器,他们都有一个修复UUID,他们不相等。那么当我不能使用相同的UUID时,我怎么能控制两个微控制器呢? – Mulgard 2015-02-09 16:38:23

+0

对于Nexus 7,打开连接并等待其他设备的设备是服务器。实际上你使用serverSocket。为了连接到几个微控制器,服务器必须知道UUID列表并为每个控制器创建一个ListenningThread。每个线程使用不同的uuid创建一个serverSocket。当连接你的微控制器将尝试连接每个UUID,直到一个匹配(因为你不知道哪一个将被采取第一) – 2015-02-09 17:17:58

+0

谢谢1000倍。那个话题在最后的日子里让我感到害怕。 – Mulgard 2015-02-09 17:24:27