3

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.

sjain
  • 23,126
  • 28
  • 107
  • 185
  • If your responsible for both ends then either approach is possible from what you have said so far. If your not responsible for both ends then you need to find out what the other end expects... – Ifor May 02 '14 at 11:14
  • In the document provided by other end it is mentioned that - "Master Bluetooth Host establishes a SPP connection and then data flows". So I assume that it is using socket ? The document doesn't provided any characteristics that Gatt profile need to communicate. It provided certain commands like "Start", "Stop" to initiate the communication. I also read somewhere that - as of Android 4.4 there is no support for peripheral mod in the Android BLE apis - https://code.google.com/p/android/issues/detail?id=59693. – sjain May 02 '14 at 11:23
  • Just stick to the legacy BT socket stuff then as that is what you need nothing to do with BTLE really. – Ifor May 02 '14 at 11:34
  • FYI: I have a Bluetooth-LE remote that is actually a generic HID-Over-GATT device. These devices can be paired with Android and then behave like a keyboard. This works for a raspberry Pi as well: https://stackoverflow.com/questions/54745576/detecting-the-buttons-on-a-bluetooth-remote-hid-over-gatt – Besi Jan 18 '20 at 12:14

0 Answers0