|
|
@@ -1,7 +1,7 @@
|
|
|
using System;
|
|
|
using UnityEngine;
|
|
|
using UnityEngine.UI;
|
|
|
-using BestHTTP.WebSocket;
|
|
|
+using JCEngineCore;
|
|
|
|
|
|
[Serializable]
|
|
|
class JCData {
|
|
|
@@ -13,84 +13,51 @@ class JCData {
|
|
|
|
|
|
public class BluetoothClient : MonoBehaviour
|
|
|
{
|
|
|
- [NonSerialized] public WebSocket ws;
|
|
|
- bool loaded = false;
|
|
|
[SerializeField] string serverIP = "110.43.54.43";
|
|
|
[SerializeField] Text text;
|
|
|
+ BleDebugClient client = new BleDebugClient();
|
|
|
public static System.Action<byte, byte[]> onDataReceived;
|
|
|
public static BluetoothClient ins;
|
|
|
|
|
|
void Start()
|
|
|
{
|
|
|
ins = this;
|
|
|
- string uri = "ws://" + serverIP + ":9888/BLE";
|
|
|
- ws = new WebSocket(new Uri(uri));
|
|
|
- ws.OnOpen += OnOpen;
|
|
|
- ws.OnMessage += OnMessage;
|
|
|
- ws.Open();
|
|
|
- }
|
|
|
-
|
|
|
- void OnOpen(WebSocket webSocket)
|
|
|
- {
|
|
|
- ws.Send(PackData("loadTempEntity", new string[]{}, 0));
|
|
|
- }
|
|
|
-
|
|
|
- void OnMessage(WebSocket webSocket, string text)
|
|
|
- {
|
|
|
- JCData data = JsonUtility.FromJson<JCData>(text);
|
|
|
- if (data.func == "receiveData")
|
|
|
- {
|
|
|
- string byteStr = data.args[1];
|
|
|
- string[] byteStrs = byteStr.Split(',');
|
|
|
- byte[] bytes = new byte[byteStrs.Length];
|
|
|
- for (int i = 0; i < bytes.Length; i++)
|
|
|
- {
|
|
|
- bytes[i] = Byte.Parse(byteStrs[i]);
|
|
|
- }
|
|
|
- Log("接收数据\n" + byteStr);
|
|
|
- if (onDataReceived != null)
|
|
|
- {
|
|
|
- onDataReceived(byte.Parse(data.args[0]), bytes);
|
|
|
- }
|
|
|
- }
|
|
|
- else if (data.func == "loadTempEntity")
|
|
|
- {
|
|
|
- loaded = true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static string PackData(string func, string[] args, int type)
|
|
|
- {
|
|
|
- JCData data = new JCData();
|
|
|
- data.uuid = "";
|
|
|
- data.type = type;
|
|
|
- data.func = func;
|
|
|
- data.args = args;
|
|
|
- return JsonUtility.ToJson(data);
|
|
|
+ JCEngine.boot("ws://" + serverIP + ":9888/BLE", client);
|
|
|
}
|
|
|
|
|
|
public static void UploadData(byte sign, byte[] bytes)
|
|
|
{
|
|
|
- if (ins && ins.loaded)
|
|
|
+ if (ins && ins.client.isValid)
|
|
|
{
|
|
|
string data = String.Join(",", bytes);
|
|
|
- ins.ws.Send(PackData("uploadData", new string[]{sign.ToString(), data}, 1));
|
|
|
+ ins.client.call("uploadData", sign.ToString(), data);
|
|
|
ins.Log("正在上传数据\n" + data);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void UploadError(params string[] errors) {
|
|
|
- try {
|
|
|
- BluetoothClient.ins.ws.Send(
|
|
|
- BluetoothClient.PackData("showError", errors, 1)
|
|
|
- );
|
|
|
- } catch (System.Exception) {}
|
|
|
+ if (ins) {
|
|
|
+ ins.client.call("showError", string.Join("\n", errors));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- void Log(string text) {
|
|
|
+ public void Log(string text) {
|
|
|
if (this.text != null)
|
|
|
{
|
|
|
this.text.text = text;
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
+class BleDebugClient : JCEntity {
|
|
|
+
|
|
|
+ public void receiveData(string sign, string byteStrList) {
|
|
|
+ string[] byteStrs = byteStrList.Split(',');
|
|
|
+ byte[] bytes = new byte[byteStrs.Length];
|
|
|
+ for (int i = 0; i < bytes.Length; i++)
|
|
|
+ {
|
|
|
+ bytes[i] = Byte.Parse(byteStrs[i]);
|
|
|
+ }
|
|
|
+ BluetoothClient.ins.Log("接收数据\n" + byteStrList);
|
|
|
+ BluetoothClient.onDataReceived?.Invoke(byte.Parse(sign), bytes);
|
|
|
+ }
|
|
|
}
|