向远程BLE设备发送命令并获得响应的方法

时间:2014-05-02 09:36:15

标签: android bluetooth-lowenergy android-bluetooth

我发现很多帖子都在讨论连接蓝牙设备的过程。

蓝牙BLE有一个很好的文档解释相同 -

http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

因此,这两种方法是---

方法1: 使用套接字

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();

方法2: 使用Gatt资料

final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
device.connectGatt(this, false, mGattCallback);

但我很困惑,我应该使用哪种方法与我的远程BLE蓝牙设备通话。我想将命令发送到远程BLE,如“开始”,“停止”,设备将做出相应的响应。

我可以在连接后写入socket并发送命令。但第二种方法使用Servicescharacteristics。不确定socket是否适用于BLE通信。

我应该将什么视为命令 - 服务或特征?

我看到该特征采用值而不是字符串形式的命令。

0 个答案:

没有答案
相关问题