0% found this document useful (0 votes)
33 views1 page

Bluetooth

The BluetoothChatService class handles Bluetooth communication between two devices using three threads - Accept, Connecting, and Connected. When start() is called, it begins listening for connections using the Accept thread. connect() establishes a connection using the Connecting thread and passes the connected devices to the Connected thread, which obtains input/output streams to send and receive messages by writing to and reading from the streams. Messages are sent using the write() method and received by reading the input stream in a loop.

Uploaded by

sri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views1 page

Bluetooth

The BluetoothChatService class handles Bluetooth communication between two devices using three threads - Accept, Connecting, and Connected. When start() is called, it begins listening for connections using the Accept thread. connect() establishes a connection using the Connecting thread and passes the connected devices to the Connected thread, which obtains input/output streams to send and receive messages by writing to and reading from the streams. Messages are sent using the write() method and received by reading the input stream in a loop.

Uploaded by

sri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

The thing is all you need is a class BluetoothChatService.

java
this class has following threads:

1. Accept
2. Connecting
3. Connected
Now when you call start function of the BluetoothChatService like:

mChatService.start();
It starts accept thread which means it will start looking for connection.

Now when you call

mChatService.connect(<deviceObject>,false/true);
Here first argument is device object that you can get from paired devices list or when
you scan for devices you will get all the devices in range you can pass that object to this
function and 2nd argument is a boolean to make secure or insecure connection.

connect function will start connecting thread which will look for any device which is
running accept thread.
When such a device is found both accept thread and connecting thread will call
connected function in BluetoothChatService:

connected(mmSocket, mmDevice, mSocketType);


this method starts connected thread in both the devices: Using this socket object
connected thread obtains the input and output stream to the other device. And
calls read function on inputstream in a while loop so that it's always trying read from
other device so that whenever other device send a message this read function returns
that message.
BluetoothChatService also has a write method which takes byte[] as input and calls
write method on connected thread.
mChatService.write("your message".getByte());
write method in connected thread just write this byte data to outputsream of the other
device.

public void write(byte[] buffer) {


try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
// mHandler.obtainMessage(
// BluetoothGameSetupActivity.MESSAGE_WRITE, -1, -1,
// buffer).sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
Now to communicate between two devices just call write function on mChatService and
handle the message that you will receive on the other device.

You might also like