| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using SmartBowSDK;
- using UnityEngine.UI;
- using System;
- using static ScreenLocate;
- namespace InfraredDLLManager
- {
- public class BLEManager : MonoBehaviour
- {
- #region 线上默认自动连接扫描
- //默认自动连接扫描
- [Header("连接扫描属性")]
- [Tooltip("是否自动连接扫描")]
- [SerializeField]
- bool autoConnectAndScan = false;
- bool isConnectName = true;//默认用名字连接
- string connectMacStr;//当前需要连接的mac
- private float notUserDoConnectTime = 0f;
- private float autoConnectInterval = 5f; // 自动连接间隔时间,单位秒
- #endregion
- //数据存储标识
- string userTags = "test";
- //建立的设备信息id.
- int deviceId = 1;
- public Text fpsText;
- public float shootSpeed = 0;
- #region log输出
- public Action<string> OnLogAction;
- void logOutBow(string text)
- {
- Debug.Log("logOutBow:" + text);
- OnLogAction?.Invoke(text);
- }
- #endregion
- // Start is called before the first frame update
- void Start()
- {
- InitBluetoothObj();
- InitBluetoothObj2();
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- public void SetFPS(float fps) {
- if(fpsText) fpsText.text = "FPS:"+ fps.ToString("F2");
- }
- //弓箭蓝牙
- SmartBowHelper BowSmartBowHelper;
- public SmartBowHelper MyBowSmartBowHelper => BowSmartBowHelper;
- void InitBluetoothObj()
- {
- SmartBowLogger.isDebug = true;
- BowSmartBowHelper = SmartBowHelper.NewInstance();
- BowSmartBowHelper.OnBluetoothModuleInited += OnBluetoothModuleInited;
- BowSmartBowHelper.OnBluetoothError += OnBluetoothError;
- BowSmartBowHelper.OnShooting += OnShooting1P;
- BowSmartBowHelper.OnBleDeviceState += OnBleDeviceState;
- BowSmartBowHelper.OnDeviceAndSystemInfoEvent += OnDeviceAndSystemInfoEvent;
- BowSmartBowHelper.OnFunctionKeyPress += OnFunctionKeyPress;
- BowSmartBowHelper.OnFunctionKeyLongPress += OnFunctionKeyLongPress;
- //进去开启自动搜索连接
- //autoConnectAndScan = true;
- //buttonScanType = ButtonScanType.AutoConnectBtn;
- //Debug.Log("开启自动搜索连接 ButtonScanType:" + buttonScanType);
- }
- SmartBowHelper BowSmartBowHelper2;
- public SmartBowHelper MyBowSmartBowHelper2 => BowSmartBowHelper2;
- void InitBluetoothObj2()
- {
- BowSmartBowHelper2 = SmartBowHelper.NewInstance();
- BowSmartBowHelper2.OnBluetoothModuleInited =()=> {
- Debug.Log("OnBluetoothModuleInited2");
- logOutBow("2P 设备 Mac:" + BowSmartBowHelper2.GetMacAddress() + "\n电量:" + BowSmartBowHelper2.GetBattery());
- };
- BowSmartBowHelper2.OnBluetoothError += OnBluetoothError;
- BowSmartBowHelper2.OnShooting += OnShooting2P;
- BowSmartBowHelper2.OnFunctionKeyPress += OnFunctionKeyPress;
- BowSmartBowHelper2.OnFunctionKeyLongPress += OnFunctionKeyLongPress;
- }
- void OnBluetoothModuleInited()
- {
- Debug.Log("OnBluetoothModuleInited:");
- BowSmartBowHelper.StartRotationSensor();
- BowSmartBowHelper.StartShootingSensor();
- logOutBow("1P 设备 Mac:" + BowSmartBowHelper.GetMacAddress() +"\n电量:"+ BowSmartBowHelper.GetBattery());
- }
- void OnBluetoothError(BluetoothError error, string message)
- {
- Debug.Log("OnBluetoothError:" + error);
- logOutBow("连接失败:" + error);
- if (error == BluetoothError.ScanNotFoundTargetDevice)
- {
- TipText.Show("连接失败,未发现目标设备!");
- return;
- }
- TipText.Show(message);
- }
- //加一个枪射击的触发时间
- float _lastShootTime = 0;
- void OnShooting1P(float speed)
- {
- //Bow.Instance.Shoot();
- //流程触发红外描点
- InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
- if (infraredScreenPositioningView)
- {
- if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
- _lastShootTime = Time.realtimeSinceStartup;
- InvokeOnCrossBtnEvent();
- }
- else
- {
- shootSpeed = speed;
- Debug.Log("1P射击速度:" + shootSpeed);
- }
- logOutBow("1P射击速度:" + shootSpeed);
- }
- void OnShooting2P(float speed)
- {
- logOutBow("2P射击速度:" + shootSpeed);
- }
- void OnFunctionKeyPress()
- {
- Debug.Log("功能键短按");
- InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType<InfraredScreenPositioningView>();
- if (infraredScreenPositioningView)
- {
- InvokeOnCrossBtnEvent();
- }
- else
- {
- //显示控制准心按钮
-
- }
- }
- void OnFunctionKeyLongPress()
- {
- Debug.Log("HOUYI Pro使用长按视角归位键;在ARTEMIS Pro使用快速双击按键");
- }
- /// <summary>
- /// 设备类型和设备操作,比如上膛,换弹夹等
- /// </summary>
- /// <param name="bluetoothDeviceType"></param>
- /// <param name="bluetoothDeviceStatus"></param>
- void OnBleDeviceState(BluetoothDeviceType bluetoothDeviceType, BluetoothDeviceStatus bluetoothDeviceStatus)
- {
- logOutBow("设备类型:" + bluetoothDeviceType + ",状态:" + bluetoothDeviceStatus);
- }
- /// <summary>
- /// 设备类型和设备信息
- /// </summary>
- /// <param name="connectPlatform"></param>
- /// <param name="bluetoothDeviceType"></param>
- void OnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bluetoothDeviceType)
- {
- logOutBow("当前设备平台:"+ connectPlatform+",设备类型:" + bluetoothDeviceType);
- }
-
- /// <summary>
- /// 根据名字搜索
- /// </summary>
- public void ConnectBluetoothDevice(SmartBowHelper _BowSmartBowHelper)
- {
- connectMacStr = "";
- _BowSmartBowHelper.SetFilters("HOUYI Pro | ARTEMIS Pro | Pistol | Pistol M9");
- connectBLEDevice(_BowSmartBowHelper);
- }
- //根据mac匹配
- public void ConnectBluetoothDeviceByMac(SmartBowHelper _BowSmartBowHelper,string mac)
- {
- connectMacStr = mac;
- connectBLEDevice(_BowSmartBowHelper);
- }
- void connectBLEDevice(SmartBowHelper _BowSmartBowHelper)
- {
- _BowSmartBowHelper.SetIsConnectName(isConnectName);
- _BowSmartBowHelper.SetConnectMacStr(connectMacStr);
- _BowSmartBowHelper.Connect(userTags, deviceId, true);
- }
- /// <summary>
- /// 自动连接设备,根据是否使用名称或MAC地址连接
- /// </summary>
- private void AutoConnectDevice()
- {
- if (isConnectName)
- {
- Debug.Log("自动根据名称连接设备...");
- ConnectBluetoothDevice(BowSmartBowHelper);
- }
- //else if (!string.IsNullOrEmpty(connectMacStr))
- //{
- // Debug.Log($"自动根据MAC连接设备: {connectMacStr}");
- // ConnectBluetoothDeviceByMac(connectMacStr);
- //}
- }
- public void DisConnectBLEDevice()
- {
- logOutBow("DisConnectBLEDevice");
- BluetoothStatusEnum _currentStatus = BowSmartBowHelper.GetBluetoothStatus();
- if (_currentStatus == BluetoothStatusEnum.Connected)
- {
- BowSmartBowHelper.Disconnect();
- }
- }
- public OnCrossBtnEventEvent OnCrossBtnEvent;
- public delegate void OnCrossBtnEventEvent();
- /// <summary>
- /// 准心事件
- /// </summary>
- public void InvokeOnCrossBtnEvent()
- {
- try
- {
- OnCrossBtnEvent?.Invoke();
- }
- catch (System.Exception e)
- {
- Debug.LogError(e);
- }
- }
- public void ButtonShoot()
- {
- //posAction?.Invoke(new Vector3(1 * Screen.width, 1 * Screen.height, 0));
- ////调用游戏中的射箭接口
- //if (ArmBow.ins)
- //{
- // ArmBow.ins.ADS_fire(true);
- //}
- //else
- //{
- // OnGameShoot?.Invoke(10);
- //}
- }
- }
- }
|