android-BluetoothSocket
2016-07-19 13:40
363 查看
BluetoothSocket
public final class BluetoothSocket
extends Object
implements Closeable
java.lang.Object | |
↳ | android.bluetooth.BluetoothSocket |
连接或连接蓝牙socket。
The interface for Bluetooth Sockets is similar to that of TCP sockets:
Socketand
ServerSocket.
On the server side, use a
BluetoothServerSocketto create a listening
server socket. When a connection is accepted by the
BluetoothServerSocket,
it will return a new
BluetoothSocketto manage the connection. On the client
side, use a single
BluetoothSocketto both initiate an outgoing connection
and to manage the connection.
蓝牙Sockets 的接口是类似于TCP套接字:Socket 和ServerSocket。在服务器端,使用BluetoothServerSocket创建一个监听服务器Socket 。被BluetoothServerSocket接受连接时,它将返回一个新的BluetoothSocket管理连接。在客户端,使用单个BluetoothSocket发起一个即将离任的连接和管理连接。
The most common type of Bluetooth socket is RFCOMM, which is the type supported by the Android APIs. RFCOMM is a connection-oriented, streaming transport over Bluetooth. It is also known as the Serial Port Profile (SPP).
最常见的一种蓝牙 socket
是RFCOMM ,Android api支持的类型。蓝牙RFCOMM是面向连接的,流媒体传输。它也被称为串行端口配置文件(SPP)。
To create a
BluetoothSocketfor connecting to a known device, use
BluetoothDevice.createRfcommSocketToServiceRecord().
Then call
connect()to attempt a connection to the remote device.
This call will block until a connection is established or the connection fails.
创建一个BluetoothSocket为连接到一个已知的设备,使用BluetoothDevice.createRfcommSocketToServiceRecord()。然后调用connect()尝试连接到远程设备。这个调用会阻塞,直到建立连接或连接失败。
To create a
BluetoothSocketas a server (or "host"), see the
BluetoothServerSocketdocumentation.
创建一个BluetoothSocket服务器(或“主机”),查看BluetoothServerSocket文档。
Once the socket is connected, whether initiated as a client or accepted as a server, open the IO streams by calling
getInputStream()and
getOutputStream()in
order to retrieve
InputStreamand
OutputStreamobjects,
respectively, which are automatically connected to the socket.
一旦连接socket,是否启动客户机或接受为一个服务器,打开IO流通过调用getInputStream()andgetOutputStream()来检索InputStream和OutputStream对象,分别是自动连接到socket。
BluetoothSocketis thread safe. In particular,
close()will
always immediately abort ongoing operations and close the socket.
BluetoothSocket是线程安全的。特别是,close()总是会立即中止正在进行的操作并关闭socket。
Note: Requires the
BLUETOOTHpermission.
Developer Guides
For more information about using Bluetooth, read the Bluetooth developerguide.
See also:
BluetoothServerSocket
InputStream
OutputStream
Summary
| |
---|---|
int | TYPE_L2CAP L2CAP socket |
int | TYPE_RFCOMM RFCOMM socket |
int | TYPE_SCO SCO socket |
| |
---|---|
void | close() Closes this stream and releases any system resources associated with it. 关闭这个流并释放与之关联的任何系统资源。 |
void | connect() Attempt to connect to a remote device. 试图连接到一个远程设备。 |
int | getConnectionType() Get the type of the underlying connection. |
InputStream | getInputStream() Get the input stream associated with this socket. |
int | getMaxReceivePacketSize() Get the maximum supported Receive packet size for the underlying transport. |
int | getMaxTransmitPacketSize() Get the maximum supported Transmit packet size for the underlying transport. |
OutputStream | getOutputStream() Get the output stream associated with this socket. |
BluetoothDevice | getRemoteDevice() Get the remote device this socket is connecting, or connected, to. 获取远程设备这个socket连接,或连接。 |
boolean | isConnected() Get the connection status of this socket, ie, whether there is an active connection with remote device. 这个socket的连接状态,即是否存在一个已有远程设备的连接。 |
|
---|
From class java.lang.Object |
From interface java.io.Closeable |
From interface java.lang.AutoCloseable |
Constants
TYPE_L2CAP
Added in API level 23int TYPE_L2CAP
L2CAP socket
Constant Value: 3 (0x00000003)
TYPE_RFCOMM
Added in API level 23int TYPE_RFCOMM
RFCOMM socket
Constant Value: 1 (0x00000001)
TYPE_SCO
Added in API level 23int TYPE_SCO
SCO socket
Constant Value: 2 (0x00000002)
Public methods
close
Added in API level 5void close ()
Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.
关闭这个流并释放与之关联的任何系统资源。如果流已经关闭然后调用这个方法没有效果。
Throws | |
---|---|
IOException |
connect
Added in API level 5void connect ()
Attempt to connect to a remote device.
This method will block until a connection is made or the connection fails. If this method returns without an exception then this socket is now connected.
Creating new connections to remote Bluetooth devices should not be attempted while device discovery is in progress. Device discovery is a heavyweight procedure on the Bluetooth adapter and will significantly slow
a device connection. Use
cancelDiscovery()to cancel an
ongoing discovery. Discovery is not managed by the Activity, but is run as a system service, so an application should always call
cancelDiscovery()even
if it did not directly request a discovery, just to be sure.
close()can be used to
abort this call from another thread.
Throws | |
---|---|
IOException | on error, for example connection failure |
getConnectionType
Added in API level 23int getConnectionType ()
Get the type of the underlying connection.
Returns | |
---|---|
int | one of TYPE_RFCOMM, TYPE_SCOor TYPE_L2CAP |
getInputStream
Added in API level 5InputStream getInputStream ()
Get the input stream associated with this socket.
The input stream will be returned even if the socket is not yet connected, but operations on that stream will throw IOException until the associated socket is connected.
Returns | |
---|---|
InputStream | InputStream |
Throws | |
---|---|
IOException |
getMaxReceivePacketSize
Added in API level 23int getMaxReceivePacketSize ()
Get the maximum supported Receive packet size for the underlying transport. Use this to optimize the reads done on the input stream, as any call to read will return a maximum of this amount of bytes - or for some
transports a multiple of this value.
Returns | |
---|---|
int | the maximum supported Receive packet size for the underlying transport. |
getMaxTransmitPacketSize
Added in API level 23int getMaxTransmitPacketSize ()
Get the maximum supported Transmit packet size for the underlying transport. Use this to optimize the writes done to the output socket, to avoid sending half full packets.
Returns | |
---|---|
int | the maximum supported Transmit packet size for the underlying transport. |
getOutputStream
Added in API level 5OutputStream getOutputStream ()
Get the output stream associated with this socket.
The output stream will be returned even if the socket is not yet connected, but operations on that stream will throw IOException until the associated socket is connected.
Returns | |
---|---|
OutputStream | OutputStream |
Throws | |
---|---|
IOException |
getRemoteDevice
Added in API level 5BluetoothDevice getRemoteDevice ()
Get the remote device this socket is connecting, or connected, to.
Returns | |
---|---|
BluetoothDevice | remote device |
isConnected
Added in API level 14boolean isConnected ()
Get the connection status of this socket, ie, whether there is an active connection with remote device.
Returns | |
---|---|
boolean | true if connected false if not connected |
相关文章推荐
- GUI界面,客户端向服务端传送文件
- Android开发技术周报 Issue#63
- uiautomator 仿 WebDriver 封装,页面工厂制作,实现多语言定位
- Android开发技术周报 Issue#64
- Android开发技术周报 Issue#65
- Android开发技术周报 Issue#66
- Android开发技术周报 Issue#67
- Android开发技术周报 Issue#68
- Android开发技术周报 Issue#70++
- Android开发技术周报 Issue#69
- Android开发技术周报 Issue#70
- Android开发技术周报 Issue#71
- [leetcode] 232. Implement Queue using Stacks
- Android开发技术周报 Issue#72
- Android开发技术周报 Issue#73
- IOS学习笔记-UITableView绑定数据实例
- Android开发技术周报 Issue#74
- Android开发技术周报 Issue#75
- Android开发技术周报 Issue#76
- Android开发技术周报 Issue#77