Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data from paired Bluetooth devices in Android

I am working on an android 4.0 application which reads data from the paired bluetooth device. I am able to search discoverable bluetooth device and pair the devices. However, I am unsure of how to read data from Bluetooth device via serial port? Does android system support SERIAL PORT? Because I cannot find the serial port like COM1 or COM2 in the android system. At the moment, I am using the BluetoothSocket to pair the device. However, is there any way to read data from Bluetooth serial port like Windows does?

socket = _devices.get(k).createRfcommSocketToServiceRecord(MY_UUID_SECURE);
socket.connect();

Any helps appreciated! Thanks in adance.

Regards,

Charles

like image 578
Charles LAU Avatar asked Jun 12 '12 12:06

Charles LAU


People also ask

Can you transmit data through Bluetooth?

Most any type of file can be transferred over Bluetooth: documents, photos, videos, music, apps, and more. If a file is stored in a folder on a computer or smartphone, you can send it.

Can you scan for Bluetooth devices?

Put your scanner in discoverable mode. In the Bluetooth menu on your phone, scan for available Bluetooth devices. Your scanner should come up. Tap to pair with it.


1 Answers

Yes there is. The socket you receive can give you an InputStream instance. If the socket is connected, you can then read data (char, String or byte, depending on what reader you will wrap around your InputStream, if you wrap one).

To open a serial port with the device, you have to use the Serial Port Profile in the UUID you use to create your socket. A simple UUID thrown around on the web is

00001101-0000-1000-8000-00805F9B34FB

You can then create your socket, connect to it, get 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 your code you can read from the reader:

String aString = mBufferedReader.readLine();

And you can do the same thing in opposite direction using OutputStream and various writers.

like image 71
AntoineG Avatar answered Sep 18 '22 05:09

AntoineG