Yes there is. The resulting socket
can provide you with an instance of InputStream
. If a socket
connected, you can read data ( char
, String
or byte
, depending on which reader you wrap around your InputStream
, if you wrap it).
To open the serial port with the device, you must use the serial port profile in the UUID that you use to create your socket. Simple UUID thrown on the Internet,
00001101-0000-1000-8000-00805F9B34FB
Then you can create your socket, connect to it, receive streams and read / write bytes with them. Example:
private static final String UUID_SERIAL_PORT_PROFILE = "00001101-0000-1000-8000-00805F9B34FB"; private BluetoothSocket mSocket = null; private BufferedReader mBufferedReader = null; private void openDeviceConnection(BluetoothDevice aDevice) throws IOException { InputStream aStream = null; InputStreamReader aReader = null; try { mSocket = aDevice .createRfcommSocketToServiceRecord( getSerialPortUUID() ); mSocket.connect(); aStream = mSocket.getInputStream(); aReader = new InputStreamReader( aStream ); mBufferedReader = new BufferedReader( aReader ); } catch ( IOException e ) { Log.e( TAG, "Could not connect to device", e ); close( mBufferedReader ); close( aReader ); close( aStream ); close( mSocket ); throw e; } } private void close(Closeable aConnectedObject) { if ( aConnectedObject == null ) return; try { aConnectedObject.close(); } catch ( IOException e ) { } aConnectedObject = null; } private UUID getSerialPortUUID() { return UUID.fromString( UUID_SERIAL_PORT_PROFILE ); }
And then, somewhere in the code, you can read from the reader:
String aString = mBufferedReader.readLine();
And you can do the same in the opposite direction using OutputStream
and various authors.
source share