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
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 eg 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
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).
Here attaching an example for you for working with BLE,Created by Nordic Semiconductor Click Here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.