【问题标题】:Customizing Android BluetoothLeGatt example and send data via BLE自定义 Android BluetoothLeGatt 示例并通过 BLE 发送数据
【发布时间】:2016-02-19 06:26:40
【问题描述】:

首先我不得不说我是 android 编程的新手。我正在尝试通过 Android 向 BLE 外围设备发送简单数据(短字符串)。因此,我从

定制了BluetoothLeGatt示例

http://developer.android.com/samples/BluetoothLeGatt/project.html

我将 gatt_services_characteristics.xml 中的 Gatt 服务列表替换为输入数据的文本字段,并在 EditText 和“发送”按钮下方。代码如下:

<TextView android:id="@+id/gatt_services_list"
            android:layout_width="match_parent"
            android:layout_height="250dp" />
    <EditText android:id="@+id/sendText"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:layout_below="@id/gatt_services_list"/>
    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="SEND"
        android:onClick="sendData"/>

我还在DeviceControlActivity.java中注释掉了函数displayGattServices(List gattServices),并写了一个按钮点击函数如下:

public void sendData(View v) {
    value = textField.getText().toString();
    byte [] strBytes = value.getBytes();

    mGattServicesList.setText(text + "\n>" + value);
    text = text + "\n>" + value;

    clearUI();
}

我现在想实现一种简单的方法来发送在 EditText 中键入的数据。数据只应显示在终端中。

我搜索了一个解决方案,但我并没有完全理解 Gatt 特性的概念,恐怕我必须为此使用它。我正在使用 Android Studio 1.4.1 和 API 18 及更高版本进行编程。

如何以简单的方式发送数据?

提前致谢

【问题讨论】:

  • 您知道您打算向其发送数据的设备的服务和特性吗?您必须在发送数据时指定服务和特征,并且(取决于外围设备和电话的角色)要接受数据,该特征需要是可写的。
  • 我的 BLE 外围设备提供 4 个特性: 0x1800 - 通用访问 0x180A - 设备信息服务 0x1801 - 通用属性 0x1101 - 串行端口 我不知道我必须使用哪一个,但我假设它是串行端口。最后,我希望手机和外围设备之间进行双向传输,而手机发送一些控制命令,外围设备只是返回一些传感器数据。

标签: java android bluetooth-lowenergy send


【解决方案1】:

Android BluetoothLeGatt 示例已自定义为通过 UART 发送和接收。看 https://github.com/danasf/hm10-android-arduino

【讨论】:

  • 请定位您提供的链接中有用的内容(例如一些代码)
猜你喜欢
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多