Nicla Sense MEプラスArduino IDEでBLEペリフェラル＜その3＞

　前回、文字列でセンサのデータを送りました。ここでは、実数で送ります。前回、キャラクタUUIDを増やすと動作がおかしくなりました。[仕様](https://www.arduino.cc/en/Reference/ArduinoBLE)では、「A characteristic value can be up to 512 bytes long.」と書かれています。512バイトがどのくらいかは計算する方法がわからないので、とりあえず、全部実数で送るスケッチを書きました。
###スケッチ その1
　加速度以降のデータはx、y、zのように複数の値をもちます。それぞれキャラクタUUIDを作りました。
　サービスを三つのグループに分けました。

- Sensor_BHI260_Service1

> 温度　temp
> 湿度　hum
> 気圧　baro
> ガス　gas

- Sensor_BHI260_Service2

> 加速度　accel
> ジャイロ　gyro
> 磁気　magnet

- Sensor_BHI260_Service3

> オイラー角　euler
> クオータニオン　quater

```C
#include "Nicla_System.h"
#include <ArduinoBLE.h>
#include "Arduino.h"
#include "Arduino_BHY2.h"

Sensor   temp(SENSOR_ID_TEMP);
Sensor   hum(SENSOR_ID_HUM);
Sensor   baro(SENSOR_ID_BARO);
SensorXYZ  accel(SENSOR_ID_ACC);
SensorXYZ  gyro(SENSOR_ID_GYRO);
SensorXYZ  magnet(SENSOR_ID_MAG);
SensorOrientation  euler(SENSOR_ID_ORI);
SensorQuaternion   quater(SENSOR_ID_RV);
Sensor   gas(SENSOR_ID_GAS);

// BLE Service
#define BHI260_SERVICE1_UUID   "F000AA30-0451-4000-B000-000000000000"
#define BHI260_SERVICE2_UUID   "F000AA40-0451-4000-B000-000000000000"
#define BHI260_SERVICE3_UUID   "F000AA50-0451-4000-B000-000000000000"
BLEService Sensor_BHI260_Service1(BHI260_SERVICE1_UUID);
BLEService Sensor_BHI260_Service2(BHI260_SERVICE2_UUID);
BLEService Sensor_BHI260_Service3(BHI260_SERVICE3_UUID);

// BLE  Characteristic
#define BHI260_Temp_Characteristic_UUID       "F000AA31-0451-4000-B000-000000000000"
#define BHI260_Hum_Characteristic_UUID        "F000AA32-0451-4000-B000-000000000000"
#define BHI260_Baro_Characteristic_UUID       "F000AA33-0451-4000-B000-000000000000"
#define BHI260_Gas_Characteristic_UUID         "F000AA39-0451-4000-B000-000000000000"

#define BHI260_AccelerationX_Characteristic_UUID    "F000AA44-0451-4000-B000-000000000000"
#define BHI260_AccelerationY_Characteristic_UUID    "F000AA45-0451-4000-B000-000000000000"
#define BHI260_AccelerationZ_Characteristic_UUID    "F000AA46-0451-4000-B000-000000000000"
#define BHI260_GyroscopeX_Characteristic_UUID       "F000AA47-0451-4000-B000-000000000000"
#define BHI260_GyroscopeY_Characteristic_UUID       "F000AA48-0451-4000-B000-000000000000"
#define BHI260_GyroscopeZ_Characteristic_UUID       "F000AA49-0451-4000-B000-000000000000"
#define BHI260_MagnetX_Characteristic_UUID      "F000AA4A-0451-4000-B000-000000000000"
#define BHI260_MagnetY_Characteristic_UUID      "F000AA4B-0451-4000-B000-000000000000"
#define BHI260_MagnetZ_Characteristic_UUID      "F000AA4C-0451-4000-B000-000000000000"

#define BHI260_EulerX_Characteristic_UUID       "F000AA57-0451-4000-B000-000000000000"
#define BHI260_EulerY_Characteristic_UUID       "F000AA58-0451-4000-B000-000000000000"
#define BHI260_EulerZ_Characteristic_UUID       "F000AA59-0451-4000-B000-000000000000"
#define BHI260_QuaterX_Characteristic_UUID      "F000AA5A-0451-4000-B000-000000000000"
#define BHI260_QuaterY_Characteristic_UUID      "F000AA5B-0451-4000-B000-000000000000"
#define BHI260_QuaterZ_Characteristic_UUID      "F000AA5C-0451-4000-B000-000000000000"
#define BHI260_QuaterW_Characteristic_UUID      "F000AA5D-0451-4000-B000-000000000000"

BLEFloatCharacteristic    BHI260_Temperature(BHI260_Temp_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Humidity(BHI260_Hum_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Barometer(BHI260_Baro_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Gas(BHI260_Gas_Characteristic_UUID, BLERead | BLENotify);

BLEFloatCharacteristic    BHI260_AccelerationX(BHI260_AccelerationX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_AccelerationY(BHI260_AccelerationY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_AccelerationZ(BHI260_AccelerationZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeX(BHI260_GyroscopeX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeY(BHI260_GyroscopeY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeZ(BHI260_GyroscopeZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerX(BHI260_MagnetX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerY(BHI260_MagnetY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerZ(BHI260_MagnetZ_Characteristic_UUID, BLERead | BLENotify);

BLEFloatCharacteristic    BHI260_EulerX(BHI260_EulerX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_EulerY(BHI260_EulerY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_EulerZ(BHI260_EulerZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionX(BHI260_QuaterX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionY(BHI260_QuaterY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionZ(BHI260_QuaterZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionW(BHI260_QuaterW_Characteristic_UUID, BLERead | BLENotify);

// BLE  Descriptor
/*    float valueof_BHI260_Temperature;
    float valueof_BHI260_Humidity;
    float valueof_BHI260_Barometer;
    float valueof_BHI260_Gas;
    float valueof_BHI260_AccelerationX;
    float valueof_BHI260_AccelerationY;
    float valueof_BHI260_AccelerationZ;
    float valueof_BHI260_GyroscopeX;
    float valueof_BHI260_GyroscopeY;
    float valueof_BHI260_GyroscopeZ;
    float valueof_BHI260_MagnetometerX;
    float valueof_BHI260_MagnetometerY;
    float valueof_BHI260_MagnetometerZ;
    float valueof_BHI260_EulerX;
    float valueof_BHI260_EulerY;
    float valueof_BHI260_EulerZ;
    float valueof_BHI260_QuaternionX;
    float valueof_BHI260_QuaternionY;
    float valueof_BHI260_QuaternionZ;
    float valueof_BHI260_QuaternionW;
*/    
#define BHI260_Temp_Descriptor_UUID       "2901"
#define BHI260_Hum_Descriptor_UUID        "2901"
#define BHI260_Baro_Descriptor_UUID       "2901"
#define BHI260_Gas_Descriptor_UUID        "2901"
#define BHI260_Acceleration_Descriptor_UUID    "2901"
#define BHI260_Gyroscope_Descriptor_UUID       "2901"
#define BHI260_Magnet_Descriptor_UUID          "2901"
#define BHI260_Euler_Descriptor_UUID        "2901"
#define BHI260_Quater_Descriptor_UUID       "2901"

BLEDescriptor   BHI260_Temp_Descriptor(BHI260_Temp_Descriptor_UUID, "Temperature ; `C");
BLEDescriptor   BHI260_Hum_Descriptor(BHI260_Hum_Descriptor_UUID, "Humidity ; %");
BLEDescriptor   BHI260_Baro_Descriptor(BHI260_Baro_Descriptor_UUID, "Barometer ; hPa");
BLEDescriptor   BHI260_Gas_Descriptor(BHI260_Gas_Descriptor_UUID, "Gas");

BLEDescriptor   BHI260_Acceleration_Descriptor(BHI260_Acceleration_Descriptor_UUID, "Acceleration x,y,z");
BLEDescriptor   BHI260_Gyroscope_Descriptor(BHI260_Gyroscope_Descriptor_UUID, "Gyroscope x,y,z");
BLEDescriptor   BHI260_Magnet_Descriptor(BHI260_Magnet_Descriptor_UUID, "Magnetometer x,y,z");

BLEDescriptor   BHI260_Euler_Descriptor(BHI260_Euler_Descriptor_UUID, "Euler head,pitch,roll");
BLEDescriptor   BHI260_Quater_Descriptor(BHI260_Quater_Descriptor_UUID, "Quaternion x,y,z,w");

#define localNAME  "Nicla_BHI260"
#define DeviceNAME "NiclaBLE"

float oldValue = 0;  // last value 
float previousMillis = 0;  // last time value was checked, in ms

void setup(){
  Serial.begin(9600);
  while(!Serial);
  Serial.println("BHI260 test");
  
  BHY2.begin();
  accel.begin();
  gyro.begin();
  temp.begin();
  gas.begin();
  euler.begin();
  hum.begin();
  baro.begin();
  quater.begin();
  magnet.begin();

  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);

  //// set the service1
  BLE.setAdvertisedService(Sensor_BHI260_Service1);

  // add characteristic
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Temperature);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Humidity);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Barometer);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Gas);

  // add descriptor
  BHI260_Temperature.addDescriptor(BHI260_Temp_Descriptor);
  BHI260_Humidity.addDescriptor(BHI260_Hum_Descriptor);
  BHI260_Barometer.addDescriptor(BHI260_Baro_Descriptor);
  BHI260_Gas.addDescriptor(BHI260_Gas_Descriptor);

  // add service
  BLE.addService(Sensor_BHI260_Service1);

  //// set the service2
  BLE.setAdvertisedService(Sensor_BHI260_Service2);

  // add characteristic
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationZ);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeZ);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerZ);
  
  // add descriptor
  BHI260_AccelerationX.addDescriptor(BHI260_Acceleration_Descriptor);
  BHI260_GyroscopeX.addDescriptor(BHI260_Gyroscope_Descriptor);
  BHI260_MagnetometerX.addDescriptor(BHI260_Magnet_Descriptor);

  //// add service
  BLE.addService(Sensor_BHI260_Service2);

  // set the service3;
  BLE.setAdvertisedService(Sensor_BHI260_Service3);

  // add characteristic
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerX);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerY);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerZ);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionX);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionY);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionZ);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionW);

  // add descriptor
  BHI260_EulerX.addDescriptor(BHI260_Euler_Descriptor);
  BHI260_QuaternionX.addDescriptor(BHI260_Quater_Descriptor);

  // add service
  BLE.addService(Sensor_BHI260_Service3);

  // set initial value for this characteristic
  BHI260_Temperature.writeValue(1);
  BHI260_Humidity.writeValue(2);
  BHI260_Barometer.writeValue(3);
  BHI260_Gas.writeValue(9);
  BHI260_AccelerationX.writeValue(41);
  BHI260_AccelerationY.writeValue(42);
  BHI260_AccelerationZ.writeValue(43);
  BHI260_GyroscopeX.writeValue(51);
  BHI260_GyroscopeY.writeValue(52);
  BHI260_GyroscopeZ.writeValue(53);
  BHI260_MagnetometerX.writeValue(61);
  BHI260_MagnetometerY.writeValue(62);
  BHI260_MagnetometerZ.writeValue(63);
  BHI260_EulerX.writeValue(71);
  BHI260_EulerY.writeValue(72);
  BHI260_EulerZ.writeValue(73);
  BHI260_QuaternionX.writeValue(81);
  BHI260_QuaternionY.writeValue(82);
  BHI260_QuaternionZ.writeValue(83);
  BHI260_QuaternionW.writeValue(84);

  // start advertising
  BLE.advertise();
  Serial.println("\nBluetooth device active, waiting for connections...");
}

void loop(){
  // wait for a BLE central
  BLEDevice central = BLE.central();
  
  // if a central is connected to the peripheral:
  if (central) {
    delay(100);
    Serial.print("\nConnected to central: ");
    // print the central's BT address:
    Serial.println(central.address());

    // check data every 200ms
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check value:
      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;
        updateValue();
        delay(1000);
      }
    }
    // when the central disconnects
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

void updateValue() {
  BHY2.update();
  delay(500);
  // if value has changed
  if (( accel.x() + gyro.y()) != oldValue) {
  
    Serial.println("");
    Serial.println(String("Temperature: ") + String(temp.value(), 1));
    Serial.println(String("Humidity: ") + String(hum.value(), 1));
    Serial.println(String("Barometer : ") + String(baro.value(), 1));
    Serial.println(String("Acceleration :x,y,z ") + String(accel.x()) + ", " +  String(accel.y()) + ", " + String(accel.z()));
    Serial.println(String("Gyroscope :x,y,z ") + String(gyro.x()) + ", " +  String(gyro.y()) + ", " + String(gyro.z()));
    Serial.println(String("Magnetometer :x,y,z ") + String(magnet.x()) + ", " +  String(magnet.y()) + ", " + String(magnet.z()));
    Serial.println(String("Orientation(Euler):head,pitch,roll ") + String(euler.heading(), 3)+", "+ String(euler.pitch(), 3)+", "+ String(euler.roll(), 3));
    Serial.println(String("Quaternion :x,y,z,w ") + String(quater.x()) + ", " +  String(quater.y()) + ", " + String(quater.z())+ ", "+ String(quater.w()));
    Serial.println(String("gas ohm? : ") + String(gas.value(), 1));
        
    float valueof_BHI260_Temperature = temp.value();
    float valueof_BHI260_Humidity = hum.value();
    float valueof_BHI260_Barometer = baro.value();
    float valueof_BHI260_Gas = gas.value();
    float valueof_BHI260_AccelerationX = accel.x();
    float valueof_BHI260_AccelerationY = accel.y();
    float valueof_BHI260_AccelerationZ = accel.z();
    float valueof_BHI260_GyroscopeX = gyro.x();
    float valueof_BHI260_GyroscopeY = gyro.y();
    float valueof_BHI260_GyroscopeZ = gyro.z();
    float valueof_BHI260_MagnetometerX = magnet.x();
    float valueof_BHI260_MagnetometerY = magnet.y();
    float valueof_BHI260_MagnetometerZ = magnet.z();
    float valueof_BHI260_EulerX =  euler.heading();
    float valueof_BHI260_EulerY =  euler.pitch();
    float valueof_BHI260_EulerZ =  euler.roll();
    float valueof_BHI260_QuaternionX = quater.x();
    float valueof_BHI260_QuaternionY = quater.y();
    float valueof_BHI260_QuaternionZ = quater.z();
    float valueof_BHI260_QuaternionW = quater.w();

    // update  characteristic
    BHI260_Temperature.writeValue(valueof_BHI260_Temperature);
    BHI260_Humidity.writeValue(valueof_BHI260_Humidity);
    BHI260_Barometer.writeValue(valueof_BHI260_Barometer);
    BHI260_Gas.writeValue(valueof_BHI260_Gas);
    BHI260_AccelerationX.writeValue(valueof_BHI260_AccelerationX);
    BHI260_AccelerationY.writeValue(valueof_BHI260_AccelerationY);
    BHI260_AccelerationZ.writeValue(valueof_BHI260_AccelerationZ);
    BHI260_GyroscopeX.writeValue(valueof_BHI260_GyroscopeX);
    BHI260_GyroscopeY.writeValue(valueof_BHI260_GyroscopeY);
    BHI260_GyroscopeZ.writeValue(valueof_BHI260_GyroscopeZ);
    BHI260_MagnetometerX.writeValue(valueof_BHI260_MagnetometerX);
    BHI260_MagnetometerY.writeValue(valueof_BHI260_MagnetometerY);
    BHI260_MagnetometerZ.writeValue(valueof_BHI260_MagnetometerZ);
    BHI260_EulerX.writeValue(valueof_BHI260_EulerX);
    BHI260_EulerY.writeValue(valueof_BHI260_EulerY);
    BHI260_EulerZ.writeValue(valueof_BHI260_EulerZ);
    BHI260_QuaternionX.writeValue(valueof_BHI260_QuaternionX);
    BHI260_QuaternionY.writeValue(valueof_BHI260_QuaternionY);
    BHI260_QuaternionZ.writeValue(valueof_BHI260_QuaternionZ);
    BHI260_QuaternionW.writeValue(valueof_BHI260_QuaternionW);


  oldValue = accel.x() + gyro.y(); // save the level for next comparison
  }
}
```

　アドバタイジングさえしていません。

###スケッチ その2
　三つ目のサービスをコメントアウトしました。

- Sensor_BHI260_Service3

> オイラー角　euler
> クオータニオン　quater

```C
#include "Nicla_System.h"
#include <ArduinoBLE.h>
#include "Arduino.h"
#include "Arduino_BHY2.h"

Sensor   temp(SENSOR_ID_TEMP);
Sensor   hum(SENSOR_ID_HUM);
Sensor   baro(SENSOR_ID_BARO);
SensorXYZ  accel(SENSOR_ID_ACC);
SensorXYZ  gyro(SENSOR_ID_GYRO);
SensorXYZ  magnet(SENSOR_ID_MAG);
SensorOrientation  euler(SENSOR_ID_ORI);
SensorQuaternion   quater(SENSOR_ID_RV);
Sensor   gas(SENSOR_ID_GAS);

// BLE Service
#define BHI260_SERVICE1_UUID   "F000AA30-0451-4000-B000-000000000000"
#define BHI260_SERVICE2_UUID   "F000AA40-0451-4000-B000-000000000000"
#define BHI260_SERVICE3_UUID   "F000AA50-0451-4000-B000-000000000000"
BLEService Sensor_BHI260_Service1(BHI260_SERVICE1_UUID);
BLEService Sensor_BHI260_Service2(BHI260_SERVICE2_UUID);
BLEService Sensor_BHI260_Service3(BHI260_SERVICE3_UUID);

// BLE  Characteristic
#define BHI260_Temp_Characteristic_UUID       "F000AA31-0451-4000-B000-000000000000"
#define BHI260_Hum_Characteristic_UUID        "F000AA32-0451-4000-B000-000000000000"
#define BHI260_Baro_Characteristic_UUID       "F000AA33-0451-4000-B000-000000000000"
#define BHI260_Gas_Characteristic_UUID         "F000AA39-0451-4000-B000-000000000000"

#define BHI260_AccelerationX_Characteristic_UUID    "F000AA44-0451-4000-B000-000000000000"
#define BHI260_AccelerationY_Characteristic_UUID    "F000AA45-0451-4000-B000-000000000000"
#define BHI260_AccelerationZ_Characteristic_UUID    "F000AA46-0451-4000-B000-000000000000"
#define BHI260_GyroscopeX_Characteristic_UUID       "F000AA47-0451-4000-B000-000000000000"
#define BHI260_GyroscopeY_Characteristic_UUID       "F000AA48-0451-4000-B000-000000000000"
#define BHI260_GyroscopeZ_Characteristic_UUID       "F000AA49-0451-4000-B000-000000000000"
#define BHI260_MagnetX_Characteristic_UUID      "F000AA4A-0451-4000-B000-000000000000"
#define BHI260_MagnetY_Characteristic_UUID      "F000AA4B-0451-4000-B000-000000000000"
#define BHI260_MagnetZ_Characteristic_UUID      "F000AA4C-0451-4000-B000-000000000000"

#define BHI260_EulerX_Characteristic_UUID       "F000AA57-0451-4000-B000-000000000000"
#define BHI260_EulerY_Characteristic_UUID       "F000AA58-0451-4000-B000-000000000000"
#define BHI260_EulerZ_Characteristic_UUID       "F000AA59-0451-4000-B000-000000000000"
#define BHI260_QuaterX_Characteristic_UUID      "F000AA5A-0451-4000-B000-000000000000"
#define BHI260_QuaterY_Characteristic_UUID      "F000AA5B-0451-4000-B000-000000000000"
#define BHI260_QuaterZ_Characteristic_UUID      "F000AA5C-0451-4000-B000-000000000000"
#define BHI260_QuaterW_Characteristic_UUID      "F000AA5D-0451-4000-B000-000000000000"

BLEFloatCharacteristic    BHI260_Temperature(BHI260_Temp_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Humidity(BHI260_Hum_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Barometer(BHI260_Baro_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Gas(BHI260_Gas_Characteristic_UUID, BLERead | BLENotify);

BLEFloatCharacteristic    BHI260_AccelerationX(BHI260_AccelerationX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_AccelerationY(BHI260_AccelerationY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_AccelerationZ(BHI260_AccelerationZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeX(BHI260_GyroscopeX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeY(BHI260_GyroscopeY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeZ(BHI260_GyroscopeZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerX(BHI260_MagnetX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerY(BHI260_MagnetY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerZ(BHI260_MagnetZ_Characteristic_UUID, BLERead | BLENotify);
/*
BLEFloatCharacteristic    BHI260_EulerX(BHI260_EulerX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_EulerY(BHI260_EulerY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_EulerZ(BHI260_EulerZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionX(BHI260_QuaterX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionY(BHI260_QuaterY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionZ(BHI260_QuaterZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionW(BHI260_QuaterW_Characteristic_UUID, BLERead | BLENotify);
*/
// BLE  Descriptor
/*    float valueof_BHI260_Temperature;
    float valueof_BHI260_Humidity;
    float valueof_BHI260_Barometer;
    float valueof_BHI260_Gas;
    float valueof_BHI260_AccelerationX;
    float valueof_BHI260_AccelerationY;
    float valueof_BHI260_AccelerationZ;
    float valueof_BHI260_GyroscopeX;
    float valueof_BHI260_GyroscopeY;
    float valueof_BHI260_GyroscopeZ;
    float valueof_BHI260_MagnetometerX;
    float valueof_BHI260_MagnetometerY;
    float valueof_BHI260_MagnetometerZ;
    float valueof_BHI260_EulerX;
    float valueof_BHI260_EulerY;
    float valueof_BHI260_EulerZ;
    float valueof_BHI260_QuaternionX;
    float valueof_BHI260_QuaternionY;
    float valueof_BHI260_QuaternionZ;
    float valueof_BHI260_QuaternionW;
*/    
#define BHI260_Temp_Descriptor_UUID       "2901"
#define BHI260_Hum_Descriptor_UUID        "2901"
#define BHI260_Baro_Descriptor_UUID       "2901"
#define BHI260_Gas_Descriptor_UUID        "2901"
#define BHI260_Acceleration_Descriptor_UUID    "2901"
#define BHI260_Gyroscope_Descriptor_UUID       "2901"
#define BHI260_Magnet_Descriptor_UUID          "2901"
#define BHI260_Euler_Descriptor_UUID        "2901"
#define BHI260_Quater_Descriptor_UUID       "2901"

BLEDescriptor   BHI260_Temp_Descriptor(BHI260_Temp_Descriptor_UUID, "Temperature ; `C");
BLEDescriptor   BHI260_Hum_Descriptor(BHI260_Hum_Descriptor_UUID, "Humidity ; %");
BLEDescriptor   BHI260_Baro_Descriptor(BHI260_Baro_Descriptor_UUID, "Barometer ; hPa");
BLEDescriptor   BHI260_Gas_Descriptor(BHI260_Gas_Descriptor_UUID, "Gas");

BLEDescriptor   BHI260_Acceleration_Descriptor(BHI260_Acceleration_Descriptor_UUID, "Acceleration x,y,z");
BLEDescriptor   BHI260_Gyroscope_Descriptor(BHI260_Gyroscope_Descriptor_UUID, "Gyroscope x,y,z");
BLEDescriptor   BHI260_Magnet_Descriptor(BHI260_Magnet_Descriptor_UUID, "Magnetometer x,y,z");
/*
BLEDescriptor   BHI260_Euler_Descriptor(BHI260_Euler_Descriptor_UUID, "Euler head,pitch,roll");
BLEDescriptor   BHI260_Quater_Descriptor(BHI260_Quater_Descriptor_UUID, "Quaternion x,y,z,w");
*/
#define localNAME  "Nicla_BHI260"
#define DeviceNAME "NiclaBLE"

float oldValue = 0;  // last value 
float previousMillis = 0;  // last time value was checked, in ms

void setup(){
  Serial.begin(9600);
  while(!Serial);
  Serial.println("BHI260 test");
  
  BHY2.begin();
  accel.begin();
  gyro.begin();
  temp.begin();
  gas.begin();
  hum.begin();
  baro.begin();
  magnet.begin();
/*  euler.begin();
  quater.begin();
*/
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);

  //// set the service1
  BLE.setAdvertisedService(Sensor_BHI260_Service1);

  // add characteristic
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Temperature);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Humidity);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Barometer);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Gas);

  // add descriptor
  BHI260_Temperature.addDescriptor(BHI260_Temp_Descriptor);
  BHI260_Humidity.addDescriptor(BHI260_Hum_Descriptor);
  BHI260_Barometer.addDescriptor(BHI260_Baro_Descriptor);
  BHI260_Gas.addDescriptor(BHI260_Gas_Descriptor);

  // add service
  BLE.addService(Sensor_BHI260_Service1);

  //// set the service2
  BLE.setAdvertisedService(Sensor_BHI260_Service2);

  // add characteristic
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationZ);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeZ);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerZ);
  
  // add descriptor
  BHI260_AccelerationX.addDescriptor(BHI260_Acceleration_Descriptor);
  BHI260_GyroscopeX.addDescriptor(BHI260_Gyroscope_Descriptor);
  BHI260_MagnetometerX.addDescriptor(BHI260_Magnet_Descriptor);

  //// add service
  BLE.addService(Sensor_BHI260_Service2);
/*
  // set the service3;
  BLE.setAdvertisedService(Sensor_BHI260_Service3);

  // add characteristic
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerX);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerY);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerZ);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionX);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionY);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionZ);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionW);

  // add descriptor
  BHI260_EulerX.addDescriptor(BHI260_Euler_Descriptor);
  BHI260_QuaternionX.addDescriptor(BHI260_Quater_Descriptor);

  // add service
  BLE.addService(Sensor_BHI260_Service3);
*/
  // set initial value for this characteristic
  BHI260_Temperature.writeValue(1);
  BHI260_Humidity.writeValue(2);
  BHI260_Barometer.writeValue(3);
  BHI260_Gas.writeValue(9);
  BHI260_AccelerationX.writeValue(41);
  BHI260_AccelerationY.writeValue(42);
  BHI260_AccelerationZ.writeValue(43);
  BHI260_GyroscopeX.writeValue(51);
  BHI260_GyroscopeY.writeValue(52);
  BHI260_GyroscopeZ.writeValue(53);
  BHI260_MagnetometerX.writeValue(61);
  BHI260_MagnetometerY.writeValue(62);
  BHI260_MagnetometerZ.writeValue(63);
/*  BHI260_EulerX.writeValue(71);
  BHI260_EulerY.writeValue(72);
  BHI260_EulerZ.writeValue(73);
  BHI260_QuaternionX.writeValue(81);
  BHI260_QuaternionY.writeValue(82);
  BHI260_QuaternionZ.writeValue(83);
  BHI260_QuaternionW.writeValue(84);
*/
  // start advertising
  BLE.advertise();
  Serial.println("\nBluetooth device active, waiting for connections...");
}

void loop(){
  // wait for a BLE central
  BLEDevice central = BLE.central();
  
  // if a central is connected to the peripheral:
  if (central) {
    delay(100);
    Serial.print("\nConnected to central: ");
    // print the central's BT address:
    Serial.println(central.address());

    // check data every 200ms
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check value:
      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;
        updateValue();
        delay(1000);
      }
    }
    // when the central disconnects
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

void updateValue() {
  BHY2.update();
  delay(500);
  // if value has changed
  if (( accel.x() + gyro.y()) != oldValue) {
  
    Serial.println("");
    Serial.println(String("Temperature: ") + String(temp.value(), 1));
    Serial.println(String("Humidity: ") + String(hum.value(), 1));
    Serial.println(String("Barometer : ") + String(baro.value(), 1));
    Serial.println(String("gas ohm? : ") + String(gas.value(), 1));
    Serial.println(String("Acceleration :x,y,z ") + String(accel.x()) + ", " +  String(accel.y()) + ", " + String(accel.z()));
    Serial.println(String("Gyroscope :x,y,z ") + String(gyro.x()) + ", " +  String(gyro.y()) + ", " + String(gyro.z()));
    Serial.println(String("Magnetometer :x,y,z ") + String(magnet.x()) + ", " +  String(magnet.y()) + ", " + String(magnet.z()));
/*    Serial.println(String("Orientation(Euler):head,pitch,roll ") + String(euler.heading(), 3)+", "+ String(euler.pitch(), 3)+", "+ String(euler.roll(), 3));
    Serial.println(String("Quaternion :x,y,z,w ") + String(quater.x()) + ", " +  String(quater.y()) + ", " + String(quater.z())+ ", "+ String(quater.w()));
*/
        
    float valueof_BHI260_Temperature = temp.value();
    float valueof_BHI260_Humidity = hum.value();
    float valueof_BHI260_Barometer = baro.value();
    float valueof_BHI260_Gas = gas.value();
    float valueof_BHI260_AccelerationX = accel.x();
    float valueof_BHI260_AccelerationY = accel.y();
    float valueof_BHI260_AccelerationZ = accel.z();
    float valueof_BHI260_GyroscopeX = gyro.x();
    float valueof_BHI260_GyroscopeY = gyro.y();
    float valueof_BHI260_GyroscopeZ = gyro.z();
    float valueof_BHI260_MagnetometerX = magnet.x();
    float valueof_BHI260_MagnetometerY = magnet.y();
    float valueof_BHI260_MagnetometerZ = magnet.z();
/*    float valueof_BHI260_EulerX =  euler.heading();
    float valueof_BHI260_EulerY =  euler.pitch();
    float valueof_BHI260_EulerZ =  euler.roll();
    float valueof_BHI260_QuaternionX = quater.x();
    float valueof_BHI260_QuaternionY = quater.y();
    float valueof_BHI260_QuaternionZ = quater.z();
    float valueof_BHI260_QuaternionW = quater.w();
*/
    // update  characteristic
    BHI260_Temperature.writeValue(valueof_BHI260_Temperature);
    BHI260_Humidity.writeValue(valueof_BHI260_Humidity);
    BHI260_Barometer.writeValue(valueof_BHI260_Barometer);
    BHI260_Gas.writeValue(valueof_BHI260_Gas);
    BHI260_AccelerationX.writeValue(valueof_BHI260_AccelerationX);
    BHI260_AccelerationY.writeValue(valueof_BHI260_AccelerationY);
    BHI260_AccelerationZ.writeValue(valueof_BHI260_AccelerationZ);
    BHI260_GyroscopeX.writeValue(valueof_BHI260_GyroscopeX);
    BHI260_GyroscopeY.writeValue(valueof_BHI260_GyroscopeY);
    BHI260_GyroscopeZ.writeValue(valueof_BHI260_GyroscopeZ);
    BHI260_MagnetometerX.writeValue(valueof_BHI260_MagnetometerX);
    BHI260_MagnetometerY.writeValue(valueof_BHI260_MagnetometerY);
    BHI260_MagnetometerZ.writeValue(valueof_BHI260_MagnetometerZ);
/*    BHI260_EulerX.writeValue(valueof_BHI260_EulerX);
    BHI260_EulerY.writeValue(valueof_BHI260_EulerY);
    BHI260_EulerZ.writeValue(valueof_BHI260_EulerZ);
    BHI260_QuaternionX.writeValue(valueof_BHI260_QuaternionX);
    BHI260_QuaternionY.writeValue(valueof_BHI260_QuaternionY);
    BHI260_QuaternionZ.writeValue(valueof_BHI260_QuaternionZ);
    BHI260_QuaternionW.writeValue(valueof_BHI260_QuaternionW);
*/

  oldValue = accel.x() + gyro.y(); // save the level for next comparison
  }
}
```

　動きましたが、最後のMagnetometerのデータは送られていません。
![2021-10-16.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/407635/d1b70035-dde3-ae77-a397-526c3b4163ee.png)

###スケッチ その3

　最初のサービスのfloatを100倍したのちにintにしてみました。
　最後のMagnetometerのデータは送られていません。floatは4バイトでintは2バイトだから、16バイト少なくなったのですが、512バイトというのは、別のところを数えているのかもしれません。
![2021-10-16 (1).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/407635/8bd601a5-69b6-dc8d-2891-1cf8377b19c9.png)

###スケッチ その4
　2番目のサービスをコメントアウトしました。

- Sensor_BHI260_Service1

> 温度　temp
> 湿度　hum
> 気圧　baro
> ガス　gas

- Sensor_BHI260_Service3

> オイラー角　euler
> クオータニオン　quater

　接続し、最初のデータは送ってきますが、Notifyをチェックしても、新しいデータは送ってきません。

###スケッチ その5
　1，2番のサービスを止めました。次の二つだけです。ただし、IMUのデータ取得プログラム自体は動かしています。

> オイラー角　euler
> クオータニオン　quater

```C
#include "Nicla_System.h"
#include <ArduinoBLE.h>
#include "Arduino.h"
#include "Arduino_BHY2.h"

Sensor   temp(SENSOR_ID_TEMP);
Sensor   hum(SENSOR_ID_HUM);
Sensor   baro(SENSOR_ID_BARO);
SensorXYZ  accel(SENSOR_ID_ACC);
SensorXYZ  gyro(SENSOR_ID_GYRO);
SensorXYZ  magnet(SENSOR_ID_MAG);
SensorOrientation  euler(SENSOR_ID_ORI);
SensorQuaternion   quater(SENSOR_ID_RV);
Sensor   gas(SENSOR_ID_GAS);

// BLE Service
#define BHI260_SERVICE1_UUID   "F000AA30-0451-4000-B000-000000000000"
#define BHI260_SERVICE2_UUID   "F000AA40-0451-4000-B000-000000000000"
#define BHI260_SERVICE3_UUID   "F000AA50-0451-4000-B000-000000000000"
//BLEService Sensor_BHI260_Service1(BHI260_SERVICE1_UUID);
//BLEService Sensor_BHI260_Service2(BHI260_SERVICE2_UUID);
BLEService Sensor_BHI260_Service3(BHI260_SERVICE3_UUID);

// BLE  Characteristic
/*
#define BHI260_Temp_Characteristic_UUID       "F000AA31-0451-4000-B000-000000000000"
#define BHI260_Hum_Characteristic_UUID        "F000AA32-0451-4000-B000-000000000000"
#define BHI260_Baro_Characteristic_UUID       "F000AA33-0451-4000-B000-000000000000"
#define BHI260_Gas_Characteristic_UUID         "F000AA39-0451-4000-B000-000000000000"

#define BHI260_AccelerationX_Characteristic_UUID    "F000AA44-0451-4000-B000-000000000000"
#define BHI260_AccelerationY_Characteristic_UUID    "F000AA45-0451-4000-B000-000000000000"
#define BHI260_AccelerationZ_Characteristic_UUID    "F000AA46-0451-4000-B000-000000000000"
#define BHI260_GyroscopeX_Characteristic_UUID       "F000AA47-0451-4000-B000-000000000000"
#define BHI260_GyroscopeY_Characteristic_UUID       "F000AA48-0451-4000-B000-000000000000"
#define BHI260_GyroscopeZ_Characteristic_UUID       "F000AA49-0451-4000-B000-000000000000"
#define BHI260_MagnetX_Characteristic_UUID      "F000AA4A-0451-4000-B000-000000000000"
#define BHI260_MagnetY_Characteristic_UUID      "F000AA4B-0451-4000-B000-000000000000"
#define BHI260_MagnetZ_Characteristic_UUID      "F000AA4C-0451-4000-B000-000000000000"
*/
#define BHI260_EulerX_Characteristic_UUID       "F000AA57-0451-4000-B000-000000000000"
#define BHI260_EulerY_Characteristic_UUID       "F000AA58-0451-4000-B000-000000000000"
#define BHI260_EulerZ_Characteristic_UUID       "F000AA59-0451-4000-B000-000000000000"
#define BHI260_QuaterX_Characteristic_UUID      "F000AA5A-0451-4000-B000-000000000000"
#define BHI260_QuaterY_Characteristic_UUID      "F000AA5B-0451-4000-B000-000000000000"
#define BHI260_QuaterZ_Characteristic_UUID      "F000AA5C-0451-4000-B000-000000000000"
#define BHI260_QuaterW_Characteristic_UUID      "F000AA5D-0451-4000-B000-000000000000"
/*
BLEFloatCharacteristic    BHI260_Temperature(BHI260_Temp_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Humidity(BHI260_Hum_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Barometer(BHI260_Baro_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_Gas(BHI260_Gas_Characteristic_UUID, BLERead | BLENotify);

BLEFloatCharacteristic    BHI260_AccelerationX(BHI260_AccelerationX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_AccelerationY(BHI260_AccelerationY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_AccelerationZ(BHI260_AccelerationZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeX(BHI260_GyroscopeX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeY(BHI260_GyroscopeY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_GyroscopeZ(BHI260_GyroscopeZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerX(BHI260_MagnetX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerY(BHI260_MagnetY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_MagnetometerZ(BHI260_MagnetZ_Characteristic_UUID, BLERead | BLENotify);
*/
BLEFloatCharacteristic    BHI260_EulerX(BHI260_EulerX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_EulerY(BHI260_EulerY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_EulerZ(BHI260_EulerZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionX(BHI260_QuaterX_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionY(BHI260_QuaterY_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionZ(BHI260_QuaterZ_Characteristic_UUID, BLERead | BLENotify);
BLEFloatCharacteristic    BHI260_QuaternionW(BHI260_QuaterW_Characteristic_UUID, BLERead | BLENotify);

// BLE  Descriptor    
#define BHI260_Temp_Descriptor_UUID       "2901"
#define BHI260_Hum_Descriptor_UUID        "2901"
#define BHI260_Baro_Descriptor_UUID       "2901"
#define BHI260_Gas_Descriptor_UUID        "2901"
#define BHI260_Acceleration_Descriptor_UUID    "2901"
#define BHI260_Gyroscope_Descriptor_UUID       "2901"
#define BHI260_Magnet_Descriptor_UUID          "2901"
#define BHI260_Euler_Descriptor_UUID        "2901"
#define BHI260_Quater_Descriptor_UUID       "2901"
/*
BLEDescriptor   BHI260_Temp_Descriptor(BHI260_Temp_Descriptor_UUID, "Temperature ; `C");
BLEDescriptor   BHI260_Hum_Descriptor(BHI260_Hum_Descriptor_UUID, "Humidity ; %");
BLEDescriptor   BHI260_Baro_Descriptor(BHI260_Baro_Descriptor_UUID, "Barometer ; hPa");
BLEDescriptor   BHI260_Gas_Descriptor(BHI260_Gas_Descriptor_UUID, "Gas");

BLEDescriptor   BHI260_Acceleration_Descriptor(BHI260_Acceleration_Descriptor_UUID, "Acceleration x,y,z");
BLEDescriptor   BHI260_Gyroscope_Descriptor(BHI260_Gyroscope_Descriptor_UUID, "Gyroscope x,y,z");
BLEDescriptor   BHI260_Magnet_Descriptor(BHI260_Magnet_Descriptor_UUID, "Magnetometer x,y,z");
*/
BLEDescriptor   BHI260_Euler_Descriptor(BHI260_Euler_Descriptor_UUID, "Euler head,pitch,roll");
BLEDescriptor   BHI260_Quater_Descriptor(BHI260_Quater_Descriptor_UUID, "Quaternion x,y,z,w");

#define localNAME  "Nicla_BHI260"
#define DeviceNAME "NiclaBLE"

float oldValue = 0;  // last value 
float previousMillis = 0;  // last time value was checked, in ms

void setup(){
  Serial.begin(9600);
  while(!Serial);
  Serial.println("BHI260 test");
  
  BHY2.begin();
//  temp.begin();
//  baro.begin();
//  hum.begin(); 
//  gas.begin(); 
  accel.begin();
  gyro.begin();
  magnet.begin();
  euler.begin();
  quater.begin();


  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName(localNAME);
  BLE.setDeviceName(DeviceNAME);
/*
  //// set the service1
  BLE.setAdvertisedService(Sensor_BHI260_Service1);

  // add characteristic
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Temperature);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Humidity);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Barometer);
  Sensor_BHI260_Service1.addCharacteristic(BHI260_Gas);

  // add descriptor
  BHI260_Temperature.addDescriptor(BHI260_Temp_Descriptor);
  BHI260_Humidity.addDescriptor(BHI260_Hum_Descriptor);
  BHI260_Barometer.addDescriptor(BHI260_Baro_Descriptor);
  BHI260_Gas.addDescriptor(BHI260_Gas_Descriptor);

  // add service
  BLE.addService(Sensor_BHI260_Service1);

  //// set the service2
  BLE.setAdvertisedService(Sensor_BHI260_Service2);

  // add characteristic
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_AccelerationZ);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_GyroscopeZ);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerX);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerY);
  Sensor_BHI260_Service2.addCharacteristic(BHI260_MagnetometerZ);
  
  // add descriptor
  BHI260_AccelerationX.addDescriptor(BHI260_Acceleration_Descriptor);
  BHI260_GyroscopeX.addDescriptor(BHI260_Gyroscope_Descriptor);
  BHI260_MagnetometerX.addDescriptor(BHI260_Magnet_Descriptor);

  //// add service
  BLE.addService(Sensor_BHI260_Service2);
*/
  // set the service3;
  BLE.setAdvertisedService(Sensor_BHI260_Service3);

  // add characteristic
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerX);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerY);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_EulerZ);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionX);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionY);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionZ);
  Sensor_BHI260_Service3.addCharacteristic(BHI260_QuaternionW);

  // add descriptor
  BHI260_EulerX.addDescriptor(BHI260_Euler_Descriptor);
  BHI260_QuaternionX.addDescriptor(BHI260_Quater_Descriptor);

  // add service
  BLE.addService(Sensor_BHI260_Service3);

  // set initial value for this characteristic
/*  BHI260_Temperature.writeValue(1);
  BHI260_Humidity.writeValue(2);
  BHI260_Barometer.writeValue(3);
  BHI260_Gas.writeValue(9);
  BHI260_AccelerationX.writeValue(41);
  BHI260_AccelerationY.writeValue(42);
  BHI260_AccelerationZ.writeValue(43);
  BHI260_GyroscopeX.writeValue(51);
  BHI260_GyroscopeY.writeValue(52);
  BHI260_GyroscopeZ.writeValue(53);
  BHI260_MagnetometerX.writeValue(61);
  BHI260_MagnetometerY.writeValue(62);
  BHI260_MagnetometerZ.writeValue(63);
*/
  BHI260_EulerX.writeValue(71);
  BHI260_EulerY.writeValue(72);
  BHI260_EulerZ.writeValue(73);
  BHI260_QuaternionX.writeValue(81);
  BHI260_QuaternionY.writeValue(82);
  BHI260_QuaternionZ.writeValue(83);
  BHI260_QuaternionW.writeValue(84);

  // start advertising
  BLE.advertise();
  Serial.println("\nBluetooth device active, waiting for connections...");
}

void loop(){
  // wait for a BLE central
  BLEDevice central = BLE.central();
  
  // if a central is connected to the peripheral:
  if (central) {
    delay(100);
    Serial.print("\nConnected to central: ");
    // print the central's BT address:
    Serial.println(central.address());

    // check data every 200ms
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check value:
      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;
        updateValue();
        delay(1000);
      }
    }
    // when the central disconnects
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

void updateValue() {
  BHY2.update();
  delay(500);
  // if value has changed
  if (( quater.x() + quater.y()) != oldValue) {
  
    Serial.println("");
//    Serial.println(String("Temperature: ") + String(temp.value(), 1));
//    Serial.println(String("Humidity: ") + String(hum.value(), 1));
//    Serial.println(String("Barometer : ") + String(baro.value(), 1));
    Serial.println(String("gas ohm? : ") + String(gas.value(), 1));
    Serial.println(String("Acceleration :x,y,z ") + String(accel.x()) + ", " +  String(accel.y()) + ", " + String(accel.z()));
    Serial.println(String("Gyroscope :x,y,z ") + String(gyro.x()) + ", " +  String(gyro.y()) + ", " + String(gyro.z()));
    Serial.println(String("Magnetometer :x,y,z ") + String(magnet.x()) + ", " +  String(magnet.y()) + ", " + String(magnet.z()));
 
    Serial.println(String("Orientation(Euler):head,pitch,roll ") + String(euler.heading(), 3)+", "+ String(euler.pitch(), 3)+", "+ String(euler.roll(), 3));
    Serial.println(String("Quaternion :x,y,z,w ") + String(quater.x()) + ", " +  String(quater.y()) + ", " + String(quater.z())+ ", "+ String(quater.w()));
    
/*        
    float valueof_BHI260_Temperature = temp.value();
    float valueof_BHI260_Humidity = hum.value();
    float valueof_BHI260_Barometer = baro.value();
    float valueof_BHI260_Gas = gas.value();
   float valueof_BHI260_AccelerationX = accel.x();
    float valueof_BHI260_AccelerationY = accel.y();
    float valueof_BHI260_AccelerationZ = accel.z();
    float valueof_BHI260_GyroscopeX = gyro.x();
    float valueof_BHI260_GyroscopeY = gyro.y();
    float valueof_BHI260_GyroscopeZ = gyro.z();
    float valueof_BHI260_MagnetometerX = magnet.x();
    float valueof_BHI260_MagnetometerY = magnet.y();
    float valueof_BHI260_MagnetometerZ = magnet.z();
*/
    float valueof_BHI260_EulerX =  euler.heading();
    float valueof_BHI260_EulerY =  euler.pitch();
    float valueof_BHI260_EulerZ =  euler.roll();
    float valueof_BHI260_QuaternionX = quater.x();
    float valueof_BHI260_QuaternionY = quater.y();
    float valueof_BHI260_QuaternionZ = quater.z();
    float valueof_BHI260_QuaternionW = quater.w();

    // update  characteristic
/*    BHI260_Temperature.writeValue(valueof_BHI260_Temperature);
    BHI260_Humidity.writeValue(valueof_BHI260_Humidity);
    BHI260_Barometer.writeValue(valueof_BHI260_Barometer);
    BHI260_Gas.writeValue(valueof_BHI260_Gas);
    BHI260_AccelerationX.writeValue(valueof_BHI260_AccelerationX);
    BHI260_AccelerationY.writeValue(valueof_BHI260_AccelerationY);
    BHI260_AccelerationZ.writeValue(valueof_BHI260_AccelerationZ);
    BHI260_GyroscopeX.writeValue(valueof_BHI260_GyroscopeX);
    BHI260_GyroscopeY.writeValue(valueof_BHI260_GyroscopeY);
    BHI260_GyroscopeZ.writeValue(valueof_BHI260_GyroscopeZ);
    BHI260_MagnetometerX.writeValue(valueof_BHI260_MagnetometerX);
    BHI260_MagnetometerY.writeValue(valueof_BHI260_MagnetometerY);
    BHI260_MagnetometerZ.writeValue(valueof_BHI260_MagnetometerZ);
*/
    BHI260_EulerX.writeValue(valueof_BHI260_EulerX);
    BHI260_EulerY.writeValue(valueof_BHI260_EulerY);
    BHI260_EulerZ.writeValue(valueof_BHI260_EulerZ);
    BHI260_QuaternionX.writeValue(valueof_BHI260_QuaternionX);
    BHI260_QuaternionY.writeValue(valueof_BHI260_QuaternionY);
    BHI260_QuaternionZ.writeValue(valueof_BHI260_QuaternionZ);
    BHI260_QuaternionW.writeValue(valueof_BHI260_QuaternionW);

  oldValue = quater.x() + quater.y(); // save the level for next comparison
  }
}
```

 　Notifyをチェックすると、最新データを送ってきます。
![2021-10-16 (2).png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/407635/f9bdc1fa-6d8b-4167-036d-0e67fe956036.png)

　三つのサービスを全部動かすのは無理そうです。x、y、zをコンパクトなパッキングにしてUUID自体の数を少なくすればうまく動くかもしれません。市販品にそういうデータ構造が多いのも、メモリなどの制約が大きいのかもしれません。
　なお、動作が不定なとき、スケッチだけを更新してもデータを正しく送ってくれないことがありました。USBケーブルを抜いて、ハード・リセットをしました。