How to convert int array to byre array?

Hi guys, I face a problem in convert int array to byte array. I am using int to calculate the data of MPU6050, and want to send the data via BLE4.0 to my android app. But bluetooth on moblie only can receive data in byte array I think, so I want to convert the calculated int array to byte array and send via the BLE module to the android.

I have two question I think:
Question1: What should I put inside the byte abcd [?] bracket that make the array equal to two int value in the int pass_data[2] array?
Question2: How to change the pass_data[0] and pass_data[1] to byte array ?

I marked the two questions under the related code below, please have a look .

Thank you for your help!!!

The codes are below :

#include <SoftwareSerial.h>
#include <Wire.h>
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#define mpu_add 0x68  //mpu6050 address

SoftwareSerial I2CBT(8,9);

class kalman {
  public :
    double getkalman(double acc, double gyro, double dt) {
      //project the state ahead
      angle += dt * (gyro - bias) ;

      //Project the error covariance ahead
      P[0][0] += dt * (dt * P[1][1] - P[0][1] - P[1][0] + Q_angle) ;
      P[0][1] -= dt * P[1][1] ;
      P[1][0] -= dt * P[1][1] ;
      P[1][1] += Q_gyro * dt ;

      //Compute the Kalman gain
      double S = P[0][0] + R_measure ;
      K[0] = P[0][0] / S ;
      K[1] = P[1][0] / S ;

      //Update estimate with measurement z
      double y = acc - angle ;
      angle += K[0] * y ;
      bias += K[1] * y ;

      //Update the error covariance
      double P_temp[2] = {P[0][0], P[0][1]} ;
      P[0][0] -= K[0] * P_temp[0] ;
      P[0][1] -= K[0] * P_temp[1] ;
      P[1][0] -= K[1] * P_temp[0] ;
      P[1][1] -= K[1] * P_temp[1] ;

      return angle ;
    } ;
    void init(double angle, double gyro, double measure) {
      Q_angle = angle ;
      Q_gyro = gyro ;
      R_measure = measure ;

      angle = 0 ;
      bias = 0 ;

      P[0][0] = 0 ;
      P[0][1] = 0 ;
      P[1][0] = 0 ;
      P[1][1] = 0 ;
    } ;
    double getvar(int num) {
      switch (num) {
        case 0 :
          return Q_angle ;
          break ;
        case 1 :
          return Q_gyro ;
          break ;
        case 2 :
          return R_measure ;
          break ;
      }
    } ;
  private :
    double Q_angle, Q_gyro, R_measure ;
    double angle, bias ;
    double P[2][2], K[2] ;
} ;

kalman kalx ;
kalman kaly ;
kalman kalz ;

long ac_x, ac_y, ac_z, gy_x, gy_y, gy_z ;

double deg[3], dgy_x, dgy_y, dgy_z ;
double dt ;
uint32_t pasttime ;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600) ;
  I2CBT.begin(9600);
  Wire.begin() ;
  Wire.beginTransmission(mpu_add) ;
  Wire.write(0x6B) ;
  Wire.write(0) ;
  Wire.endTransmission(true) ;

  
  kalx.init(0.001, 0.003, 0.03) ;  //init kalman filter
  kaly.init(0.001, 0.003, 0.03) ;
  kalz.init(0.001, 0.003, 0.03) ;
}

void loop() {
  // put your main code here, to run repeatedly:

  Wire.beginTransmission(mpu_add) ; //get acc data
  Wire.write(0x3B) ;
  Wire.endTransmission(false) ;
  Wire.requestFrom(mpu_add, 6, true) ;
  ac_x = Wire.read() << 8 | Wire.read() ;
  ac_y = Wire.read() << 8 | Wire.read() ;
  ac_z = Wire.read() << 8 | Wire.read() ;

  Wire.beginTransmission(mpu_add) ; //get gyro data
  Wire.write(0x43) ;
  Wire.endTransmission(false) ;
  Wire.requestFrom(mpu_add, 6, true) ;
  gy_x = Wire.read() << 8 | Wire.read() ;
  gy_y = Wire.read() << 8 | Wire.read() ;
  gy_z = Wire.read() << 8 | Wire.read() ;

  deg[0] = atan2(ac_x, ac_z) * 180 / PI ;  //acc data to degree data
  deg[1] = atan2(ac_x, ac_y) * 180 / PI ;
  deg[2] = atan2(ac_y, ac_z) * 180 / PI ;
  dgy_x = gy_x / 131. ;  //gyro output to
  dgy_y = gy_y / 131. ;
  dgy_z = gy_z / 131. ;

  dt = (double)(micros() - pasttime) / 1000000;
  pasttime = micros();  //convert output to understandable data

  double val[3] ;

  val[0] = kalx.getkalman(deg[0], dgy_y, dt) ;  //get kalman data
  val[1] = kaly.getkalman(deg[1], dgy_z, dt) ;
  val[2] = kalz.getkalman(deg[2], dgy_x, dt) ;

  int pass_data[2] ;
  char sign[2] = {3, 3} ;
  byte abcd[?];

Question1: What should I put inside the byte abcd [?] bracket that make the array equal to two int value in the int pass_data[2] array?

  pass_data[0] = abs(val[0]) / 1 ;  //double to integer
  if (val[0] < 0) sign[0] -= 2 ;  //check sign

Question2: How to change the pass_data[0] and pass_data[1] to byte array ?

  pass_data[1] = abs(val[2]) / 1 ;
  if (!(val[2] < 0)) sign[1] -= 2 ;

  if (Serial.available()) {
    if (Serial.read() == 's') {
      Serial.write(0xff) ;
      Serial.write(pass_data[0]) ;
      Serial.write(sign[0]) ;
      Serial.write(pass_data[1]) ;
      Serial.write(sign[1]) ;
      Serial.flush() ;
    }
  }
  if (I2CBT.available()) {
    
      I2CBT.write(0xff) ;
      I2CBT.write(pass_data[0]) ;
      I2CBT.write(sign[0]) ;
      I2CBT.write(pass_data[1]) ;
      I2CBT.write(sign[1]) ;
      I2CBT.flush() ;
    
  }
}

You don't need to convert anything. pass_data is an array that occupies 4 bytes.

However, you won't be able to send 4 bytes in a row, since you are sending something else after the first two bytes and after the second two bytes. Not that you send the bytes correctly, mind you.

What is receiving the data? What is the problem with how it receives the data?

   I2CBT.write((byte *)&pass_data[0], 2);

is the proper way to send two bytes stored in an int.

Thank you Paul !!

I am trying to using Unity and MPU6050 and BLE4.0 to do the project. I want to make the android app or window application that using Unity 3d object. The workflow is when I move the mpu6050, the data of mpu6050 can be send to the android app or window application via BLE4.0 module with arduino.

I want to make the wireless version of the demo that similar to the above link. But I can only do the demo with the usb wire that using serial port communication , I cant make it success using BLE communication.

The serial port communication C# script of the cube in Unity is below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;
using BLEFramework.Unity;


public class degree : MonoBehaviour
{
    

    SerialPort sp = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
    //("COM3", 9600);
    //("\\\.\\COM11",9600);
    int[] val = new int[5];
    int[] angle = new int[2];


    // Use this for initialization
    void Start()
    {
        sp.Open();
        sp.ReadTimeout = 1;
       

    }

    // Update is called once per frame
    void Update()
    {
        if (sp.IsOpen)
        {
            try
            {
                sp.Write("s");
                val[0] = sp.ReadByte();
                if (val[0] == 0xff)
                {
                    for (int i = 1; i < 5; i++)
                    {
                        val[i] = sp.ReadByte();
                    }

                    angle[0] = val[1] * (val[2] - 2);
                    angle[1] = val[3] * (val[4] - 2);

                    transform.rotation =
                        Quaternion.Euler(angle[1], 0f, angle[0]);

                }
            }
            catch (System.Exception) { }
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }

        
    }
  
}

After write()ing the 0xff, you write (or think you are writing) 6 bytes. Yet you read 4 bytes, and do something weird with 2 of them. I really do not understand how you think that you are reading what you are writing.

I do not see the value in sending 3 bytes (2 for the value that include the sign and one for the (redundant) sign instead of a max of 6, in human-readable format, unless speed is of the essence.

If it was, you could still send 6 bytes faster at 115200 than you can send 3 bytes at 9600.