| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System;
- using UnityEngine;
- using UnityEngine.UI;
- using JCEngineCore;
- [Serializable]
- class JCData {
- public string uuid;
- public int type;
- public string func;
- public string[] args;
- }
- public class BluetoothClient : MonoBehaviour
- {
- [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;
- JCEngine.bootNew("ws://" + serverIP + ":9888/BLE", client);
- }
- public static void UploadData(byte sign, byte[] bytes)
- {
- if (ins && ins.client.isValid)
- {
- string data = String.Join(",", bytes);
- ins.client.call("uploadData", sign.ToString(), data);
- ins.Log("正在上传数据\n" + data);
- }
- }
- public static void UploadError(params string[] errors) {
- if (ins) {
- ins.client.call("showError", string.Join("\n", errors));
- }
- }
- 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);
- }
- }
|