My Daily Gist | Ferdinand Silva


Connecting Android Device To HC-05 Bluetooth Module Sample Code (Java)

 
package com.ferdinandsilva.bluetoothtest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
/*
Permissions:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
*/
public Button button;
public EditText editText;
public BluetoothAdapter btAdapter;
public BluetoothDevice btDevice;
public BluetoothSocket btSocket;
public static final String SERVICE_ID = "00001101-0000-1000-8000-00805f9b34fb"; //SPP UUID
public static final String SERVICE_ADDRESS = "98:D3:31:FB:82:85"; // HC-05 BT ADDRESS
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
btAdapter = BluetoothAdapter.getDefaultAdapter();
btDevice = btAdapter.getRemoteDevice(SERVICE_ADDRESS);
if(btAdapter == null) {
Toast.makeText(getApplicationContext(), "Bluetooth not available", Toast.LENGTH_LONG).show();
} else {
if(!btAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 3);
} else {
ConnectThread connectThread = new ConnectThread(btDevice);
connectThread.start();
}
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btSocket != null) {
try{
OutputStream out = btSocket.getOutputStream();
out.write((editText.getText().toString() + "\r\n").getBytes());
}catch(IOException e) {
}
}
}
});
}
private class ConnectThread extends Thread {
private final BluetoothSocket thisSocket;
private final BluetoothDevice thisDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
thisDevice = device;
try {
tmp = thisDevice.createRfcommSocketToServiceRecord(UUID.fromString(SERVICE_ID));
} catch (IOException e) {
Log.e("TEST", "Can't connect to service");
}
thisSocket = tmp;
}
public void run() {
// Cancel discovery because it otherwise slows down the connection.
btAdapter.cancelDiscovery();
try {
thisSocket.connect();
Log.d("TESTING", "Connected to shit");
} catch (IOException connectException) {
try {
thisSocket.close();
} catch (IOException closeException) {
Log.e("TEST", "Can't close socket");
}
return;
}
btSocket = thisSocket;
}
public void cancel() {
try {
thisSocket.close();
} catch (IOException e) {
Log.e("TEST", "Can't close socket");
}
}
}
}