首页 > 解决方案 > 如何从 BLE 设备接收数据?

问题描述

因此,我正在使用 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;
  private 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);
      }
     }
    }
  };
    
  @Kroll.method
  public boolean connect()
  {
   try {
    bluetoothGatt = btDevice.connectGatt(appContext, true,
    new BluetoothGattCallbackHandler(AndroidbleModule.this));
    if (bluetoothGatt != null) {
    System.out.println("*****     *****     Connected to: =====>>>>>    " + btDevice.getAddress() + " " + btDevice.getName());
    this.fireEvent("nb_onConnect",null);
    isConnected = true;
    bluetoothGatt = connectedGatt;
        }
  } catch (Exception e) {
    isConnected = false;
    this.fireEvent("nb_NoConnection", null);
    }
   return true;
  };
    
  @Kroll.method
  public void readData() 
  {
  System.out.println("WHAT THE HELL DO I DO????");
  }

}

public final class BluetoothGattCallbackHandler extends 
  BluetoothGattCallback {

private static final String LCAT = AndroidbleModule.LCAT;
private KrollProxy proxy;
private static final String UUID_SERVICE_TS002004 = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";
private static final String UUID_CHARACTERISTIC_WRITE_TS002004 = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E";
private static final String UUID_CHARACTERISTIC_NOTIFY_TS002004 = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E";
BluetoothGattCharacteristic btCharacteristic;

public BluetoothGattCallbackHandler(KrollProxy proxy) {
    super();
    this.proxy = proxy;
  }

  @Override
  public void onConnectionStateChange(final BluetoothGatt gatt,
    final int status, final int newState) {
    KrollDict kd = new KrollDict();
    kd.put("newState", newState);
    kd.put("status", status);
    if (proxy.hasListeners("didConnectionStateChange")) {
        proxy.fireEvent("didConnectionStateChange", kd);
    }
    gatt.discoverServices();
    }

  @Override
  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
       super.onServicesDiscovered(gatt, status);
       Log.i(LCAT,"onServicesDiscovered");
       if (status != BluetoothGatt.GATT_SUCCESS) return;
       btCharacteristic = 
gatt.getService(UUID.fromString(UUID_SERVICE_TS002004)).getCharacteristic(UUID.fromString(UUID_CHARACTERISTIC_NOTIFY_TS002004));
       gatt.setCharacteristicNotification(btCharacteristic,true);

       BluetoothGattDescriptor descriptor = btCharacteristic.getDescriptor(UUID.fromString(UUID_CHARACTERISTIC_WRITE_TS002004));
        
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
       gatt.writeDescriptor(descriptor);
  }

  @Override
  public void onCharacteristicChanged(BluetoothGatt gatt,
    final BluetoothGattCharacteristic characteristic) {
    byte[] data = characteristic.getValue();
    Log.i(LCAT, "Char changed " + data.toString());
    for (BluetoothGattDescriptor descriptor : 
 characteristic.getDescriptors()) {
 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
    }
  }
    
  @Override
  public void onCharacteristicRead(BluetoothGatt gatt, 
  BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
        Log.i(LCAT,"onCharacteristicRead");
    }
    
  @Override
  public void onDescriptorRead(BluetoothGatt gatt, 
  BluetoothGattDescriptor descriptor, int status) {
       super.onDescriptorRead(gatt, descriptor, status);
       Log.i(LCAT,"onDescriptorRead");
  }
}

我期待某个善良的灵魂会去天堂怜悯我并帮助我获得那些甜蜜的数据字节。

标签: javaandroidbluetooth-lowenergy

解决方案


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

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

推荐阅读