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
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
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
Related
Problem
How can I differentiate between being unable to establish a Bluetooth connection with a remote Android device because:
scenario 1: the remote device is out of range, or its Bluetooth is disabled.
scenario 2: the remote device is in range, but there is no server socket on the remote device to accept my connection.
What I've tried
I could not differentiate between exceptions thrown when connecting because it threw the identical exceptions in both cases:
java.io.IOException: read failed, socket might closed or timeout, read ret -1
I could not use fetchUuidsWithSdp() to check if my UUID supported by the remote device because it behaves the same way in either scenario...according to the documentation:
This API is asynchronous and {#link #ACTION_UUID} intent is sent, with the UUIDs supported by the remote end. If there is an error in getting the SDP records or if the process takes a long time, {#link #ACTION_UUID} intent is sent with the UUIDs that is currently present in the cache...
it's behaviour also seems sort of unpredictable according to this SO thread.
lastly, I didn't want to use sdpSearch to differentiate between the two, because that was added in API 23, and I want to be able to support down to API 19.
You can determine if a device is in range by trying to connect to a standard UUID that is usually available on Android devices. If the call to connect:
fails, then remote device is out of range or its Bluetooth is disabled.
succeeds, then the remote device is in range and you should close the connection and then try to connect to your app's UUID...if that fails, then there was no listening socket...if it succeeds, then all is well.
Example code:
public BluetoothSocket connect(BluetoothDevice remoteDevice) throws IOException
{
OPP_UUID = UUID.fromString("00001105-0000-1000-8000-00805f9b34fb");
// check if remote device is in range...throw exception if out of range
try
{
BluetoothSocket socket = remoteDevice
.createRfcommSocketToServiceRecord(OPP_UUID);
socket.connect();
socket.close();
}
catch(IOException ex)
{
throw new IOException("out of range",ex);
}
// try to connect to service on remote device...throw exception if UUID
// is not available
try
{
BluetoothSocket socket = remoteDevice
.createRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
return socket;
}
catch(IOException ex)
{
throw new IOException("no listening server socket",ex);
}
}
I used BluetoothDevice.getUuids() to get the UUIDs available on one of my Android devices. It gave me this list:
0000110a-0000-1000-8000-00805f9b34fb - Advanced Audio Distribution Profile (0x110a)
00001105-0000-1000-8000-00805f9b34fb - Object Push Profile (0x1105) // least invasive one...it seems
00001115-0000-1000-8000-00805f9b34fb - Personal Area Networking Profile (0x1115)
0000112f-0000-1000-8000-00805f9b34fb - Phonebook Access (0x112f) // this works!
00001112-0000-1000-8000-00805f9b34fb - Headset - Audio Gateway (0x1112)
0000111f-0000-1000-8000-00805f9b34fb - Handsfree Audio Gateway (0x111f)
00001132-0000-1000-8000-00805f9b34fb - Message Access Profile (0x1132) // this works too!
00000000-0000-1000-8000-00805f9b34fb - Base UUID (0x0000) // couldn't get this to work :(
Standard UUIDs from Bluetooth spec.
I have a ESP8266 chip which is connected to the microcircuit. When chip gets value "200" the light is starting to blink 4 times and than it returns "100" value. I need to make an Android app using Java which will connect to the ESP8266 chip, send data to it and will get value "100". I don't know what library I should use to deal with it. Please, help me, how can I do that? I think it is not the most hard question here.
For your Controller you dont need any Libary. You just can use the serial AT Commands: http://www.electrodragon.com/w/ESP8266
After setting up your ESP like this:
In your App you should deal with TCP-Sockets: https://de.wikibooks.org/wiki/Googles_Android/_TCP-Sockets
Try something like this in an async task:
socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), Connect_Timeout);
DataOutputStream DataOut = new DataOutputStream(socket.getOutputStream());
DataOut.writeBytes(message);
DataOut.flush();
socket.close();
So your ESP is the Server and the App the Client. This should work without problems.
Im doing bluetooth based application, I want to connect other devices like nokia devices, and printer.
I refer the android bluetooth documentation http://developer.android.com/guide/topics/connectivity/bluetooth.html. It demonstrates all the fundamental Bluetooth API capabilites, And I did these all the things
Scanning for other Bluetooth devices
Querying the local Bluetooth adapter for paired Bluetooth devices
Establishing RFCOMM channels/sockets
Connecting to a remote device
Transfering data over Bluetooth
i get reference from BluetoothChat, samples of android.
BluetoothChat This application send data to another android device but for that this application must be installed in both the devices.
Like this How to send file from Android device to other device through Bluetooth by code
What i want is
I want to send file from one device to another device from my application and that also works even another device not running our application. i.e. Receiver device also able to receive file using default Bluetooth.
Is this possible in android?
I think this is not possible.
in fact, when you create a bluetooth socket, you have to use createRfcommSocketToServiceRecord(UUID)
This function requires an UUID that is a string shared between the applications on the two devices so the connection can be established.
Without a bluetooth socket listening on the other device, with the exact same UUID, you won't be able to share data.
You can do connection between two BT devices easily.
You just need to call
createRfcommSocketToServiceRecord(UUID)
with UUID that understand receiver device.
For file transfer action UUID must be equal (for example) to 00001106-0000-1000-8000-00805F9B34FB (File transfer service)
So you connection code might looks like code below
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:0A:94:16:77:A0");
BluetoothSocket clientSocket;
try {
log(TAG, "Remote device " + device);
ParcelUuid[] uuids = device.getUuids();
boolean isFileTransferSupported = false;
UUID ftpUID = UUID.fromString("00001106-0000-1000-8000-00805F9B34FB");
// Check if remote device supports file transfer
for (ParcelUuid parcelUuid: uuids) {
if (parcelUuid.getUuid().equals(ftpUID)) {
isFileTransferSupported = true;
break;
}
}
if (!isFileTransferSupported) {
log(TAG, "Remote bluetooth device does not supports file transfer ");
return;
}
clientSocket = device.createRfcommSocketToServiceRecord(ftpUID);
clientSocket.connect();
} catch (IOException e) {
return;
}
I am developing an Android application were I am supposed to get the data from a database in csv/txt file format and later I have to send the files to a wifi printer.
Do anyone know how I might get started doing that?
The answer was finally easy :
Socket client = new Socket(_IP, PORT);
oStream = new PrintStream(client.getOutputStream(), true, "UTF-8");
oStream.println("-------------------------------------------------\r\n");
oStream.println(" NAME : DEMO CLIENT\r\n");
oStream.println(" CODE : 00000234242\r\n");
oStream.println(" ADDRESS : Street 52\r\n");
oStream.println(" Phone : 2310-892345\r\n");
oStream.println("-------------------------------------------------\r\n");
oStream.flush();
oStream.close();
client.close();
You could read a data from database to a file directly. and then you can connect the printer via sockets or wifi. And then pass on to the printer.
There are bunch of projects on github, maybe you can look at them, for example EasyPrinter.
You can do this using sockets. You can get examples in these links
http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/
Example: Android bi-directional network socket using AsyncTask
and you can google it. (Socket programming via java and android examples)
So first you must get your printer ip and port and send data to printer via a socket.
To be ui friendly, you can create a setting form where you can set available printers ip and port
Here's an Open Source project to print things with a Bixolon Bluetooth or WiFi printer on Android: https://github.com/rocboronat/FewlapsLovesBixolon
I am trying to establish a bluetooth connection between my J2ME application (using JSR-082 API) and my desktop application written with Python (using pybluez bluetooth API). However, I could not find a suitable bluetooth communication protocols to pair them.
In pybluez, the way you connect to a server is as follows:
addr, port = "01:23:45:67:89:AB", 1
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((addr, port))
However in JSR-082 bluetooth API, the way you create a server is as follows:
StreamConnectionNotifier connectionNotifier =
(StreamConnectionNotifier) Connector.open("btspp://localhost:" +
"0000000000000000000000000000ABCD;name=JSR82_ExampleService");
streamConnection = connectionNotifier.acceptAndOpen();
or as follows:
L2CAPConnectionNotifier connectionNotifier =
(L2CAPConnectionNotifier) Connector.open("btl2cap://localhost:" +
"0000000000000000000000000000ABCD;name=JSR82_ExampleService");
streamConnection = connectionNotifier.acceptAndOpen();
In pybluez API we use port numbers, and in JSR-082 API we use URLs. How am I going to establish a bluetooth connection then? Is there a way to create a server using a port number in JSR-082 API?
Using JSR-82, you create a server based on a UUID. You need to perform an SDP search to determine the "port" (actually, channel number for RFCOMM or PSM for L2CAP) of the remote service. So, in pybluez, you'd call bluetooth.find_service() (as shown here), examine each of the services returned, and pick the one with a matching UUID ("service-id" in bluez).