background image

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

    

public

 AcceptThread() {  

        

// Use a temporary object that is later assigned to mmServerSocket,  

        

// because mmServerSocket is final  

        BluetoothServerSocket tmp = 

null

;  

        

try

 {  

            

// MY_UUID is the app's UUID string, also used by the client code  

            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);  
        } 

catch

 (IOException e) { }  

        mmServerSocket = tmp;  
    }  
   
    

public

 

void

 run() {  

        BluetoothSocket socket = 

null

;  

        

// Keep listening until exception occurs or a socket is returned  

        

while

 (

true

) {  

            

try

 {  

                socket = mmServerSocket.accept();  
            } 

catch

 (IOException e) {  

                

break

;  

            }  
            

// If a connection was accepted  

            

if

 (socket != 

null

) {  

                

// Do work to manage the connection (in a separate thread)  

                manageConnectedSocket(socket);  
                mmServerSocket.close();  
                

break

;  

            }  
        }  
    }  
   
    /** Will cancel the listening socket, and cause the thread to finish */  
    

public

 

void

 cancel() {  

        

try

 {  

            mmServerSocket.close();  
        } 

catch

 (IOException e) { }  

    }  
}  

客户端的实现
通过搜索得到服务器端的 BluetoothService

                     找软件资料,就到一览软件文库

http://wk.yl1001.com/rj/