Android Bluetooth Serial Baud Rate

84
Android Bluetooth Serial Baud Rate Rating: 7,6/10 5075 votes

The Serial port Bluetooth module is fully qualified Bluetooth V2.0+EDR (Enhanced Data Rate) 3Mbps Modulation with complete 2.4GHz radio. Up to +4dBm RF transmit power; Low Power 1.8V Operation,1.8 to 3.6V I/O; PIO control; UART interface with programmable baud rate; Integrated antenna; Edge.

Summary: Constants Methods Inherited Methods

public final class BluetoothSocket
extends Object implements Closeable

java.lang.Object
android.bluetooth.BluetoothSocket

A connected or connecting Bluetooth socket.

The interface for Bluetooth Sockets is similar to that of TCP sockets: Socket and ServerSocket. On the server side, use a BluetoothServerSocket to create a listening server socket. When a connection is accepted by the BluetoothServerSocket, it will return a new BluetoothSocket to manage the connection. On the client side, use a single BluetoothSocket to both initiate an outgoing connection and to manage the connection.

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).

To create a BluetoothSocket for 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.

To create a BluetoothSocket as a server (or 'host'), see the BluetoothServerSocket documentation.

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 InputStream and OutputStream objects, respectively, which are automatically connected to the socket.

BluetoothSocket is thread safe. In particular, close() will always immediately abort ongoing operations and close the socket.

Note: Requires the Manifest.permission.BLUETOOTH permission.

Developer Guides

For more information about using Bluetooth, read the Bluetooth developer guide.

See also:

Summary

Constants

intTYPE_L2CAP

L2CAP socket

intTYPE_RFCOMM

RFCOMM socket

intTYPE_SCO

SCO socket

Public methods

voidclose()

Closes this stream and releases any system resources associated with it.

voidconnect()

Attempt to connect to a remote device.

intgetConnectionType()

Get the type of the underlying connection.

InputStreamgetInputStream()

Get the input stream associated with this socket.

intgetMaxReceivePacketSize()

Get the maximum supported Receive packet size for the underlying transport.

intgetMaxTransmitPacketSize()

Get the maximum supported Transmit packet size for the underlying transport.

OutputStreamgetOutputStream()

Get the output stream associated with this socket.

BluetoothDevicegetRemoteDevice()

Get the remote device this socket is connecting, or connected, to.

booleanisConnected()

Get the connection status of this socket, ie, whether there is an active connection with remote device.

Inherited methods

From class java.lang.Object
Objectclone()

Creates and returns a copy of this object.

booleanequals(Object obj)

Indicates whether some other object is 'equal to' this one.

voidfinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?>getClass()

Returns the runtime class of this Object.

inthashCode()

Returns a hash code value for the object.

final voidnotify()

Wakes up a single thread that is waiting on this object's monitor.

final voidnotifyAll()

Wakes up all threads that are waiting on this object's monitor.

StringtoString()

Returns a string representation of the object.

final voidwait(long timeout, int nanos)

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

final voidwait(long timeout)

Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

final voidwait()

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

From interface java.io.Closeable
abstract voidclose()

Closes this stream and releases any system resources associated with it.

From interface java.lang.AutoCloseable
abstract voidclose()

Closes this resource, relinquishing any underlying resources.

Constants

TYPE_L2CAP

L2CAP socket

Constant Value: 3 (0x00000003)

TYPE_RFCOMM

RFCOMM socket

Constant Value: 1 (0x00000001)

TYPE_SCO

SCO socket

Constant Value: 2 (0x00000002)

Public methods

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.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Throws
IOException

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 BluetoothAdapter#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 BluetoothAdapter#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
IOExceptionon error, for example connection failure

getConnectionType

Get the type of the underlying connection.

Returns
intone of TYPE_RFCOMM, TYPE_SCO or TYPE_L2CAP

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
InputStreamInputStream
Throws
IOException

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.

Android
Returns
intthe maximum supported Receive packet size for the underlying transport.

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
intthe maximum supported Transmit packet size for the underlying transport.
Android bluetooth serial

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
OutputStreamOutputStream
Throws
IOException

getRemoteDevice

Get the remote device this socket is connecting, or connected, to.

Returns
BluetoothDeviceremote device

isConnected

Android Bluetooth Serial Terminal

Get the connection status of this socket, ie, whether there is an active connection with remote device.

Android Bluetooth Serial Port

Returns
booleantrue if connected false if not connected