![](/img/trans.png)
[英]Bluetooth LE send string data between Android devices via application
[英]Two methods to send data via Bluetooth LE
我正在研究一个通过蓝牙LE向其发送数据的Android应用。 我很困惑这里有两种方法。 有人可以解释这两种方法及其相互之间的关系:
private String localmessage = null;
public void Send_Oe_Ef(BluetoothDevice device,String message)
{
localmessage= message;
Send_Oe_Ef(device);
}
public void Send_Oe_Ef(BluetoothDevice device)
{
boolean result = false;
Log.i(TAG, "Send_Oe_Ef ");
isNoti = true;
BluetoothGattService mCC2540 = mBluetoothGatt.getService(device, CC2540_SERVICE);
if (mCC2540 == null)
{
Log.e(TAG, "CC2540 service not found!");
return;
}
BluetoothGattCharacteristic mHRMcharac = mCC2540.getCharacteristic(CC2540_CHARACTERISTIC);
if (mHRMcharac == null) {
Log.e(TAG, "CC2540 charateristic not found!");
return;
}
byte[] value = new byte[14];
value[0] = (byte) 1;
value[1] = (byte) 2;
value[2] = (byte) 3;
value[3] = (byte) 4;
value[4] = (byte) 5;
value[5] = (byte) 6;
value[6] = (byte) 7;
value[7] = (byte) 8;
value[8] = (byte) 9;
value[9] = (byte) 10;
value[10] = (byte) 11;
value[11] = (byte) 12;
value[12] = (byte) 13;
value[13] = (byte) 14;
try
{
value = localmessage.getBytes("UTF-8");
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
mHRMcharac.setValue(value);
mBluetoothGatt.writeCharacteristic(mHRMcharac);
Log.e(TAG, "SetValue");
}
提前致谢
第一种方法是调用第二种方法。 有一秒钟,我以为它们是类的构造函数所在的位置,但当然不是。
BLE使用GATT协议。 简而言之,GATT协议由服务组成,而服务又由特征组成。 特征描述您要在远程设备上访问的被测实体。 通常,每个设备都有一个特定的UUID,您可以使用它连接到该设备。 例如,如果您查看TI SensorTag,则将使用F0000000-0451-4000-B000-00000000AA01连接到其温度传感器。 http://processors.wiki.ti.com/index.php/SensorTag_User_Guide
据我所知
BluetoothGattService mCC2540 = mBluetoothGatt.getService(device, CC2540_SERVICE);
正在尝试获取在远程设备上由常量CC2540_SERVICE定义的服务。
这条线
BluetoothGattCharacteristic mHRMcharac = mCC2540.getCharacteristic(CC2540_CHARACTERISTIC);
正在尝试访问CC2540_CHARACTERISTIC引用的服务mCC2540内部的特征
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.