1

I have to develop an Android app to connect to a Bluetooth module that is connected to a board. My goal is to send and receive data to this board.

I'm currently able to enable the Bluetooth on my phone, to pair to the Bluetooth module but I don't know how to connect and send/receive data to this module.

Most of examples explain how to create a server and a client to communicate via sockets. Is it the good way for me? As described here : https://developer.android.com/guide/topics/connectivity/bluetooth.html#java

Avenger
  • 89
  • 1
  • 2
  • 9
  • "Is it the good way for me?" - It is the *only* way it can work. The bluetooth module is probably the "server", awaiting connections. Then the Android will have to create a connection to the other device as a "client", see https://developer.android.com/guide/topics/connectivity/bluetooth.html#example_1. – JimmyB Apr 04 '19 at 12:08
  • What sort of Bluetooth module do you have? One that supports BLE (Bluetooth 4.0 or higher, like the HC-08) or an older one for Bluetooth 2/3 (like the HC-05/06)? – Codo Apr 04 '19 at 12:36
  • I'm asking because they required different protocols: BLE uses GATT characteristics, the older ones use SPP. – Codo Apr 04 '19 at 12:38
  • I have a SPP Bluetooth module, I'm now able to send and receive data. To receive data. – Avenger Apr 04 '19 at 19:44

2 Answers2

0

Do it like in the Example: https://developer.android.com/guide/topics/connectivity/bluetooth.html#example_1

Note that you will probably need to know what kind of service/profile the module provides. Often, generic modules/devices use the Serial Port Profile (SPP).

You use createInsecureRfcommSocketToServiceRecord() or createRfcommSocketToServiceRecord() to connect.

Which UUID you need depends on the actual service the module provides. For SPP see e.g. How to find the UUID of serial port Bluetooth device?:

The short, 16-bit UUID for SPP is

0x1101

the full UUID is

"00001101-0000-1000-8000-00805f9b34fb"

So, on Android, you'd use

final UUID SPP_SERVICE_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

final BluetoothSocket socket = device.createRfcommSocketToServiceRecord( SPP_SERVICE_UUID );

socket.connect();

final InputStream is = socket.getInputStream();
final OutputStream os = socket.getOutputStream();

// Send data to output stream and/or receive data from input stream
// ...

socket.close(); // Disconnect
JimmyB
  • 12,101
  • 2
  • 28
  • 44
0

Following is a method:

  • create service class which communicate every time when it is
    connected to that device.

  • register that service into your main activity through broadcast update. then scan you Bluetooth devices(after validating permissions) and connect it,
    note that connection code must be in your serviceclass(all communication with device is through service class).

  • after that you can sent data to and from the Bluetooth device.

Here attaching an example for you for working with BLE,Created by Nordic Semiconductor Click Here

Dev.Barai
  • 82
  • 1
  • 8