Browse Source

增加Hex数组转换通用方法

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
6d0e303a28
  1. 5
      Assets/MsgTransmitTools/Example/Script/HttpServerExample.cs
  2. 33
      Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs

5
Assets/MsgTransmitTools/Example/Script/HttpServerExample.cs

@ -10,7 +10,10 @@ public class HttpServerExample : MonoBehaviour,IHttpServer
{ {
//接收指定数据后回调 //接收指定数据后回调
this.RegisterMessageEvent<DYData.BluetoothGatewayData>(e => { this.RegisterMessageEvent<DYData.BluetoothGatewayData>(e => {
Debug.Log("ip输出:" + e.ip); string watchData = "myWatchData:";
byte[] result = e.findDataByMac("535708030130").toByteArray();
watchData += "\nHeartRate:"+ result[result.Length - 5] + "\nBloodOxygen:"+ result[result.Length - 4] + "\nBodyTempreture:"+ (result[result.Length - 2]+ result[result.Length - 3]*256) + "\nSos:"+ result[result.Length - 1];
Debug.Log(watchData);
}); });
//开启接收该类数据,关闭接收可以用this.offReceive<DYData.BluetoothGatewayData>(); //开启接收该类数据,关闭接收可以用this.offReceive<DYData.BluetoothGatewayData>();
this.onReceive<DYData.BluetoothGatewayData>(); this.onReceive<DYData.BluetoothGatewayData>();

33
Assets/MsgTransmitTools/src/ProtocolRulesExtention.cs

@ -1,11 +1,42 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using LitJson;
using System;
namespace JXSoft { namespace JXSoft {
//协议规则解析通用方法扩展 //协议规则解析通用方法扩展
public static class ProtocolRulesExtention public static class ProtocolRulesExtention
{ {
/// <summary>
/// 根据Byte类型数据获取对应的值。
/// </summary>
/// <param name="self">只能包含A-F,0-9</param>
/// <param name="start">起始位置</param>
/// <param name="length">长度</param>
/// <returns></returns>
public static byte[] toByteArray(this string self) {
int hexlen = self.Length;
byte[] result;
if (hexlen % 2 == 1)
{
//奇数
hexlen++;
result = new byte[(hexlen / 2)];
self += "0" ;
}
else
{
//偶数
result = new byte[(hexlen / 2)];
}
int j = 0;
for (int i = 0; i < hexlen; i += 2)
{
result[j] = (byte)int.Parse(self.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
j++;
}
return result;
}
} }
} }

Loading…
Cancel
Save