| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using ArduinoBluetoothAPI;
- using System;
- using UnityEngine;
- public class BluetoothD : MonoBehaviour
- {
-
- BluetoothHelper bluetoothHelper;
- private bool btnCanClick = true;
- private float roll = 0;
- private float pitch = 0;
- private float yaw = 0;
- private float baseX = 0;//EulerAngleX
- private float baseY = 0;//EulerAngleY
- private float baseZ = 0;//EulerAngleY
- private int ReceiveDataCount = 0;
- private bool hasSetBasePoint = false;
- public static GameObject controlTarget;
- public static BluetoothD ins;
- void Start()
- {
- ins = this;
- try {
- BluetoothHelper.BLE = false;
- bluetoothHelper = BluetoothHelper.GetNewInstance("HC-06");
- bluetoothHelper.setFixedLengthBasedStream(11);
- bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
- {
- Log("连接成功");
- bluetoothHelper.StartListening();
- Log("开始采集信息\n" + bluetoothHelper.getDeviceName());
- };
- bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
- {
- btnCanClick = true;
- Log("连接失败");
- };
- bluetoothHelper.OnDataReceived += (BluetoothHelper helper) =>
- {
- byte[] bytes = helper.ReadBytes();
- if (bytes[1] == 89) {
- float q1 = TwoByteToFloat(bytes[3], bytes[2]);
- float q2 = TwoByteToFloat(bytes[5], bytes[4]);
- float q3 = TwoByteToFloat(bytes[7], bytes[6]);
- float q4 = TwoByteToFloat(bytes[9], bytes[8]);
- q1 = q1 / 32768;
- q2 = q2 / 32768;
- q3 = q3 / 32768;
- q4 = q4 / 32768;
- q2 = 0f - q2;
- q3 = 0f - q3;
- ReceiveDataCount++;
- if (controlTarget != null) {
- controlTarget.transform.rotation = new Quaternion(q2, q1, q3, q4);
- pitch = controlTarget.transform.eulerAngles.x;
- yaw = controlTarget.transform.eulerAngles.y;
- roll = controlTarget.transform.eulerAngles.z;
- controlTarget.transform.eulerAngles = new Vector3(controlTarget.transform.eulerAngles.x + baseX, controlTarget.transform.eulerAngles.y + baseY, controlTarget.transform.eulerAngles.z + baseZ);
- }
- Log(q1 + ", " + q2 + ", " + q3 + ", " + q4);
- }
- };
- } catch (Exception e) {
- Debug.Log(e.Message);
- Log("请打开蓝牙");
- }
- this.Invoke("Connect", 1);
- }
- void OnDestroy()
- {
- if (bluetoothHelper != null) {
- bluetoothHelper.Disconnect();
- }
- }
- public void SetBasePoint()
- {
- baseX = -pitch;
- baseY = 90 -yaw;
- baseZ = -90 -roll ;
- if (ReceiveDataCount > 10) {
- hasSetBasePoint = true;
- }
- }
- public void AutoSetBasePoint()
- {
- if (!hasSetBasePoint && ReceiveDataCount > 10 && (roll > 87 && roll < 93) && (pitch < 3 || pitch > 357)) {
- SetBasePoint();
- }
- }
- public void Connect()
- {
- if (!btnCanClick) {
- return;
- }
- btnCanClick = false;
- try {
- if (bluetoothHelper != null) {
- bluetoothHelper.Connect();
- Log("正在扫描设备");
- } else {
- btnCanClick = true;
- Log("蓝牙初始化失败");
- }
- } catch (Exception e) {
- btnCanClick = true;
- Debug.Log(e.Message);
- Log("请先配对蓝牙");
- }
- }
- float TwoByteToFloat(byte b1, byte b2)
- {
- ushort twoByte = (ushort) (b1 * 256 + b2);
- short shortNum = (short) twoByte;
- return (float) shortNum;
- }
- void Log(string text)
- {
-
- }
- }
|