蓝牙断开重连相关逻辑优化
This commit is contained in:
1 parent
87a0e7fb6d
commit
f25d0b7b8c
11 files changed
+455
-27
No files matched your search
@@ -27,7 +27,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
_isEnabled = value;
|
||||
if (EnableChanged != null)
|
||||
{
|
||||
EnableChanged(_isEnabled, new EventArgs());
|
||||
EnableChanged(_isEnabled, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
_pullValue = value;
|
||||
if (this.PullChanged != null)
|
||||
{
|
||||
this.PullChanged(this, new EventArgs());
|
||||
this.PullChanged(this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,15 +68,15 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
else if (data[0] == 0x3d)
|
||||
{
|
||||
//老款表头不支持
|
||||
List<ushort> r = new List<ushort>();
|
||||
//List<ushort> r = new List<ushort>();
|
||||
for (int i = 3; i < data.Length; i += 2)
|
||||
{
|
||||
ushort pull = data[i] < 5 ? (ushort)0 : Convert.ToUInt16(Math.Round(LbsToNewton(data[i], true)));
|
||||
r.Add(pull);
|
||||
//r.Add(pull);
|
||||
PullValue = pull;
|
||||
}
|
||||
|
||||
Debug.Log("拉力曲线:" + string.Join(",", r));
|
||||
// Debug.Log("拉力曲线:" + string.Join(",", r));
|
||||
}
|
||||
else if (data[0] == 0x31)
|
||||
{
|
||||
@@ -102,11 +102,12 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
var time = ((data[3] << 16) + (data[2] << 8) + data[1]) / 100;
|
||||
Debug.Log("划船时间" + time);
|
||||
isReadyStatus = data[9] == 0;
|
||||
if (RowerResChanged != null)
|
||||
{
|
||||
RowerResChanged.Invoke(data[19], new EventArgs());
|
||||
}
|
||||
var hasChanged = data[19] != ResistanceLevel;
|
||||
ResistanceLevel = data[19];
|
||||
if (RowerResChanged != null && hasChanged)
|
||||
{
|
||||
RowerResChanged.Invoke(this, null);
|
||||
}
|
||||
//isReadyStatus = data[2] == 1 || data[2] == 129;
|
||||
}
|
||||
else if (data[0] == 0x32)
|
||||
@@ -163,7 +164,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
{
|
||||
if (_totalDistance == 0 && value != 0 && StartEvent != null)
|
||||
{
|
||||
StartEvent.Invoke(null, new EventArgs());
|
||||
StartEvent.Invoke(this, null);
|
||||
}
|
||||
_totalDistance = value;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
{
|
||||
if (_instantaneousPace == 0 && value != 0 && StartEvent != null)
|
||||
{
|
||||
StartEvent.Invoke(null, new EventArgs());
|
||||
StartEvent.Invoke(this, null);
|
||||
}
|
||||
_instantaneousPace = value;
|
||||
}
|
||||
@@ -104,7 +104,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
_pullValue = value;
|
||||
if (this.PullChanged != null)
|
||||
{
|
||||
this.PullChanged(this, new EventArgs());
|
||||
this.PullChanged(this, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,7 +200,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected) && d.Sensor == SensorType.Rower);
|
||||
if (resChanged && device != null && deviceNamePool.FirstOrDefault(x => device.Name.ToLower().Contains(x.ToLower())) == null && RowerResChanged != null)
|
||||
{
|
||||
RowerResChanged.Invoke(this.commonRes, null);
|
||||
RowerResChanged.Invoke(this, null);
|
||||
}
|
||||
b += this.SizeOfDataForFlag(RowerDataFlag.ResistanceLevel);
|
||||
}
|
||||
@@ -241,7 +241,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
var device = App.MainDeviceAdapter.GetDevices().FirstOrDefault(d => (d.State == DeviceState.Connected) && d.Sensor == SensorType.Rower);
|
||||
if (thinkResChanged && device != null && deviceNamePool.FirstOrDefault(x=>device.Name.ToLower().Contains(x.ToLower()))!=null && RowerResChanged != null)
|
||||
{
|
||||
RowerResChanged.Invoke(this.thinkRes, null);
|
||||
RowerResChanged.Invoke(this, null);
|
||||
}
|
||||
b += this.SizeOfDataForFlag(RowerDataFlag.ThinkDragFactor);
|
||||
}
|
||||
@@ -254,6 +254,8 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
tempPullList.Add(val);
|
||||
PullValue = val;
|
||||
}
|
||||
Debug.Log("拉力列表:" + string.Join(",", tempPullList));
|
||||
|
||||
if (pullList == null) pullList = new List<ushort>();
|
||||
foreach (var pull in tempPullList)
|
||||
{
|
||||
@@ -269,7 +271,7 @@ namespace Assets.Scripts.Devices.Ble.Characteristic
|
||||
pullList.Add(pull);
|
||||
}
|
||||
tempPullList.Clear();
|
||||
//Debug.Log("拉力:" + string.Join(",",list));
|
||||
|
||||
}
|
||||
}
|
||||
List<ushort> pullList { get; set; }
|
||||
|
||||
@@ -116,6 +116,10 @@ namespace Assets.Scripts.Ble
|
||||
private void ManagerStatusChanged(BleMobileThread sender, WclBleManagerStatus status)
|
||||
{
|
||||
this.BleState = BleMobileInterface.StateFromNativeState(status);
|
||||
if (this.BleState == BleState.Off)
|
||||
{
|
||||
ClearAllCallbacks();
|
||||
}
|
||||
}
|
||||
|
||||
private void WatcherScanInfoReceived(BleMobileThread sender, string address, string name, int rssi,string[] uuids)
|
||||
@@ -348,6 +352,7 @@ namespace Assets.Scripts.Ble
|
||||
BluetoothLEHardwareInterface.DisconnectPeripheral(peripheral.Address, (address) =>
|
||||
{
|
||||
Debug.Log("断开回调"+ address);
|
||||
PeripheralDisconnected(address,peripheral);
|
||||
peripheralDisconnectedEvent(this, peripheral, null, true);
|
||||
callback?.Invoke();
|
||||
});
|
||||
@@ -374,6 +379,17 @@ namespace Assets.Scripts.Ble
|
||||
{
|
||||
bleMobileThread?.Stop();
|
||||
}
|
||||
|
||||
public void ClearAllCallbacks()
|
||||
{
|
||||
callbacks?.Clear();
|
||||
servicesCallbacks?.Clear();
|
||||
characteristicNotificationCallbacks?.Clear();
|
||||
characteristicReadCallbacks?.Clear();
|
||||
characteristicsDiscoveredCallbacks?.Clear();
|
||||
_discoveredCallback = null;
|
||||
_pCache?.Clear();
|
||||
}
|
||||
|
||||
//发现服务列表
|
||||
public void DiscoverServices(BlePeripheralInfo peripheral, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse<List<BleServiceInfo>>> callback)
|
||||
|
||||
@@ -50,6 +50,8 @@ public class PFUISlider : MonoBehaviour
|
||||
}
|
||||
public void SetValue(float a)
|
||||
{
|
||||
a = a > 1 ? 1 : a;
|
||||
a = a < 0 ? 0 : a;
|
||||
slider.value = a;
|
||||
slider.onValueChanged?.Invoke(a);
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ public class ConnectDeviceModal : PFUIPanel
|
||||
private Dictionary<string, DeviceItem> deviceList;
|
||||
private Text noDevice,Title;
|
||||
private RectTransform searchIconRect;
|
||||
private GameObject deviceNotFound;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
@@ -52,6 +53,7 @@ public class ConnectDeviceModal : PFUIPanel
|
||||
deviceList = new Dictionary<string, DeviceItem>();
|
||||
|
||||
var container = this.transform.Find("GameObject");
|
||||
deviceNotFound = container.Find("Panel/Error").gameObject;
|
||||
var closeBtn = container.Find("CloseBtn");
|
||||
Logo = container.Find("Logo").GetComponent<Image>();
|
||||
Title = container.Find("Title").GetComponent<Text>();
|
||||
@@ -259,6 +261,7 @@ public class ConnectDeviceModal : PFUIPanel
|
||||
//如果没有可连接的设备按钮只读
|
||||
connectBtn.mButton.enabled = deviceList.Count > 0;
|
||||
connectBtn.mButton.interactable = deviceList.Count > 0;
|
||||
deviceNotFound.SetActive(deviceList.Count <= 0);
|
||||
}
|
||||
}
|
||||
//选中需要连接的设备
|
||||
|
||||
@@ -466,6 +466,10 @@ public class RowerHomeScript : PFUIPanel
|
||||
int truelyTime = 0;
|
||||
private void StartFunc(object sender, EventArgs e)
|
||||
{
|
||||
var rowdata = (IRowerCommonData)sender;
|
||||
if (rowdata != RowerData)
|
||||
return;
|
||||
|
||||
Debug.Log($"开始了:{openTimer}");
|
||||
if (UIManager.Instance.confirm != null && UIManager.Instance.confirm.IsActive())
|
||||
{
|
||||
@@ -626,6 +630,7 @@ public class RowerHomeScript : PFUIPanel
|
||||
|
||||
private void Init()
|
||||
{
|
||||
pointCount = 0;
|
||||
print("初始化");
|
||||
if (Rower != null)
|
||||
{
|
||||
@@ -734,20 +739,18 @@ public class RowerHomeScript : PFUIPanel
|
||||
transform.Find("ResBar/BtnSub").GetComponent<Button>().interactable = !(bool)sender;
|
||||
transform.Find("ResBar/BtnAdd").GetComponent<Button>().interactable = !(bool)sender;
|
||||
}
|
||||
bool initRes = false;
|
||||
|
||||
private void ResChanged(object sender, EventArgs e)
|
||||
{
|
||||
Debug.Log(Convert.ToSingle(sender));
|
||||
if (!initRes && slider)
|
||||
var originData = (IRowerCommonData)sender;
|
||||
if (slider && RowerData!= null && originData == RowerData)
|
||||
{
|
||||
var origin = Convert.ToSingle(sender);
|
||||
var origin = RowerData.ResistanceLevel;
|
||||
var wrapped = origin< 50 ? 50 : origin;
|
||||
slider.GetComponent<PFUISlider>().SetValue((wrapped - 50) / 300f);
|
||||
initRes = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool checkRowing()
|
||||
{
|
||||
if (seconds > 0)
|
||||
@@ -792,6 +795,7 @@ public class RowerHomeScript : PFUIPanel
|
||||
private Text rmyAvgForceText { get; set; }
|
||||
private Text PeakForceText { get; set; }
|
||||
private Text rmyPeakForceText { get; set; }
|
||||
private bool interruptFlag{ get; set; }
|
||||
|
||||
void TimerTicks()
|
||||
{
|
||||
@@ -801,21 +805,26 @@ public class RowerHomeScript : PFUIPanel
|
||||
PaintPullCurve((ushort)(a / 10));
|
||||
if (RowerData == null) return;
|
||||
#endif
|
||||
#if !UNITY_EDITOR
|
||||
//#if !UNITY_EDITOR
|
||||
if (Rower == null)
|
||||
{
|
||||
interruptFlag = true;
|
||||
//HandleDiscardDirect();
|
||||
isPause = true;
|
||||
UIManager.ShowConfirm(App.GetLocalString("Warning"), App.GetLocalString("Save the game?"), () =>
|
||||
{
|
||||
HandleSaveDirect();
|
||||
UIManager.CloseConfirm();
|
||||
interruptFlag = false;
|
||||
}, 2, ()=> {
|
||||
Init();
|
||||
interruptFlag = false;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (interruptFlag) return;
|
||||
if (RowerData == null) return;
|
||||
#endif
|
||||
//#endif
|
||||
var heartRate = HeartRate ?? 0;
|
||||
BPMText.text = heartRate.ToString();
|
||||
//断线重连继续
|
||||
@@ -1087,7 +1096,10 @@ public class RowerHomeScript : PFUIPanel
|
||||
|
||||
void PaintPullCurve(object sender, EventArgs e)
|
||||
{
|
||||
//防止设备断开重连rowData对象发生变化,事件重复触发
|
||||
var ftms = (IRowerCommonData)sender;
|
||||
if (RowerData != ftms)
|
||||
return;
|
||||
//Debug.Log(ftms.PullValue);
|
||||
PaintPullCurve(ftms.PullValue);
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class RowerDeviceView : MonoBehaviour
|
||||
/// </summary>
|
||||
private Sprite sprite3;
|
||||
|
||||
private float timer = 1.0f;
|
||||
private float timer = 0f;
|
||||
private void Awake()
|
||||
{
|
||||
title = this.transform.Find("Title").GetComponent<Text>();
|
||||
@@ -274,7 +274,7 @@ public class RowerDeviceView : MonoBehaviour
|
||||
|
||||
logo.sprite = sprite0;
|
||||
|
||||
searchState.text = App.GetLocalString("CHOOSE DEVICE");
|
||||
|
||||
var hasDevice = false;
|
||||
if (SensorType == SensorType.SpeedCadence)
|
||||
{
|
||||
@@ -284,6 +284,7 @@ public class RowerDeviceView : MonoBehaviour
|
||||
{
|
||||
hasDevice = App.MainDeviceAdapter.GetDevices().Any(d => d.Sensor == SensorType);
|
||||
}
|
||||
searchState.text = hasDevice ? App.GetLocalString("CHOOSE DEVICE") : App.GetLocalString("NO DEVICE");
|
||||
noDevice.text = "";// App.GetLocalString("NOCONN DEVICE"); //hasDevice ? App.GetLocalString("CHOOSE DEVICE") : ;
|
||||
if (hasDevice)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user