简体   繁体   English

如何在Android设备上使用Android应用通过蓝牙执行5字节数据接收?

[英]How to perform 5 bytes data receive via Bluetooth with Android app on Android device?

I need to receive 5 bytes frame via Bluetooth on Android device. 我需要在Android设备上通过蓝牙接收5个字节的帧。 I have no problem with sending the data frame, but i don't know how to receive this properly. 我没有发送数据帧的问题,但我不知道如何正确接收它。 I don't need to receive string, only byte values. 我不需要接收字符串,只需接收字节值。 Does anyone have code for something like that? 有人有类似的代码吗? I'm programming in Android Studio 2.2.3 我在Android Studio 2.2.3中编程

You have to enable notification / indication respected to the characteristics. 您必须启用有关特性的通知/指示。 After you write the command. 编写命令后。 You will get callbacks from GATT as bytes. 您将以字节为单位从GATT获得回调。

1) Scan Devices 1)扫描设备

2) Connect with devices 2)与设备连接

   device.connectGatt(mContext, autoConnect,BluetoothGattCallback, BluetoothDevice.TRANSPORT_LE);

BluetoothGattCallback - Callback BluetoothGattCallback-回调

In this callback you have multiple inherited methods. 在此回调中,您具有多个继承的方法。 For your purpose use this 为了您的目的使用此

Inherit this method , to get a bytes from your peripheral. 继承此方法,以从外围设备获取字节。

 public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    throw new RuntimeException("Stub!");
}

3) You have to enable the Indication/Notification according to your peripheral requirement. 3)您必须根据外围设备要求启用指示/通知。

// For enable Indication - And give your parameter as your charactertics. //对于启用指示-并指定参数作为特征。

      private static final UUID CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID =     UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");


   public final boolean enableIndications(final BluetoothGattCharacteristic characteristic) {
    Log.d("CheckData", "enableIndications");
    final BluetoothGatt gatt = mBluetoothGatt;
    if (gatt == null || characteristic == null)
        return false;

    // Check characteristic property
    final int properties = characteristic.getProperties();
    if ((properties & BluetoothGattCharacteristic.PROPERTY_INDICATE) == 0)
        return false;

    gatt.setCharacteristicNotification(characteristic, true);
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
    if (descriptor != null) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
        return gatt.writeDescriptor(descriptor);
    }
    return false;
}

// For enable Notifciation - And give your parameter as your charactertics. //对于启用Notifciation-并提供您的参数作为特征。

        protected final boolean enableNotifications(final BluetoothGattCharacteristic characteristic, boolean enable) {
    final BluetoothGatt gatt = mBluetoothGatt;
    if (gatt == null || characteristic == null)
        return false;

    // Check characteristic property
    final int properties = characteristic.getProperties();
    if ((properties & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == 0)
        return false;

    gatt.setCharacteristicNotification(characteristic, enable);
    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID);
    if (descriptor != null) {
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        return gatt.writeDescriptor(descriptor);
    }
    return false;
}

4) Write values to your respected characteristics. 4)根据您所尊重的特征写下价值观。

5) Response will come to your registered callbacks BluetoothGattCallback 5)响应将来到您注册的回调BluetoothGattCallback

   public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

      characteristic.getStringValue(1) // Output Bytes
      characteristic.getValue()  // Output as Byte Array
      Log.d("Values", characteristic.getStringValue(1));
  }

characteristic.getStringValue(1) // Output Bytes as string from particular offset characteristic.getValue() // Output as Byte Array Characteristics.getStringValue(1)//从特定偏移量以字符串形式输出字节 特征.getValue()//以字节数组形式输出

Hope this answer will help you. 希望这个答案对您有帮助。

Cheers Up Vote Up 振作起来投票

Happy Coding 快乐编码

you can find tutorials on how to set up a bluetooth connection in the offical doc. 您可以在官方文档中找到有关如何建立bluetooth连接的教程。 https://developer.android.com/guide/topics/connectivity/bluetooth.html https://developer.android.com/guide/topics/connectivity/bluetooth.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM