解决蓝牙设备丢失以后状态不变的问题
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user