Need help with this code to use bluetooth

Hi guys need some help with this code. This code produces a graph on a serial plotter. But the next step is that i would like to display the exact graph on an android app. The issue is that i am a newbie in arduino. I want to bring this graph via bluetooth to display on the android app. I am unsure what is the few lines of code needed here to communicate with the android app.

#include "CurieIMU.h"

int ledPin = 13;  // use the built in LED on pin 13 of the Uno

// IMU
float FS_SEL = 131;                                  // IMU gyro values to degrees/sec
unsigned long last_read_time;
float angle_x, angle_y, angle_z;                     // These are the result angles
float last_x_angle, last_y_angle, last_z_angle;      // These are the filtered angles
float lGXA, lGYA, lGZA;                              // Store the gyro angles to compare drift

// FUNCTIONS
//Math
//Convert radians to degrees
double rtod(double fradians) {return(fradians * 180.0 / PI);}

void set_last_read_angle_data(unsigned long time, float x, float y, float z, float x_gyro, float y_gyro, float z_gyro) {
  last_read_time = time;
  last_x_angle = x; last_y_angle = y; last_z_angle = z;
  lGXA = x_gyro; lGYA = y_gyro; lGZA = z_gyro;
}

void setup() {

   pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);

  Serial.begin(9600); // Default connection rate for my BT module
  while (!Serial);

  // init CurieImu 
  CurieIMU.begin
  ();
  // use the code below to calibrate accel/gyro offset values
  Serial.println("Internal sensor offsets BEFORE calibration...");
  Serial.print(CurieIMU.getXAccelOffset()); 
  Serial.print("\t"); // -76
  Serial.print(CurieIMU.getYAccelOffset()); 
  Serial.print("\t"); // -235
  Serial.print(CurieIMU.getZAccelOffset()); 
  Serial.print("\t"); // 168
  Serial.print(CurieIMU.getXGyroOffset()); 
  Serial.print("\t"); // 0
  Serial.print(CurieIMU.getYGyroOffset()); 
  Serial.print("\t"); // 0
  Serial.println(CurieIMU.getZGyroOffset());

  // The board must be resting in a horizontal position for 
  // the following calibration procedure to work correctly!
  CurieIMU.autoCalibrateGyroOffset();
  CurieIMU.autoCalibrateXAccelOffset(0);
  CurieIMU.autoCalibrateYAccelOffset(0);
  CurieIMU.autoCalibrateZAccelOffset(1);
  CurieIMU.setGyroOffsetEnabled(true);
  CurieIMU.setAccelOffsetEnabled(true);

  set_last_read_angle_data(millis(), 0, 0, 0, 0, 0, 0);
}

void loop() {
  unsigned long t_now = millis();
  int ax = CurieIMU.getAccelerationX();
  int ay = CurieIMU.getAccelerationY();
  int az = CurieIMU.getAccelerationZ();
  int gx = CurieIMU.getRotationX();
  int gy = CurieIMU.getRotationY();
  int gz = CurieIMU.getRotationZ();


  // Convert gyro values to degrees/sec
  float gyro_x = gx/FS_SEL;
  float gyro_y = gy/FS_SEL;
  float gyro_z = gz/FS_SEL;

   // Compute the accel angles
  float accelX = rtod(atan(ay / sqrt( pow(ax, 2) + pow(az, 2))));
  float accelY = rtod(-1 * atan(ax/sqrt(pow(ay,2) + pow(az,2))));
  
  // Compute the (filtered) gyro angles
  float dt =(t_now - last_read_time)/1000.0;
  float gyroX = gyro_x*dt + last_x_angle;
  float gyroY = gyro_y*dt + last_y_angle;
  float gyroZ = gyro_z*dt + last_z_angle;  

  // Compute the drifting gyro angles
  float unfiltered_gyro_angle_x = gyro_x*dt + lGXA;
  float unfiltered_gyro_angle_y = gyro_y*dt + lGYA;
  float unfiltered_gyro_angle_z = gyro_z*dt + lGZA;
  
  // Apply the complementary filter to figure out the change in angle 
  // Alpha depends on the sampling rate...
  float alpha = 0.96;
  angle_x = alpha*gyroX + (1.0 - alpha)*accelX;
  angle_y = alpha*gyroY + (1.0 - alpha)*accelY;
  angle_z = gyroZ;  //Accelerometer doesn't give z-angle

  // Update the saved data with the latest values
  set_last_read_angle_data(t_now, angle_x, angle_y, angle_z, unfiltered_gyro_angle_x, unfiltered_gyro_angle_y, unfiltered_gyro_angle_z);

  Serial.print(',');
  Serial.print(angle_x);
  Serial.print(',');
  Serial.print(angle_y);
  Serial.print(','); 
  Serial.println(angle_z); 
  Serial.println();
}

Connect your SPP Bluetooth module properly configured and paired to Serial and no line needs change

J-M-L:
Connect your SPP Bluetooth module properly configured and paired to Serial and no line needs change

Im using arduino 101 and it has an inbuilt BLE in it. Do u have any links for me to refer to or any steps for me to follow? As its limited to find tutorials on the arduino 101.

Serial.print(variable);

may be all you need, but you might have to be a little less secretive about the android app. With Bluetooth Graphics Terminal, you have to wrap the data into a string first. The instructions for doing that are in the app store, and the code then is

Serial.println(string);

Ah that is new info I have not played with the 101 BTLE stack, if I remember correctly a SPP API is not offered

J-M-L: Ah that is new info I have not played with the 101 BTLE stack, if I remember correctly a SPP API is not offered

I fear I might be sowing some confusion here....

I haven't the faintest idea of what you are talking about, and I suspect you haven't the faintest idea of what I'm talking about either.

What I was referring to is the way the Android app requires its data. Needless to say, Android neither knows nor cares about what sort of Arduino is sending it, or even if it is an Arduino.

I get what you are saying - my understanding of the original question was that the android app would be reading the serial data in the same way it's sent to to the serial plotter. So the API on Android would be SPP for the communication profile and data format basically CSV

While I don't know what an API is, what you say could be true, but I think it is unlikely.

Actually, I now see that OP's output looks pretty ordinary csv, so I guess his Arduino can output data in the same way as any other does.

API = Application programming interface

that's the "vocabulary" and "media" you would need to use to talk to your Android app.

from his ask, I took for granted that his android app would just take a bunch of CSV formatted data over a BT based SPP connexion and draw the graph

for example, pfodApp (Protocol For Operations Discovery) enables raw data sent by a pfodDevice (e.g. Arduino) to be plotted and saved to a file for later use. (limited header might be needed to send the raw data)

J-M-L: I took for granted that his android app would just take a bunch of CSV formatted data over a BT based SPP connexion and draw the graph

Possibly but not probably, hence my original post.

I say this because I am only aware of two bluetooth graph apps. Bluetooth Graphics Terminal is the one I alluded to above, and the one I use. It requires data to be wrapped up into a string, complete with dedicated additional components. The other is BlueGraph which I believe does use a feed in csv, just like anything else, but not likely to be used by the OP as it is impossible to find in the Store unless you already know the exact name. Don't ask why this is.

Ok - well seems the OP is MIA so...

I have tried n followed the bluetooth terminal instruction accordingly about the Serial.print(“E”). However i have an issue as my phone cant detect my board. I know there is something i have to implement in the code which got to do with “#include <CurieBLE.h>”. I have tried to use BLEPeripheral etc from the example code “callbackLED”. But fail to get the phone to pair up still. Any ideas on how to get them paired up?

Mdzain: have an issue as my phone cant detect my board.. But fail to get the phone to pair up still.

No amount of code in arduino is going to fix the problem. This is between bluetooth and the phone, and, after you ensure that the power is on and bluetooth is ready to connect, all the work is at the phone end.

Nick_Pyner: No amount of code in arduino is going to fix the problem. This is between bluetooth and the phone, and, after you ensure that the power is on and bluetooth is ready to connect, all the work is at the phone end.

When i scan for available bluetooth on my phone using the bluetooh terminal app, my arduino 101 board is found & will only paired when i use the curieBLE examples code. It will not even detect the arduino 101 upon scanning with the code that i posted here.

I'm afraid I don't know what you are doing, and I bet you don't either. For starters you need to understand the difference between pairing and connecting. You don't pair with the terminal app, you can only pair in the settings, which would neither know nor care what code is running, if any. Once you have succeeded in pairing, you can connect with the terminal app. Arduino takes no part in either, it just sends data to the serial port. If there is no data, perhaps because you forgot to connect arduino to bluetooth, it simply means nothing is received by the phone. It does not mean the phone is disconnected.