57

Can anybody give me Android Bluetooth communication tutorial links or hints? Please don't tell me to refer to the BluetoothChat example, I can only understand how to discover and connect to devices but don't know how to send and receive the data over Bluetooth.

I am actually working on an Android and embedded Bluetooth device project. Please help me out.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Pratik
  • 831
  • 2
  • 8
  • 13
  • 2
    You can base BluetoothChat example to send the data over. If you look at the code, text message string is converted to byte[] before being sent over bluetooth. So convert your data to byte[] and then send it over. – Win Myo Htet Nov 22 '13 at 17:52
  • IMHO chat example is good just for chat or a bit faster communication.. When you need to send 100+ messages per second I am not sure if is useful. – Ewoks Jan 21 '14 at 13:45
  • The Android developer page has [tutorials](http://developer.android.com/guide/topics/connectivity/bluetooth.html) and explains how to use the Bluetooth feature: I hope this helps. – Luke Taylor Sep 24 '12 at 10:12
  • I have gone through this I can discover devices, connect them but how to send and receive data on BT? – Pratik Sep 24 '12 at 10:16
  • There is a good open source project to show you how to deal with the communication between Android device and embedded bluetooth device. https://github.com/akexorcist/Android-BluetoothSPPLibrary – ifeegoo Aug 08 '15 at 13:44
  • I guess it's better to understand Bluetooth connection with [this tutorial](http://mobile.tutsplus.com/tutorials/android/android-quick-look-bluetoothadapter/). http://luugiathuy.com/2011/02/android-java-bluetooth/ http://code.google.com/p/android-bluetooth-touchpad/wiki/FrameworkTutorial :) – MBMJ Sep 24 '12 at 10:16

1 Answers1

11

I have also used following link as others have suggested you for bluetooth communication.

http://developer.android.com/guide/topics/connectivity/bluetooth.html

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.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • 3
    Has anybody faced the issue with this chat service that it could not connect with the device, or even it connect's and instantly disconnects ? – Jacob Dec 31 '14 at 11:23