java - 如何从 BLE 设备接收数据?

标签 java android bluetooth-lowenergy

因此,我正在使用 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");
  }
}

我希望有好心人会去天堂怜悯我,帮助我获得那些甜蜜的数据字节。

最佳答案

您的代码中缺少的一件事是设置通知,这样 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);
}

关于java - 如何从 BLE 设备接收数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57078704/

相关文章:

android - Android 布局中扭曲的 ProgressBar 微调器

ios - 如何检测外围设备何时停止广告日期,以便我可以删除该外围设备形式发现的设备列表?

ios - 信标测距与 BLE 扫描

c - 蓝牙 LE L2CAP CID 与 PSM

java - 如何让两个子类在游戏中相互通信

java - 为什么我的 Spring Boot starter 没有将它的依赖项带到项目中?

android - 缺少 NativeScript ANDROID_HOME

android动画未在onAnimationEnd完成

java - Android应用程序备份和恢复?

java - 读取文件并将值存储到两个数组中,每一行一个数组