Back to Browse

Bluetooth for Android and Arduino HC-05 Module, Java implementation

59.5K views
Mar 18, 2018
28:43

Here's a concise instructional video on how to use Android mobile telephone to control Arduino microcontroller using HC-05 bluetooth adapter. The programming language that I used is Java in Android Studio. ----------------------------------------------- Here is the code for Android: ----------------------------------------------- package net.bane.bluetoothapplication; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; public class MainActivity extends AppCompatActivity { static final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); System.out.println(btAdapter.getBondedDevices()); BluetoothDevice hc05 = btAdapter.getRemoteDevice("00:21:13:02:B6:5B"); System.out.println(hc05.getName()); BluetoothSocket btSocket = null; int counter = 0; do { try { btSocket = hc05.createRfcommSocketToServiceRecord(mUUID); System.out.println(btSocket); btSocket.connect(); System.out.println(btSocket.isConnected()); } catch (IOException e) { e.printStackTrace(); } counter++; } while (!btSocket.isConnected() && counter < 3); try { OutputStream outputStream = btSocket.getOutputStream(); outputStream.write(48); } catch (IOException e) { e.printStackTrace(); } InputStream inputStream = null; try { inputStream = btSocket.getInputStream(); inputStream.skip(inputStream.available()); for (int i = 0; i < 26; i++) { byte b = (byte) inputStream.read(); System.out.println((char) b); } } catch (IOException e) { e.printStackTrace(); } try { btSocket.close(); System.out.println(btSocket.isConnected()); } catch (IOException e) { e.printStackTrace(); } } } ----------------------------------------------- Note: Youtube does not allow angled brackets in the description, so I used the big angle bracket (<) character instead. Please replace with the regular less-than sign.

Download

0 formats

No download links available.

Bluetooth for Android and Arduino HC-05 Module, Java implementation | NatokHD