解决蓝牙设备丢失以后状态不变的问题
This commit is contained in:
1 parent
15e440715d
commit
5f7cc43593
43 files changed
+114
-5363
No files matched your search
@@ -35,6 +35,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
{
|
||||
this.hwInterface = bleWinHwInterface;
|
||||
this.hwInterface.BluetoothStateChangedEvent += BluetoothStateChangedEvent;
|
||||
this.hwInterface.PeripheralDisconnectedEvent += HwInterface_PeripheralDisconnectedEvent;
|
||||
this.peripheralInfo = peripheralInfo;
|
||||
|
||||
base.Sensor = sensor;
|
||||
@@ -48,6 +49,22 @@ namespace Assets.Scripts.Devices.Ble
|
||||
Characteristics.Add(new ModelNumberCharacteristic());
|
||||
}
|
||||
|
||||
private void HwInterface_PeripheralDisconnectedEvent(BleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect)
|
||||
{
|
||||
if (this.State != DeviceState.Disconnected)
|
||||
{
|
||||
//Debug.Log("111111111111111111111111111111111111111111111111111111111111");
|
||||
this.State = DeviceState.Disconnected;
|
||||
|
||||
if (!manualDisconnect)
|
||||
{
|
||||
this.ConnectToPeripheralIfPossible();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
}
|
||||
|
||||
private void ConnectToPeripheralIfPossible()
|
||||
{
|
||||
if(this.peripheralInfo == null)
|
||||
@@ -85,6 +102,17 @@ namespace Assets.Scripts.Devices.Ble
|
||||
this.State = DeviceState.Disconnected;
|
||||
}
|
||||
|
||||
private void Disconnect()
|
||||
{
|
||||
if (this.State != DeviceState.Disconnected)
|
||||
{
|
||||
Debug.Log("断开设备" + this.Name);
|
||||
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, () => {
|
||||
this.State = DeviceState.Disconnected;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void ServicesDiscoveredAction(BleWinHwInterface hwInterface, BlePeripheralInfo sender, BleResponse<List<BleServiceInfo>> response)
|
||||
{
|
||||
//Debug.Log("搜索service");
|
||||
@@ -196,7 +224,6 @@ namespace Assets.Scripts.Devices.Ble
|
||||
// service = new CyclingPowerService(serviceInfo, this.hwInterface);
|
||||
// this.AddService(service, response);
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
private void AddService(BleService service, BleResponse<List<BleCharacteristicInfo>> scanCharacteristicsResponse = null)
|
||||
@@ -227,18 +254,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
|
||||
private void CheckAndSetConnectionStatus()
|
||||
{
|
||||
}
|
||||
|
||||
private void Disconnect()
|
||||
{
|
||||
if (this.State != DeviceState.Disconnected)
|
||||
{
|
||||
Debug.Log("断开设备" + this.Name);
|
||||
this.hwInterface.DisconnectPeripheral(this.peripheralInfo, ()=> {
|
||||
this.State = DeviceState.Disconnected;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void BluetoothStateChangedEvent(BleWinHwInterface hwInterface, BleState state)
|
||||
{
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
{
|
||||
//TODO:取消注释,自动连接设备
|
||||
//Debug.Log("自动连接" + device111.Id);
|
||||
//device111.Connect();
|
||||
device111.Connect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
||||
{
|
||||
discoveredDevices[device.Peripheral.Address].SignalStrength = device.Rssi;
|
||||
Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
|
||||
//Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -119,5 +119,21 @@ namespace Assets.Scripts.Devices.Ble
|
||||
item.Value.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除未连接的设备
|
||||
/// </summary>
|
||||
public void ClearDevice()
|
||||
{
|
||||
var list = discoveredDevices.ToList();
|
||||
foreach (var item in list)
|
||||
{
|
||||
if(item.Value.State == Ant.DeviceState.Disconnected)
|
||||
{
|
||||
hwInterface.pCache.Remove(item.Key);
|
||||
discoveredDevices.Remove(item.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,5 +7,5 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Assets.Scripts.Devices.Ble
|
||||
{
|
||||
public delegate void PeripheralDisconnectedCallback(BleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response);
|
||||
public delegate void PeripheralDisconnectedCallback(BleWinHwInterface hwInterface, BlePeripheralInfo peripheral, BleResponse response, bool manualDisconnect);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace Assets.Scripts.Ble
|
||||
private static BleWinHwInterface hwInterface;
|
||||
private WclBleMainThread wclBleMainThread;
|
||||
|
||||
private readonly Dictionary<string, BleAdvertisementInfo> pCache = new Dictionary<string, BleAdvertisementInfo>();
|
||||
public readonly Dictionary<string, BleAdvertisementInfo> pCache = new Dictionary<string, BleAdvertisementInfo>();
|
||||
|
||||
private Action<BleAdvertisementInfo> _discoveredCallback;
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace Assets.Scripts.Ble
|
||||
private Dictionary<BleCharacteristicInfo, CharacteristicReadCallback> characteristicReadCallbacks = new Dictionary<BleCharacteristicInfo, CharacteristicReadCallback>();
|
||||
private Dictionary<BleServiceInfo, CharacteristicsDiscoveredCallback> characteristicsDiscoveredCallbacks = new Dictionary<BleServiceInfo, CharacteristicsDiscoveredCallback>();
|
||||
|
||||
private Dictionary<BlePeripheralInfo, Action> disconnectedCallback = new Dictionary<BlePeripheralInfo, Action>();
|
||||
|
||||
private CharacteristicReadCallback characteristicReadEvent;
|
||||
public event CharacteristicReadCallback CharacteristicReadEvent
|
||||
@@ -236,9 +237,8 @@ namespace Assets.Scripts.Ble
|
||||
this.servicesCallbacks.Remove(peripheral);
|
||||
this.characteristicNotificationCallbacks.Remove(peripheral);
|
||||
|
||||
gattThread.Discounect();
|
||||
|
||||
callback?.Invoke();
|
||||
this.disconnectedCallback.Add(peripheral, callback);
|
||||
gattThread.Discounect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,6 +272,9 @@ namespace Assets.Scripts.Ble
|
||||
if (!response.IsSuccess)
|
||||
{
|
||||
gattClient.Stop();
|
||||
|
||||
this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
||||
this.callbacks.Remove(gattClient.Peripheral);
|
||||
return;
|
||||
}
|
||||
this.callbacks[gattClient.Peripheral].Invoke(this, gattClient.Peripheral, response);
|
||||
@@ -286,6 +289,22 @@ namespace Assets.Scripts.Ble
|
||||
private void GattDisconnected(WclBleGattThread gattClient, BleResponse response)
|
||||
{
|
||||
Debug.Log("gatt disconnected");
|
||||
|
||||
this.callbacks.Remove(gattClient.Peripheral);
|
||||
this.servicesCallbacks.Remove(gattClient.Peripheral);
|
||||
this.characteristicNotificationCallbacks.Remove(gattClient.Peripheral);
|
||||
|
||||
var manualDisconnect = disconnectedCallback.ContainsKey(gattClient.Peripheral);
|
||||
this.peripheralDisconnectedEvent(this, gattClient.Peripheral, response, manualDisconnect);
|
||||
|
||||
if (disconnectedCallback.ContainsKey(gattClient.Peripheral))
|
||||
{
|
||||
disconnectedCallback[gattClient.Peripheral].Invoke();
|
||||
disconnectedCallback.Remove(gattClient.Peripheral);
|
||||
}
|
||||
//this.pCache.Remove(gattClient.Peripheral.Address);
|
||||
|
||||
|
||||
}
|
||||
private void GattServicesDiscovered(WclBleGattThread gattClient, BleResponse<List<BleServiceInfo>> response)
|
||||
{
|
||||
|
||||
@@ -71,5 +71,20 @@ namespace Assets.Scripts.Devices
|
||||
item.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 清除未连接的设备
|
||||
/// </summary>
|
||||
public void ClearDevice()
|
||||
{
|
||||
foreach (var item in adapters)
|
||||
{
|
||||
if(item is BleDeviceAdapter)
|
||||
{
|
||||
(item as BleDeviceAdapter).ClearDevice();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,13 +68,12 @@ public class MainController : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
//UIManager.ShowHomePanel();
|
||||
UIManager.ShowHomePanel();
|
||||
//UIManager.ShowUserInfoPanel();
|
||||
//UIManager.ShowEditUserPanel();
|
||||
//UIManager.ShowBigMapPanel();
|
||||
//UIManager.ShowEarthPanel();
|
||||
}
|
||||
UIManager.ShowDevicePanel();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
|
||||
@@ -217,6 +217,12 @@ public class ConnectDeviceModal : PFUIPanel
|
||||
}
|
||||
}
|
||||
|
||||
public override void Show()
|
||||
{
|
||||
base.Show();
|
||||
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
base.Close();
|
||||
|
||||
@@ -60,16 +60,18 @@ public class DeviceItem : Selectable, IEventSystemHandler, IPointerClickHandler
|
||||
}
|
||||
//Debug.Log(DeviceInfo.SignalStrength);
|
||||
|
||||
//signal.enabled = true;
|
||||
|
||||
var signalValue = DeviceInfo.SignalStrength * -1;
|
||||
if (signalValue >= 0 && signalValue <= 30)
|
||||
if (signalValue >= 0 && signalValue <= 20)
|
||||
{
|
||||
signal.sprite = Resources.Load<Sprite>("Images/Signal_1");
|
||||
}
|
||||
else if (signalValue > 30 && signalValue <= 70)
|
||||
else if (signalValue > 20 && signalValue <= 60)
|
||||
{
|
||||
signal.sprite = Resources.Load<Sprite>("Images/Signal_2");
|
||||
}
|
||||
else if (signalValue > 70 && signalValue <= 97)
|
||||
else if (signalValue > 60 && signalValue <= 97)
|
||||
{
|
||||
signal.sprite = Resources.Load<Sprite>("Images/Signal_3");
|
||||
}
|
||||
|
||||
@@ -288,7 +288,15 @@ public class DeviceView : MonoBehaviour
|
||||
logo.sprite = sprite3;
|
||||
|
||||
title.text = connectedDevice.Name;
|
||||
noDevice.text = connectedDevice.DeviceNumber.ToString();
|
||||
if (connectedDevice.Network == NetworkType.BLE)
|
||||
{
|
||||
noDevice.enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
noDevice.enabled = true;
|
||||
noDevice.text = connectedDevice.DeviceNumber.ToString();
|
||||
}
|
||||
noDevice.color = Color.white;
|
||||
|
||||
if (powerValue != null)
|
||||
|
||||
@@ -190,8 +190,10 @@ public class DeviceController : PFUIPanel
|
||||
var cg = this.GetComponent<CanvasGroup>();
|
||||
cg.alpha = 0;
|
||||
base.Show();
|
||||
App.MainDeviceAdapter.ClearDevice();
|
||||
|
||||
cg.DOFade(1f, 0.3f);
|
||||
|
||||
cg.DOFade(1f, 0.3f);
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
|
||||
@@ -264,6 +264,9 @@ public class EditUserController : PFUIPanel
|
||||
|
||||
void SetDayDropdown()
|
||||
{
|
||||
Debug.Log(mDayDropdown.SelectedItem);
|
||||
|
||||
|
||||
mDayDropdown.ClearOptions();
|
||||
var year = mYearDropdown.SelectedItem;
|
||||
var month = mMonthDropdown.SelectedItem;
|
||||
|
||||
@@ -111,6 +111,7 @@ namespace Assets.Scripts
|
||||
{"Cadence","Cadence Sensor|Images/Devices/Cadence_3" },
|
||||
{"HeartRate","Heart Rate Monitor|Images/Devices/Heart Rate_3" },
|
||||
{"SpeedCadence","Speed Meter|Images/Devices/Speed_3" },
|
||||
{"Speed","Speed Meter|Images/Devices/Speed_3" }
|
||||
};
|
||||
}
|
||||
return _deviceDict;
|
||||
|
||||
Reference in new issue
Block a user