Preface
This chapter is about android. bluetooth. javasthserversocket, which is a part of Android bluetooth. Server Communication socket, similar to TCP ServerSocket. Android 2.3 r1, translated from Sun Yat-sen University's "Android Club SYSU", welcome to visit their WIKI: http://www.android-wiki.net, thanks again "Android Club SYSU "! I look forward to your participation in Android API Chinese translation, contact me over140@gmail.com.
Statement
You are welcome to repost, but please keep the original source of the article :)
Android Club SYSU: http://www.android-wiki.net
Blog Garden: http://www.cnblogs.com/
Android Chinese translation group: http://code.taobao.org/project/view/404/
Body
I. Structure
Public final class implements thserversocket extends Object implements Closeable
Java. lang. Object
Android. bluetooth. javasthserversocket
Ii. Overview
A Bluetooth listening port.
The Bluetooth port listening interface is similar to the TCP port: Socket and ServerSocket. On the server side, use the descrithserversocket class to create a listening service port. When a connection is accepted by rabbitthserversocket, it returns a new rabbitthsocket to manage the connection. On the client side, use a separate javasthsocket class to initialize an external connection and manage the connection.
The most common Bluetooth port is RFCOMM, which is supported by Android APIs. RFCOMM is a connection-oriented data stream transmission mode through the bluetooth module. It is also known as the Serial Port specification (Serial Port Profile, SPP ).
To create a listener for the new connectionBluetoothServerSocketClass, useBluetoothAdapter.listenUsingRfcommWithServiceRecord()Method. Then callaccept()Method to monitor the request for this link. Before the connection is established, the call is blocked, that is, it returnsBluetoothSocketClass to manage the connection. If you no longer need to accept the connection after obtaining the class, it is best to callBluetoothServerSocketClassclose()Method. CloseBluetoothServerSocketThe class does not close this returnedBluetoothSocketClass.
BluetoothSocketClass thread security. Special,close()The method will immediately discard external operations and close the server port.
Note: YesBLUETOOTHPermission.
See
BluetoothSocket
Iii. Public Methods
Public synchronized thsocketaccept (int timeout)
Blocked until the connection is established within the timeout period.
Return a connectedthsocket class on a established connection.
Each time the call returns, it can call it to receive new connections.
close()Methods can be used to discard calls from another thread.
Parameters
Timeout)
Return Value
Connected javasthsocket
Exception
An error occurs in IOException, such as the call being abandoned or timeout.
Public ipvthsocket accept ()
Blocking until a connection is established. (Translator's note: The default timeout value is-1. See the source code)
Return a connectedthsocket class on a established connection.
Each time the call returns, it can call it to receive new connections.
close()Methods can be used to discard calls from another thread.
Return Value
Connected javasthsocket
Exception
An error occurs in IOException, such as the call being abandoned or timeout.
Public voidClose()
Close the port immediately and release all related resources.
The system immediately throws an IO exception because the port is blocked by other threads.
Disabling javasthserversocket does not close any javasthsocket that accepts self-accept.
Exception
IOException