Android Sample Bluetooth Code to Send a Simple String via Bluetooth

Android sample bluetooth code to send a simple string via bluetooth


private OutputStream outputStream;
private InputStream inStream;

private void init() throws IOException {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
if (blueAdapter.isEnabled()) {
Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();

if(bondedDevices.size() > 0) {
Object[] devices = (Object []) bondedDevices.toArray();
BluetoothDevice device = (BluetoothDevice) devices[position];
ParcelUuid[] uuids = device.getUuids();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
socket.connect();
outputStream = socket.getOutputStream();
inStream = socket.getInputStream();
}

Log.e("error", "No appropriate paired devices.");
} else {
Log.e("error", "Bluetooth is disabled.");
}
}
}

public void write(String s) throws IOException {
outputStream.write(s.getBytes());
}

public void run() {
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes = 0;
int b = BUFFER_SIZE;

while (true) {
try {
bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}

how to send data (a string) to a paired device in android?

Something like this might suffice:

DataOutputStream os;

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

BroadcastReceiver discoveryResult = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
BluetoothDevice remoteDevice;

remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

Toast.makeText(getApplicationContext(), "Discovered: " + remoteDeviceName + " address " + remoteDevice.getAddress(), Toast.LENGTH_SHORT).show();

try{
BluetoothDevice device = bluetooth.getRemoteDevice(remoteDevice.getAddress());

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

BluetoothSocket clientSocket = (BluetoothSocket) m.invoke(device, 1);

clientSocket.connect();

os = new DataOutputStream(clientSocket.getOutputStream());

new clientSock().start();
} catch (Exception e) {
e.printStackTrace();
Log.e("BLUETOOTH", e.getMessage());
}
}
};

registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND));

bluetooth.enable();
if (!bluetooth.isDiscovering()) {
bluetooth.startDiscovery();
}

public class clientSock extends Thread {
public void run () {
try {
os.writeBytes("anything you want"); // anything you want
os.flush();
} catch (Exception e1) {
e1.printStackTrace();
return;
}
}
}

You will also need a lot of imports such as these:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
import java.util.UUID;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

note that not all imports are necessary for this example code, your IDE might help you to sort them out for you.

Pass data on the os.writeBytes("anything you want"); // anything you want line.

You will also need Permissions

Bluetooth Code to send string In Android

What kind of device are you trying to connect to?

-> Based on discussion: If trying to connect to another phone running Android there must be application which is accepting the connection. Working example is available from Google: BluetoothChat


You can try speeding up the connection process:

// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();

Or you can try using different UUID (are there more supported)?

System.out.println(uuids);

Or you can try using insecure connection:

device.createInsecureRfcommSocketToServiceRecord(...)

BTW: Your code for reading data from inpust stream won't work very well. Take a look at example from Google Chat Application: BluetoothChatService

How to send/receive messages via bluetooth android studio

I have made few changes to your app:-

Firstly, I shifted the code responsible for creating the Bluetooth connection to ConnectThread.

2) Added AcceptThread responsible for listening incoming connections and ConnectedThread maintaining the BTConnection, Sending the data, and
receiving incoming data through input/output streams respectively.
3) Created 2 buttons to start ConnectThread and AcceptThread.

NOTE: Make sure both the devices are paired and the device that you
are trying to connect to is at the top of the list(or just remove all
the paired devices from both the devices and only pair the devices
that you want to connect). Also, you must start the AcceptThread
before ConnectThread

MAINACTIVITY.JAVA

public class MainActivity extends AppCompatActivity {

private static final UUID MY_UUID_INSECURE =
UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");

private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private BluetoothDevice mmDevice;
private UUID deviceUUID;
ConnectedThread mConnectedThread;
private Handler handler;

String TAG = "MainActivity";
EditText send_data;
TextView view_data;
StringBuilder messages;






public void pairDevice(View v) {

Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
Log.e("MAinActivity", "" + pairedDevices.size() );
if (pairedDevices.size() > 0) {
Object[] devices = pairedDevices.toArray();
BluetoothDevice device = (BluetoothDevice) devices[0];
//ParcelUuid[] uuid = device.getUuids();
Log.e("MAinActivity", "" + device );
//Log.e("MAinActivity", "" + uuid)

ConnectThread connect = new ConnectThread(device,MY_UUID_INSECURE);
connect.start();

}
}

private class ConnectThread extends Thread {
private BluetoothSocket mmSocket;

public ConnectThread(BluetoothDevice device, UUID uuid) {
Log.d(TAG, "ConnectThread: started.");
mmDevice = device;
deviceUUID = uuid;
}

public void run(){
BluetoothSocket tmp = null;
Log.i(TAG, "RUN mConnectThread ");

// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
Log.d(TAG, "ConnectThread: Trying to create InsecureRfcommSocket using UUID: "
+MY_UUID_INSECURE );
tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID_INSECURE);
} catch (IOException e) {
Log.e(TAG, "ConnectThread: Could not create InsecureRfcommSocket " + e.getMessage());
}

mmSocket = tmp;

// Make a connection to the BluetoothSocket

try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();

} catch (IOException e) {
// Close the socket
try {
mmSocket.close();
Log.d(TAG, "run: Closed Socket.");
} catch (IOException e1) {
Log.e(TAG, "mConnectThread: run: Unable to close connection in socket " + e1.getMessage());
}
Log.d(TAG, "run: ConnectThread: Could not connect to UUID: " + MY_UUID_INSECURE );
}

//will talk about this in the 3rd video
connected(mmSocket);
}
public void cancel() {
try {
Log.d(TAG, "cancel: Closing Client Socket.");
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "cancel: close() of mmSocket in Connectthread failed. " + e.getMessage());
}
}
}

private void connected(BluetoothSocket mmSocket) {
Log.d(TAG, "connected: Starting.");

// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
}

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "ConnectedThread: Starting.");

mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;



try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run(){
byte[] buffer = new byte[1024]; // buffer store for the stream

int bytes; // bytes returned from read()

// Keep listening to the InputStream until an exception occurs
while (true) {
// Read from the InputStream
try {
bytes = mmInStream.read(buffer);
final String incomingMessage = new String(buffer, 0, bytes);
Log.d(TAG, "InputStream: " + incomingMessage);

runOnUiThread(new Runnable() {

@Override
public void run() {
view_data.setText(incomingMessage);
}
});


} catch (IOException e) {
Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() );
break;
}
}
}


public void write(byte[] bytes) {
String text = new String(bytes, Charset.defaultCharset());
Log.d(TAG, "write: Writing to outputstream: " + text);
try {
mmOutStream.write(bytes);
} catch (IOException e) {
Log.e(TAG, "write: Error writing to output stream. " + e.getMessage() );
}
}

/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}


public void SendMessage(View v) {
byte[] bytes = send_data.getText().toString().getBytes(Charset.defaultCharset());
mConnectedThread.write(bytes);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


send_data =(EditText) findViewById(R.id.editText);
view_data = (TextView) findViewById(R.id.textView);

if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}


}

public void Start_Server(View view) {

AcceptThread accept = new AcceptThread();
accept.start();

}

private class AcceptThread extends Thread {

// The local server socket
private final BluetoothServerSocket mmServerSocket;

public AcceptThread(){
BluetoothServerSocket tmp = null ;

// Create a new listening server socket
try{
tmp = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("appname", MY_UUID_INSECURE);

Log.d(TAG, "AcceptThread: Setting up Server using: " + MY_UUID_INSECURE);
}catch (IOException e){
Log.e(TAG, "AcceptThread: IOException: " + e.getMessage() );
}

mmServerSocket = tmp;
}

public void run(){
Log.d(TAG, "run: AcceptThread Running.");

BluetoothSocket socket = null;

try{
// This is a blocking call and will only return on a
// successful connection or an exception
Log.d(TAG, "run: RFCOM server socket start.....");

socket = mmServerSocket.accept();

Log.d(TAG, "run: RFCOM server socket accepted connection.");

}catch (IOException e){
Log.e(TAG, "AcceptThread: IOException: " + e.getMessage() );
}

//talk about this is in the 3rd
if(socket != null){
connected(socket);
}

Log.i(TAG, "END mAcceptThread ");
}

public void cancel() {
Log.d(TAG, "cancel: Canceling AcceptThread.");
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "cancel: Close of AcceptThread ServerSocket failed. " + e.getMessage() );
}
}

}

ACTIVITY_MAIN.XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hpi5.bluethoothshot.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="58dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="153dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="SendMessage"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="22dp"
app:layout_constraintTop_toBottomOf="@+id/editText"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Server"
android:onClick="Start_Server"
android:layout_marginEnd="53dp"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginBottom="84dp" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ConnectionREq"
android:onClick="pairDevice"
android:layout_marginStart="34dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="@+id/button2"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/button2"
android:layout_marginLeft="30dp"
app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

send/receive data via Bluetooth

The best and most comprehensive example is Android's official bluetooth chat example. You probably do not need to change most of the stuff. See this:
https://github.com/googlesamples/android-BluetoothChat



Related Topics



Leave a reply



Submit