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 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(); 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(); if (infraredScreenPositioningView) { InvokeOnCrossBtnEvent(); } else { //显示控制准心按钮 } } void OnFunctionKeyLongPress() { Debug.Log("HOUYI Pro使用长按视角归位键;在ARTEMIS Pro使用快速双击按键"); } /// /// 设备类型和设备操作,比如上膛,换弹夹等 /// /// /// void OnBleDeviceState(BluetoothDeviceType bluetoothDeviceType, BluetoothDeviceStatus bluetoothDeviceStatus) { logOutBow("设备类型:" + bluetoothDeviceType + ",状态:" + bluetoothDeviceStatus); } /// /// 设备类型和设备信息 /// /// /// void OnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bluetoothDeviceType) { logOutBow("当前设备平台:"+ connectPlatform+",设备类型:" + bluetoothDeviceType); } /// /// 根据名字搜索 /// 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); } /// /// 自动连接设备,根据是否使用名称或MAC地址连接 /// 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(); /// /// 准心事件 /// 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); //} } } }