为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 BLE 设备接收数据?

如何从 BLE 设备接收数据?

猛跑小猪 2023-03-31 15:25:42
因此,我正在使用 Eclipse 开发一个 Java BLE Android 模块(对模块和 Appcelerator(制作 Android 应用程序)进行编码)。我正在尝试从 BLE 设备接收数据并将其显示在 Android 手机上。我可以扫描设备并连接到它。但我真的,真的无法从它那里接收到任何数据。我已经尝试了至少 10 种不同的东西但是......主要问题是我不太了解 BLE API,而且我对 Java 有点菜鸟。谁能帮助可怜的人真正从设备中读取数据?主要问题是设置蓝牙特征 UUID(我有)。我只是不知道该怎么做...下面是模块的代码...public class AndroidbleModule extends KrollModule {      public static final String LCAT = "BLE";  private BluetoothManager btManager;  private BluetoothAdapter btAdapter;  private BluetoothDevice btDevice;  private TiApplication appContext;  private Activity activity;  private KrollFunction onFound;  private KrollFunction onConnections;  prvate BluetoothLeScanner btScanner;  private UUID uuid;  BluetoothGatt bluetoothGatt;  BluetoothGattCharacteristic btChar;  BluetoothGattCallbackHandler btData;  KrollDict kd;  Boolean isConnected = false;  BluetoothGatt connectedGatt;          private ScanCallback scanCallback = new ScanCallback() {  @Override  public void onScanResult(int callbackType, ScanResult result) {  BluetoothDevice device = result.getDevice();    if (device != null) {    BluetoothDeviceProxy btDeviceProxy = new   BluetoothDeviceProxy(device);    if (device.getName() != null) {      Log.d(LCAT, "Found: " + device.getName() + " " +   device.getAddress());      ArrayList<String> ids = new ArrayList<String>();        if (device.getUuids() != null) {             for (ParcelUuid id1 : device.getUuids()) {    ids.add(id1.toString());    }    }    btDevice = device;    kd = new KrollDict();    kd.put("name", btDevice.getName());    kd.put("macaddress", btDevice.getAddress());    fireEvent("nb_DevicesFound", kd);                          btScanner.stopScan(scanCallback);      }     }    }  };  我希望某个好心人会去天堂怜悯我,帮助我获得那些甜蜜的数据字节。
查看完整描述

2 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

您的代码中缺少的一件事是设置通知,以便 Central 可以收听 Peripheral 的响应。尝试这样的事情:


public void setNotifications() {

    BluetoothGattService service = bluetoothGatt.getService(SERVICE_UUID);

    BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);


    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(DESCRIPTOR_UUID);

    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

    characteristic.addDescriptor(descriptor);

    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

    bluetoothGatt.writeDescriptor(descriptor);


    bluetoothGatt.setCharacteristicNotification(characteristic, true);

}

之后,您可以向设备发送命令并听到它返回。


public void returnData(String data) {

    BluetoothGattService service = bluetoothGatt.getService(SERVICE_UUID);

    BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);

    String dataString = data;

    dataString.getBytes();

    writeCharacteristic.setValue(dataString);

    writeCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

    bluetoothGatt.writeCharacteristic(writeCharacteristic);

}


查看完整回答
反对 回复 2023-03-31
?
米脂

TA贡献1836条经验 获得超3个赞

首先,阅读蓝牙概述。查看项目是否添加了蓝牙权限。

这里的一个错误是isConnected=true设置得太早了,因为你可以认为你是在发现 ble 服务之后连接的(status == BluetoothGatt.GATT_SUCCESS)。否则无法读写特性。

一个很好的起点可以是这个来自谷歌的回购协议。这是一个旧项目,我不确定您是否必须更新一些依赖项才能使其编译,但这并不重要。

建立连接并开始读取字节很容易,但是如果你想与 ble 设备建立可靠的连接,由于 android 碎片化和制造商不遵循蓝牙规范,这可能非常困难,几乎不可能使蓝牙低适用于所有设备的能量。

一旦你开始阅读一些字节,我建议你通过这个视频学习一些重要的技巧。如果你想更深入,请仔细阅读这个关于 ble 的精彩资源

一旦你开始对 ble 感到绝望,可能是阅读这个已知问题列表的好时机

最后,您会发现在 android 中使用 ble low energy 可以做的最好的事情是使用开源库,例如 Nordic semiconductor ble 库RxAndroid Ble

但在使用 ble 库之前,最好先了解该库在做什么并了解您为什么需要它。

编辑:我从未使用过 appcelerator,但这里有一个用于 appcelerator titanium 的蓝牙模块


查看完整回答
反对 回复 2023-03-31
?
Helenr

TA贡献1780条经验 获得超3个赞

您的客户端特征配置描述符的 uuid 有误。它应该是 00002902-0000-1000-8000-00805f9b34fb 但你写了 6E400002-B5A3-F393-E0A9-E50E24DCCA9E。



查看完整回答
反对 回复 2023-03-31
  • 2 回答
  • 0 关注
  • 143 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信