我已经尝试了一个星期……我只是不了解发生了什么。 所以这就是我想要达到的目标。
我想通过经典的HC-05蓝牙模块将MPU_6050数据发送到AndroidVR应用,以便我可以旋转立方体。
首先,所有连接都连接良好,并且在我在终端应用程序中使用过,因为工作正常。
我已经使用带有台式机的串行端口执行了相同的操作,就像魅力一样工作...我只需要将数据串行打印为
1.01,22.1,35.1,
使用Serial.print();
在统一方面,我可以获取字符串Array中的值,并且可以使用split()函数执行类似的操作
string[] output_array = sp.ReadLine().Split(',');
这是sp是串行类对象。但我不能在Android中使用串行端口。可以吗?
所以我正在使用这个蓝牙插件: https://assetstore.unity.com/packages/tools/input-management/android-microcontrollers-bluetooth-16467
现在我要面对的问题首先是Arduino ...因为我正在使用
Serial.write();
现在在Unity中,我不知道如何分割数据……并从列表中获取值。在Unity中将其转换为字符串会得到我不想要的ASCII值。
下面是简单的代码...请帮助我。
#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.
}
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";
}
}
我知道我知道它不是最漂亮的代码...但是只是为了实现目标,我知道我应该使用Vector3等,但这只是出于测试目的...所以保持简单。请让我知道你的想法。