| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- using ArduinoBluetoothAPI;
- using System;
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using DG.Tweening;
- /* 蓝牙瞄准模块 */
- public class BluetoothAim : MonoBehaviour
- {
- readonly string targetDeviceName = "Bbow_20210501";
- string targetDeviceService {
- get {
- if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3) {
- #if UNITY_ANDROID
- return "0000fff0";
- #else
- return "fff0";
- #endif
- }
- return "6e400001";
- }
- }
- string targetDeviceCharacteristicWrite {
- get {
- if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3) {
- #if UNITY_ANDROID
- return "0000fff2";
- #else
- return "fff2";
- #endif
- }
- return "6e400002";
- }
- }
- string targetDeviceCharacteristicNotify {
- get {
- if (CommonConfig.devicePlan == 0 || CommonConfig.devicePlan == 3) {
- #if UNITY_ANDROID
- return "0000fff1";
- #else
- return "fff1";
- #endif
- }
- return "6e400003";
- }
- }
- BluetoothHelper bluetoothHelper;
- BluetoothHelperCharacteristic characteristicWrite;
- BluetoothHelperService bluetoothService;
- string deviceName = "";
- bool canConnect = true;
- [SerializeField] Text textUI;
- public BluetoothStatusEnum status = BluetoothStatusEnum.Connect;
- int dataCount = 0;
- public bool hasData = false;
- public long hasDataTime;
- public static bool scanLock = false; //防止同时扫描冲突
- public static BluetoothAim ins;
- void Start()
- {
- ins = this;
- InitAutoDormancy();
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
- new GameObject("BleUDP").AddComponent<BleUDP>();
- #endif
- }
- void OnDestroy()
- {
- DisconnectBleHelper();
- }
- private bool userDoConnect = false;
- private bool doConnect = false;
- public Func<bool> action_DoConnectInterceptor;
- public void DoConnect()
- {
- if (action_DoConnectInterceptor != null) {
- if (action_DoConnectInterceptor.Invoke()) return;
- }
- if (status == BluetoothStatusEnum.Connect)
- {
- connectCanceled = false;
- userDoConnect = true;
- doConnect = true;
- SetStatus(BluetoothStatusEnum.Connecting);
- }
- else if (status == BluetoothStatusEnum.ConnectSuccess)
- {
- userDoConnect = false;
- doConnect = false;
- OnDisconnect();
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
- BleUDP.ins.Disconnect();
- #else
- DisconnectBleHelper();
- #endif
- }
- }
- //连接取消,退回登录时需要取消连接(扫描)
- [NonSerialized] public bool connectCanceled = false;
- void OnDisconnect()
- {
- curMac = null;
- dataCount = 0;
- hasData = false;
- canConnect = true;
- SetStatus(BluetoothStatusEnum.ConnectFail);
- BowCamera.isTouchMode = true;
- DestroyWhenDisconenct();
- if (AimHandler.ins) AimHandler.ins.SetMsOldDefault();
- }
- float notUserDoConnectTime = 0;
- void Update()
- {
- if (userDoConnect && status == BluetoothStatusEnum.Connect)
- {
- DoConnect();
- }
- if (doConnect) Connect();
- if (CommonConfig.StandaloneMode)
- {
- if (!userDoConnect && status == BluetoothStatusEnum.Connect)
- {
- if (notUserDoConnectTime == 0)
- {
- DoConnect();
- notUserDoConnectTime = 0.1f;
- }
- else
- {
- notUserDoConnectTime += Time.deltaTime;
- if (notUserDoConnectTime > 2.5f)
- {
- DoConnect();
- notUserDoConnectTime = 0.1f;
- }
- }
- }
- }
- }
- void SetStatus(BluetoothStatusEnum statusValue)
- {
- status = statusValue;
- if (status == BluetoothStatusEnum.ConnectFail)
- {
- Sequence sequence = DOTween.Sequence();
- sequence.AppendInterval(2f);
- sequence.AppendCallback(delegate ()
- {
- if (status == BluetoothStatusEnum.ConnectFail)
- {
- status = BluetoothStatusEnum.Connect;
- }
- });
- sequence.SetUpdate(true);
- SimulateMouseController.ins?.SetBleConnected(false);
- } else if (status == BluetoothStatusEnum.ConnectSuccess) {
- SimulateMouseController.ins?.SetBleConnected(true);
- }
- }
- void DisconnectBleHelper()
- {
- if (bluetoothHelper != null) bluetoothHelper.Disconnect();
- }
- void Connect()
- {
- if (BluetoothShoot.scanLock)
- {
- return;
- }
- if (!canConnect)
- {
- return;
- }
- doConnect = false;
- scanLock = true;
- canConnect = false;
- _scanCanRetryCount = 4;
- SetStatus(BluetoothStatusEnum.Connecting);
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
- ConnectBleByUDP();
- #else
- ConnectBleHelper();
- #endif
- }
- int _scanCanRetryCount = 4;
- void ConnectBleHelper()
- {
- #if UNITY_ANDROID
- PopupMgr.ins.ClearAllTip();
- if (BluetoothHelperAndroid.IsBluetoothEnabled() == false)
- {
- HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception1"));
- return;
- }
- if (BluetoothHelperAndroid.RequestBluetoothPermissions(ConnectBleHelper, (permission) => {
- if (permission.Contains("LOCATION"))
- {
- HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception2"));
- }
- else if (permission.Contains("BLUETOOTH"))
- {
- HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-exception3"));
- }
- })) return;
- #endif
- try
- {
- BluetoothHelper.BLE = true;
- bluetoothHelper = BluetoothHelper.GetNewInstance();
- bluetoothHelper.OnConnected += (BluetoothHelper helper) =>
- {
- Log("连接成功\n" + helper.getDeviceName());
- SetStatus(BluetoothStatusEnum.ConnectSuccess);
- if (connectCanceled) {
- Debug.Log("ble connectCanceled");
- DoConnect();
- return;
- }
- BowCamera.isTouchMode = false;
- foreach (BluetoothHelperService service in helper.getGattServices())
- {
- if (service.getName().ToLower().StartsWith(targetDeviceService))
- {
- bluetoothService = service;
- foreach (BluetoothHelperCharacteristic characteristic in service.getCharacteristics())
- {
- if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicWrite))
- {
- characteristicWrite = characteristic;
- }
- else if (characteristic.getName().ToLower().StartsWith(targetDeviceCharacteristicNotify))
- {
- BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristic.getName());
- ch.setService(bluetoothService.getName());
- bluetoothHelper.Subscribe(ch);
- }
- }
- }
- }
- // CallDelay(1, OpenInfrared);
- // CallDelay(2, OpenReceiveData);
- // CallDelay(3, RequestBattery);
- CallDelay(2, () =>
- {
- if (status != BluetoothStatusEnum.ConnectSuccess) return;
- InitWhenConenct();
-
- });
- };
- bluetoothHelper.OnConnectionFailed += (BluetoothHelper helper) =>
- {
- Log("连接失败\n" + helper.getDeviceName());
- OnDisconnect();
- };
- bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
- {
- if (status != BluetoothStatusEnum.ConnectSuccess) return;
- // logger.Log(String.Join(",", value));
- if (!hasData) {
- hasData = true;
- hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
- }
- dataCount++;
- if (curMac == null && dataCount < 500) {
- UploadMacAddress(value);
- }
- byte[] bytes = value;
- // Log(String.Join(",", bytes));
- BluetoothClient.UploadData(0, bytes);
- if (AimHandler.ins)
- {
- AimHandler.ins.OnDataReceived(bytes);
- }
- };
- bluetoothHelper.OnScanEnded += (BluetoothHelper helper, LinkedList<BluetoothDevice> nearbyDevices) =>
- {
- scanLock = false;
- if (connectCanceled) {
- userDoConnect = false;
- canConnect = true;
- status = BluetoothStatusEnum.Connect;
- Debug.Log("ble connectCanceled");
- return;
- }
- foreach (BluetoothDevice device in nearbyDevices)
- {
- Log("发现设备 " + device.DeviceName);
- if (device.DeviceName == targetDeviceName)
- {
- deviceName = device.DeviceName;
- bluetoothHelper.setDeviceName(deviceName);
- bluetoothHelper.Connect();
- Log("匹配设备 " + device.DeviceName);
- return;
- }
- }
- if (_scanCanRetryCount > 0) {
- _scanCanRetryCount--;
- scanLock = true;
- ConnectBleHelper();
- } else {
- userDoConnect = false;
- canConnect = true;
- Log("没有发现设备");
- TextAutoLanguage2.GetTextByKey("ble-dev-notfound");
- SetStatus(BluetoothStatusEnum.ConnectFail);
- }
- };
- bluetoothHelper.ScanNearbyDevices();
- Log("正在扫描设备");
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- HandleConnectException(TextAutoLanguage2.GetTextByKey("ble-please-open-ble"));
- }
- }
- void HandleConnectException(string errorText)
- {
- scanLock = false;
- canConnect = true;
- // SetStatus(BluetoothStatusEnum.ConnectFail);
- status = BluetoothStatusEnum.Connect;
- userDoConnect = false;
- PopupMgr.ins.ShowTip(errorText);
- }
- void ConnectBleByUDP()
- {
- try
- {
- BleUDP.ins.OnConnected = () =>
- {
- Log("连接成功\n" + deviceName);
- SetStatus(BluetoothStatusEnum.ConnectSuccess);
- BowCamera.isTouchMode = false;
- InitWhenConenct();
- };
- BleUDP.ins.OnConnectionFailed = () =>
- {
- Log("连接失败\n" + deviceName);
- OnDisconnect();
- };
- BleUDP.ins.OnCharacteristicChanged = (byte[] value) =>
- {
- if (!hasData) {
- hasDataTime = JCUnityLib.TimeUtils.GetTimestamp();
- UploadMacAddress(value);
- }
- hasData = true;
- byte[] bytes = value;
- // Log(String.Join(",", bytes));
- BluetoothClient.UploadData(0, bytes);
- if (AimHandler.ins)
- {
- AimHandler.ins.OnDataReceived(bytes);
- }
- };
- BleUDP.ins.OnScanEnded = () =>
- {
- scanLock = false;
- deviceName = targetDeviceName;
- BleUDP.ins.Connect();
- Log("发现设备\n" + deviceName);
- };
- BleUDP.ins.ScanNearbyDevices();
- }
- catch (Exception e)
- {
- Debug.LogError(e.Message);
- Debug.LogError(e.StackTrace);
- scanLock = false;
- canConnect = true;
- SetStatus(BluetoothStatusEnum.ConnectFail);
- }
- }
- #region 自动进入/退出休眠状态, 这里做程指令发送队列,为了控制连续发送指令的间隔,避免硬件收不到或处理不过来
- class CmdToSend
- {
- public string[] cmds;
- public Action onComplete;
- public Func<bool> canDo;
- public CmdToSend(string[] cmds, Action onComplete, Func<bool> canDo)
- {
- this.cmds = cmds;
- this.onComplete = onComplete;
- this.canDo = canDo;
- }
- }
- Queue<CmdToSend> cmdWaitingList = new Queue<CmdToSend>();
- bool isSendCmdLocked = false;
- bool canAutoDormancy = false;
- bool isStartUp = false;
- JCUnityLib.CountLock needModularAwake = new JCUnityLib.CountLock();
- void CheckAndStartUp()
- {
- if (needModularAwake.IsLocked())
- {
- StartUp();
- }
- else
- {
- Dormancy();
- }
- }
- void InitAutoDormancy()
- {
- // GlobalEventCenter.ins.onGameSceneLoad += () => {
- // needModularAwake.Lock();
- // CheckAndStartUp();
- // };
- // GlobalEventCenter.ins.onGameSceneDestroy += () => {
- // needModularAwake.Unlock();
- // CheckAndStartUp();
- // };
- // GlobalEventCenter.ins.onSimulateMouseAwakeChanged += (waked) => {
- // if (waked) needModularAwake.Lock();
- // else needModularAwake.Unlock();;
- // CheckAndStartUp();
- // };
- // GlobalEventCenter.ins.onDeviceCalibrateViewAwakeChanged += (waked) => {
- // if (waked) needModularAwake.Lock();
- // else needModularAwake.Unlock();;
- // CheckAndStartUp();
- // };
- //暂时关闭自动休眠,默认是需要模块保持激活
- needModularAwake.Lock();
- }
- void InitWhenConenct()
- {
- canAutoDormancy = true;
- List<string> cmds = new List<string>();
- cmds.Add("M"); //获取Mac地址
- cmds.Add("b"); //确保开启stm32
- cmds.Add("b"); //获取初始电量
- cmds.Add("1"); //开启发送逻辑
- Action onComplete = null;
- if (needModularAwake.IsLocked())
- {
- cmds.Add("w"); //红外灯开启
- cmds.Add("3"); //九轴开启
- onComplete = () =>
- {
- isStartUp = true;
- };
- }
- else
- {
- cmds.Add("s"); //红外灯关闭
- cmds.Add("S"); //Stm32关闭
- cmds.Add("4"); //九轴关闭
- onComplete = () =>
- {
- isStartUp = false;
- };
- }
- SendCDM(null, onComplete, cmds.ToArray());
- }
- void DestroyWhenDisconenct()
- {
- canAutoDormancy = false;
- sendCMD_CheckAndDoStop(null);
- }
- //启动
- void StartUp()
- {
- SendCDM(() =>
- {
- return !isStartUp;
- }, () =>
- {
- isStartUp = true;
- }, "b", "w", "3");
- }
- //休眠
- void Dormancy()
- {
- SendCDM(() =>
- {
- return isStartUp;
- }, () =>
- {
- isStartUp = false;
- }, "4", "s", "S");
- }
- void SendCDM(Func<bool> canDo, Action onComplete, params string[] cmds)
- {
- CmdToSend cmdToSend = new CmdToSend(cmds, onComplete, canDo);
- if (isSendCmdLocked)
- {
- cmdWaitingList.Enqueue(cmdToSend);
- return;
- }
- sendCMD_NotCheck(cmdToSend);
- }
- void sendCMD_NotCheck(CmdToSend cmdToSend)
- {
- if (cmdToSend.canDo != null && !cmdToSend.canDo.Invoke())
- {
- sendCMD_CheckNext();
- return;
- }
- isSendCmdLocked = true;
- Sequence sequence = DOTween.Sequence();
- sequence.PrependInterval(0.3f);
- foreach (var cmd in cmdToSend.cmds)
- {
- sequence.AppendCallback(() =>
- {
- bool stopped = sendCMD_CheckAndDoStop(sequence);
- if (!stopped) WriteData(cmd);
- });
- sequence.AppendInterval(0.5f);
- }
- sequence.AppendCallback(() =>
- {
- bool stopped = sendCMD_CheckAndDoStop(sequence);
- if (!stopped)
- {
- isSendCmdLocked = false;
- cmdToSend.onComplete?.Invoke();
- sendCMD_CheckNext();
- }
- });
- sequence.SetUpdate(true);
- }
- void sendCMD_CheckNext()
- {
- if (cmdWaitingList.Count <= 0) return;
- CmdToSend cmdToSend = cmdWaitingList.Dequeue();
- sendCMD_NotCheck(cmdToSend);
- }
- bool sendCMD_CheckAndDoStop(Sequence sequence)
- {
- if (canAutoDormancy) return false;
- isStartUp = false;
- isSendCmdLocked = false;
- cmdWaitingList.Clear();
- if (sequence != null) sequence.Kill();
- return true;
- }
- #endregion
- public void RequestBattery()
- {
- if (!isStartUp) return;
- if (isSendCmdLocked) return;
- WriteData("b");
- }
- public void ReplyInfraredShoot()
- {
- if (isSendCmdLocked) return;
- WriteData("I");
- }
- void CallDelay(float delayTime, TweenCallback callback)
- {
- Sequence sequence = DOTween.Sequence();
- sequence.PrependInterval(delayTime).AppendCallback(callback);
- sequence.SetUpdate(true);
- }
- public void WriteData(string data)
- {
- #if UNITY_STANDALONE_WIN || UNITY_EDITOR
- BleUDP.ins.SendMsg(data);
- #else
- if (DebugDeviceCMD.ins) DebugDeviceCMD.ins.ShowCMD(data);
- BluetoothHelperCharacteristic ch = new BluetoothHelperCharacteristic(characteristicWrite.getName());
- ch.setService(bluetoothService.getName());
- bluetoothHelper.WriteCharacteristic(ch, data);
- #endif
- }
- void Log(string text)
- {
- if (textUI)
- {
- textUI.text = text;
- }
- Debug.Log(string.Format("[{0}]{1}", typeof(BluetoothAim).Name, text));
- }
- [NonSerialized] public string curMac;
- void UploadMacAddress(byte[] bytes) {
- string mac = System.Text.Encoding.ASCII.GetString(bytes);
- if (mac != null) mac = mac.Trim();
- if (CheckIsMacValid(mac)) {
- curMac = mac;
- SideTipView.ShowTip("Mac获取成功:" + mac, Color.white);
-
- } else {
- SideTipView.ShowTip("Mac获取失败", Color.yellow);
- }
- }
- bool CheckIsMacValid(string mac) {
- if (mac == null) return false;
- if (!mac.StartsWith("{")) return false;
- if (!mac.EndsWith("}")) return false;
- if (!mac.Contains(":")) return false;
- char[] validChars = {'{','}',':','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- foreach (var c in mac.ToCharArray()) {
- if (Array.IndexOf(validChars, c) == -1) return false;
- }
- if (mac.Length != 19) return false;
- string macNoneFrame = mac.Substring(1, mac.Length - 2);
- string[] macNoneFrameSplits = macNoneFrame.Split(':');
- if (macNoneFrameSplits.Length != 6) return false;
- foreach (var item in macNoneFrameSplits) {
- if (item.Length != 2) return false;
- foreach (var c in item.ToCharArray())
- if (Array.IndexOf(validChars, c) < 3) return false;
- }
- return true;
- }
- }
|