|
@@ -23,11 +23,24 @@ namespace SmartBowSDK
|
|
|
|
|
|
|
|
private BluetoothWindows _bluetoothWindows;
|
|
private BluetoothWindows _bluetoothWindows;
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 6轴
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ private o06DOF.SixAxisFusion sixAxisFusion;
|
|
|
|
|
+
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
/// 过滤名称
|
|
/// 过滤名称
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
public string filters { get; set; } = "";
|
|
public string filters { get; set; } = "";
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 当前的传感器类型
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ public SensorAxisType sensorAxisType { get; set; } = SensorAxisType.NineAxisSensor;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
private void SetStatus(BluetoothStatusEnum newStatus)
|
|
private void SetStatus(BluetoothStatusEnum newStatus)
|
|
|
{
|
|
{
|
|
|
BluetoothStatusEnum oldStatus = bluetoothStatus;
|
|
BluetoothStatusEnum oldStatus = bluetoothStatus;
|
|
@@ -84,6 +97,16 @@ namespace SmartBowSDK
|
|
|
{
|
|
{
|
|
|
_bluetoothWindows.Connect();
|
|
_bluetoothWindows.Connect();
|
|
|
SetStatus(BluetoothStatusEnum.Connecting);
|
|
SetStatus(BluetoothStatusEnum.Connecting);
|
|
|
|
|
+
|
|
|
|
|
+ //初始化一次六轴解析
|
|
|
|
|
+ if (sixAxisFusion == null)
|
|
|
|
|
+ {
|
|
|
|
|
+ Vector3 accelOffset = Vector3.zero;
|
|
|
|
|
+ Vector3 gyroOffset = Vector3.zero;
|
|
|
|
|
+ float gForce = 1.0f;
|
|
|
|
|
+ float gyroToDegree = 1.0f;
|
|
|
|
|
+ sixAxisFusion = new o06DOF.SixAxisFusion(accelOffset, gyroOffset, gForce, gyroToDegree);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -120,7 +143,26 @@ namespace SmartBowSDK
|
|
|
_bluetoothHelper = BluetoothHelper.GetNewInstance();
|
|
_bluetoothHelper = BluetoothHelper.GetNewInstance();
|
|
|
_bluetoothHelper.OnConnected += OnConnected;
|
|
_bluetoothHelper.OnConnected += OnConnected;
|
|
|
_bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
|
|
_bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
|
|
|
- _bluetoothHelper.OnCharacteristicChanged += OnCharacteristicChanged;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ SmartBowLogger.Log(this,"创建蓝牙的SensorAxisType类型:"+ sensorAxisType);
|
|
|
|
|
+ if (sensorAxisType == SensorAxisType.SixAxisSensor)
|
|
|
|
|
+ {
|
|
|
|
|
+ //6轴时候,另外的解析操作
|
|
|
|
|
+ _bluetoothHelper.OnCharacteristicChanged += OnCharacteristicChanged6Axis;
|
|
|
|
|
+ //初始化一次六轴解析
|
|
|
|
|
+ if (sixAxisFusion == null) {
|
|
|
|
|
+ Vector3 accelOffset = Vector3.zero;
|
|
|
|
|
+ Vector3 gyroOffset = Vector3.zero;
|
|
|
|
|
+ float gForce = 1.0f;
|
|
|
|
|
+ float gyroToDegree = 1.0f;
|
|
|
|
|
+ sixAxisFusion = new o06DOF.SixAxisFusion(accelOffset, gyroOffset, gForce, gyroToDegree);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ //九轴的解析
|
|
|
|
|
+ _bluetoothHelper.OnCharacteristicChanged += OnCharacteristicChanged;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
_bluetoothHelper.OnScanEnded += OnScanEnded;
|
|
_bluetoothHelper.OnScanEnded += OnScanEnded;
|
|
|
_bluetoothHelper.ScanNearbyDevices();
|
|
_bluetoothHelper.ScanNearbyDevices();
|
|
|
_NoneToConnectingStep = 2;
|
|
_NoneToConnectingStep = 2;
|
|
@@ -183,12 +225,32 @@ namespace SmartBowSDK
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- StartCoroutine(InitWhenConnected());
|
|
|
|
|
|
|
+ //指令区分
|
|
|
|
|
+ if (sensorAxisType == SensorAxisType.SixAxisSensor)
|
|
|
|
|
+ {
|
|
|
|
|
+ //6轴指令
|
|
|
|
|
+ StartCoroutine(InitWhenConnected6Axis());
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ //九轴
|
|
|
|
|
+ StartCoroutine(InitWhenConnected());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
void OnConnected_windows()
|
|
void OnConnected_windows()
|
|
|
{
|
|
{
|
|
|
SetStatus(BluetoothStatusEnum.Connected);
|
|
SetStatus(BluetoothStatusEnum.Connected);
|
|
|
- StartCoroutine(InitWhenConnected());
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (sensorAxisType == SensorAxisType.SixAxisSensor)
|
|
|
|
|
+ {
|
|
|
|
|
+ //6轴指令
|
|
|
|
|
+ StartCoroutine(InitWhenConnected6Axis());
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ //九轴
|
|
|
|
|
+ StartCoroutine(InitWhenConnected());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void OnConnectionFailed(BluetoothHelper helper)
|
|
void OnConnectionFailed(BluetoothHelper helper)
|
|
@@ -200,7 +262,12 @@ namespace SmartBowSDK
|
|
|
{
|
|
{
|
|
|
SetStatus(BluetoothStatusEnum.None);
|
|
SetStatus(BluetoothStatusEnum.None);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 原九轴的传感器解析
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="helper"></param>
|
|
|
|
|
+ /// <param name="value"></param>
|
|
|
|
|
+ /// <param name="characteristic"></param>
|
|
|
void OnCharacteristicChanged(BluetoothHelper helper, byte[] value, BluetoothHelperCharacteristic characteristic)
|
|
void OnCharacteristicChanged(BluetoothHelper helper, byte[] value, BluetoothHelperCharacteristic characteristic)
|
|
|
{
|
|
{
|
|
|
if (helper != _bluetoothHelper) return;
|
|
if (helper != _bluetoothHelper) return;
|
|
@@ -209,9 +276,71 @@ namespace SmartBowSDK
|
|
|
if (macAddress == null && _receivedDataCount++ < 500) ParseMacAddress(value);
|
|
if (macAddress == null && _receivedDataCount++ < 500) ParseMacAddress(value);
|
|
|
smartBowHelper.aimHandler.OnDataReceived(bytes);
|
|
smartBowHelper.aimHandler.OnDataReceived(bytes);
|
|
|
}
|
|
}
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 六轴传感器时候使用的解析
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="helper"></param>
|
|
|
|
|
+ /// <param name="value"></param>
|
|
|
|
|
+ /// <param name="characteristic"></param>
|
|
|
|
|
+ void OnCharacteristicChanged6Axis(BluetoothHelper helper, byte[] value, BluetoothHelperCharacteristic characteristic)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (helper != _bluetoothHelper) return;
|
|
|
|
|
+ if (bluetoothStatus != BluetoothStatusEnum.Connected) return;
|
|
|
|
|
+ byte[] bytes = value;
|
|
|
|
|
+ if (macAddress == null && _receivedDataCount++ < 500) ParseMacAddress(value);
|
|
|
|
|
+ //smartBowHelper.aimHandler.OnDataReceived(bytes);
|
|
|
|
|
+ //转字符串后就是 Bat
|
|
|
|
|
+ if (bytes.Length == 20 && bytes[19] == 0x0a
|
|
|
|
|
+ //Bat
|
|
|
|
|
+ && bytes[2] == 0x42 && bytes[3] == 0x61 && bytes[4] == 0x74) {
|
|
|
|
|
+ //第一步 解析电量?
|
|
|
|
|
+ string betty = System.Text.Encoding.ASCII.GetString(bytes);
|
|
|
|
|
+ // SmartBowLogger.Log(this, BitConverter.ToString(bytes).Replace("-", ""));
|
|
|
|
|
+ //SmartBowLogger.Log(this, "betty:" + betty);
|
|
|
|
|
+ // 第二步:解析JSON字符串
|
|
|
|
|
+ Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(betty);
|
|
|
|
|
+ // 第三步:提取"Bat"的值
|
|
|
|
|
+ int batValue = (int)json["Bat"];
|
|
|
|
|
+ SmartBowLogger.Log(this, "Bat Value: " + batValue); // 输出: 100
|
|
|
|
|
+ battery = batValue;
|
|
|
|
|
+ }
|
|
|
|
|
+ //传感器数据
|
|
|
|
|
+ if (bytes.Length == 20 && bytes[0] == 0x7b && bytes[19] == 0x7d)
|
|
|
|
|
+ {
|
|
|
|
|
+ o06DOF.SixData sixData = sixAxisFusion.getSixAxisByBytes(bytes);
|
|
|
|
|
+ Quaternion quaternion = sixAxisFusion.tranUpdateData(sixData);
|
|
|
|
|
+ //输出欧拉角 pitch yaw roll
|
|
|
|
|
+ smartBowHelper.InvokeOnSixAxisRotationUpdate(quaternion.eulerAngles);
|
|
|
|
|
+ //smartBowHelper.InvokeOnAxisDataUpdateEvent(bytes);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 重置6轴identify
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ public void ResetSixAxisFusion() {
|
|
|
|
|
+ if (sixAxisFusion != null) {
|
|
|
|
|
+ sixAxisFusion.ResetToInitialRotation();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// pc调用
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="deviceId"></param>
|
|
|
|
|
+ /// <param name="value"></param>
|
|
|
void OnCharacteristicChanged_windows(string deviceId, byte[] value)
|
|
void OnCharacteristicChanged_windows(string deviceId, byte[] value)
|
|
|
{
|
|
{
|
|
|
- OnCharacteristicChanged(null, value, null);
|
|
|
|
|
|
|
+ if (sensorAxisType == SensorAxisType.SixAxisSensor)
|
|
|
|
|
+ {
|
|
|
|
|
+ //6轴指令
|
|
|
|
|
+ OnCharacteristicChanged6Axis(null, value, null);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ //九轴
|
|
|
|
|
+ OnCharacteristicChanged(null, value, null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices)
|
|
void OnScanEnded(BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices)
|
|
@@ -299,6 +428,30 @@ namespace SmartBowSDK
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 初始化6轴指令
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
|
+ IEnumerator InitWhenConnected6Axis()
|
|
|
|
|
+ {
|
|
|
|
|
+ string myConnectedHandlerID = Guid.NewGuid().ToString();
|
|
|
|
|
+ _connectedHandlerID = myConnectedHandlerID;
|
|
|
|
|
+ yield return new WaitForSecondsRealtime(BluetoothWindows.IsWindows() ? 0.3f : 2.2f);
|
|
|
|
|
+ Queue<string> cmds = new Queue<string>(new string[] { "M", "B", "B" });
|
|
|
|
|
+ while (cmds.Count > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (bluetoothStatus != BluetoothStatusEnum.Connected || myConnectedHandlerID != _connectedHandlerID) yield break;
|
|
|
|
|
+ string cmd = cmds.Dequeue();
|
|
|
|
|
+ WriteData(cmd);
|
|
|
|
|
+ yield return new WaitForSecondsRealtime(0.3f);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (bluetoothStatus != BluetoothStatusEnum.Connected || myConnectedHandlerID != _connectedHandlerID) yield break;
|
|
|
|
|
+ StartCoroutine(LoopRequestBattery6Axis(myConnectedHandlerID));
|
|
|
|
|
+ moduleInited = true;
|
|
|
|
|
+ smartBowHelper.InvokeOnBluetoothModuleInited();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private int _battery = 0;
|
|
private int _battery = 0;
|
|
|
public int battery
|
|
public int battery
|
|
|
{
|
|
{
|
|
@@ -306,10 +459,14 @@ namespace SmartBowSDK
|
|
|
set
|
|
set
|
|
|
{
|
|
{
|
|
|
_battery = value;
|
|
_battery = value;
|
|
|
- SmartBowLogger.Log(this, "Battery缓存成功");
|
|
|
|
|
|
|
+ //SmartBowLogger.Log(this, "Battery缓存成功");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 九轴原本的连接电池指令
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="myConnectedHandlerID"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
IEnumerator LoopRequestBattery(string myConnectedHandlerID)
|
|
IEnumerator LoopRequestBattery(string myConnectedHandlerID)
|
|
|
{
|
|
{
|
|
|
while (bluetoothStatus == BluetoothStatusEnum.Connected && myConnectedHandlerID == _connectedHandlerID)
|
|
while (bluetoothStatus == BluetoothStatusEnum.Connected && myConnectedHandlerID == _connectedHandlerID)
|
|
@@ -318,6 +475,19 @@ namespace SmartBowSDK
|
|
|
AddCommandToQueue("b");
|
|
AddCommandToQueue("b");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 6轴的连接电池指令
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="myConnectedHandlerID"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
|
+ IEnumerator LoopRequestBattery6Axis(string myConnectedHandlerID)
|
|
|
|
|
+ {
|
|
|
|
|
+ while (bluetoothStatus == BluetoothStatusEnum.Connected && myConnectedHandlerID == _connectedHandlerID)
|
|
|
|
|
+ {
|
|
|
|
|
+ yield return new WaitForSecondsRealtime(10);
|
|
|
|
|
+ AddCommandToQueue("B");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
private List<string> _commands = new List<string>();
|
|
private List<string> _commands = new List<string>();
|
|
|
void LoopHandleCommands()
|
|
void LoopHandleCommands()
|