2012-03-01 80 views
0

我通過BT將多個設備連接到服務器設備。 我有一些UUID以便能夠連接。查找用於BT套接字的UUID

但是,有時一個設備將斷開連接,我需要使服務器可發現並使用設備最初使用的相同UUID。這樣我可以確定該UUID是可用的。

有什麼方法可以知道用什麼UUID連接到設備?

回答

0

我明白你的問題,但恐怕Android SDK不公開一種方法來獲取與BluetoothSocket相關聯的內部UUID,儘管如果你在Android源代碼中看到,BluetoothSocket知道關聯的UUID。

您可以創建多個BluetoothServerSockets,以偵聽不同的UUID。你想要的只是一個不會失去由給定的BluetoothServerSocket上的accept()調用創建的BluetoothSocket之間的關聯的問題。因爲當你創建ServerSocket時,你知道UUID。

public class IncommingConnectionsForAGivenUUID extends Thread 
{ 
    private BluetoothServerSocket serverSocket; 
    private UUID serviceUUID; 
    private boolean isRunning; 

    public IncommingConnectionsForAGivenUUID(BluetoothAdapter btAdapter, UUID serviceUUID) throws IOException 
    { 
     this.serverSocket = btAdapter.listenUsingRfcommWithServiceRecord("localBtName", serviceUUID); 
     this.serviceUUID = serviceUUID; 
    } 

    @Override 
    public void run() 
    { 
     this.isRunning = true; 

     while(this.isRunning) 
     { 
     BluetoothSocket socket = this.serverSocket.accept(); 

     // do something with the socket... 

     // this socket will have the UUID passed in 
     // the constructor. So in this point you can associate 
     // this socket with the serviceUUID variable without 
     // having to call a method from the BluetoothSocket 
     } 
    } 
} 

好吧,這不是唯一的解決方案,但如果你想與接受給定的ServerSocket的BluetoothSockets的UUID關聯這是一個可能的解決方案。


每次你要與接收連接另一UUID時只需調用這個線程。

希望它有幫助^^'