I found there are many posts talking about the process of connecting to bluetooth devices.
There is a good document for Bluetooth BLE that explains the same -
http://developer.android.com/guide/topics/connectivity/bluetooth-le.html
Accordingly, the two approaches are---
Approach 1: Using Socket
BluetoothDevice hxm = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
Method m;
m = hxm.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
socket = (BluetoothSocket)m.invoke(hxm, Integer.valueOf(1));
//socket = device.createRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
Approach 2: Using Gatt Profile
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
device.connectGatt(this, false, mGattCallback);
But I am confused that which approach should I use to talk to my remote BLE bluetooth device. I want to send command to Remote BLE like "Start", "Stop" and the device will respond accordingly.
I can write to socket after connection and send the command. But the second approach uses Services
and characteristics
. Not sure if socket
will work for BLE communication or not.
What should I consider as a command - A service or a characteristic ?
I see that characteristic takes the values and not a command in string form.