I have been trying this for a week now ...I just not getting what's going on. So here what I'm trying to achieve.
I want to send my MPU_6050 data to AndroidVR app via classic HC-05 Bluetooth module.so that I can rotate the cube.
First of all ,all by connection are good well connected and working coz I have used in Terminal App.
I have performed the same thing using Serial Port with Desktop it work like charm...I just need to print the data in series as
1.01,22.1,35.1,
using Serial.print();
and on unity side I can get the value in string Array and can split() function something like this
string[] output_array = sp.ReadLine().Split(',');
here is sp is serial class object . but I cant used Serial port in Android .Can I?
so I'm using this Bluetooth plugin : https://assetstore.unity.com/packages/tools/input-management/android-microcontrollers-bluetooth-16467
Now the problem I'm facing is first in Arduino ... as i'm using
Serial.write();
- so I won't get the accurate result eg: 0.36 would be 0
- second if the value is -14.3 i.e., negative so it would be 255-14 it wold send 241
Now in the Unity I don't know how do I split the data ... and get the value from the list . converting it to string in Unity would give ASCII value which I don't want.
Below is the simple code...please help me with this .
Arduino code :
#include <Wire.h>
#define SEPARATOR 255
void setup()
{
Serial.begin(115200);
Wire.begin();
calibrate_sensors();
set_last_read_angle_data(millis(), 0, 0, 0, 0, 0, 0);
}
void loop()
{
/* Every thing above work well and all calculation are Performed
I'm using Complimentary Filter for MPU6050 */
byte buf[]={angle_x ,angle_y, angle_z,SEPARATOR};
//here first three values are x,y,z angle and a separator(255 as per ASCII)
Serial.write(buf,sizeof(buf));//sending the data(byte array) via HC-05.
}
Unity C# code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using TechTweaking.Bluetooth;
public class JoystickHandler : MonoBehaviour
{
private BluetoothDevice device;
public Text statusText;
public Text logsOnScreen;
public Transform cube;
public float speed = 1.0f;
void Awake()
{
statusText.text = "Status : ...";
BluetoothAdapter.enableBluetooth();
device = new BluetoothDevice();
device.Name = "HC-05";
//device.MacAddress = "XX:XX:XX:XX:XX:XX";
device.setEndByte(255);
device.ReadingCoroutine = ManageConnection;
}
public void connect()
{
device.connect();
}
public void disconnect()
{
device.close();
}
IEnumerator ManageConnection(BluetoothDevice device)
{
statusText.text = "Status : Connected & Can read";
while (device.IsConnected && device.IsReading)
{
byte[] packets = device.read();
if (packets != null)
{
statusText.text = "so we got some data";
int val1 = packets[0];
int val2 = packets[1];
int val3 = packets[2];
logsOnScreen.text = val1+" , "+val2+","+val3;
cube.Rotate(val1,val2,val3);
}
yield return null;
}
statusText.text = "Status : Done Reading";
}
}
I know I know its not the prettiest code ...but just trying to achieve the goal and I know I should have used Vector3 and other but this is just for testing purpose ...so kept simple .Please let me know your thought.