设备连接稳定性
This commit is contained in:
1 parent
c333086e30
commit
46b6c7fa01
9 files changed
+111
-62
No files matched your search
@@ -106,6 +106,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
|
||||
if (response.Error != null)
|
||||
{
|
||||
this.State = DeviceState.Disconnected;
|
||||
if (response.Error.Code == WclBleErrors.WCL_E_BLUETOOTH_LE_DEVICE_NOT_FOUND)
|
||||
{
|
||||
Debug.Log("未找到设备");
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.Scripts.Ble
|
||||
@@ -210,30 +211,41 @@ namespace Assets.Scripts.Ble
|
||||
|
||||
List<BleServiceInfo> servicelist = new List<BleServiceInfo>();
|
||||
List<BleCharacteristicInfo> characteristilist = new List<BleCharacteristicInfo>();
|
||||
|
||||
public void ConnectPeripheral(BlePeripheralInfo info, Action<IBleWinHwInterface, BlePeripheralInfo, BleResponse> callback)
|
||||
{
|
||||
if (!this.callbacks.ContainsKey(info))
|
||||
var exsit = this.callbacks.Where(c => c.Key.Address == info.Address).Any();
|
||||
if (!exsit)
|
||||
{
|
||||
this.callbacks.Add(info, callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
var obj = this.callbacks.Where(c => c.Key.Address == info.Address).FirstOrDefault();
|
||||
this.callbacks.Remove(obj.Key);
|
||||
this.callbacks.Add(info, callback);
|
||||
}
|
||||
Debug.Log("try connect device" + info.Name + this.callbacks.Count.ToString());
|
||||
var self = this;
|
||||
if (this.callbacks.Count == 1)
|
||||
{
|
||||
BleResponse s = new BleResponse();
|
||||
s.IsSuccess = true;
|
||||
s.Error = null;
|
||||
var self = this;
|
||||
BluetoothLEHardwareInterface.ConnectToPeripheral(info.Address, (address) =>
|
||||
{
|
||||
BleResponse s = new BleResponse
|
||||
{
|
||||
IsSuccess = true,
|
||||
Error = null
|
||||
};
|
||||
callback?.Invoke(self, info, s);
|
||||
this.callbacks.Remove(info);
|
||||
Debug.Log("连接成功!"+info.Name);
|
||||
Debug.Log("连接成功!" + info.Name);
|
||||
if (this.callbacks.Any())
|
||||
{
|
||||
this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value);
|
||||
}
|
||||
}, (address, service) =>
|
||||
{
|
||||
Debug.Log("发现服务");
|
||||
ServicesDiscovered(address, service);
|
||||
}, (address, service, characteristic) =>
|
||||
{
|
||||
@@ -242,9 +254,25 @@ namespace Assets.Scripts.Ble
|
||||
{
|
||||
Debug.Log("disconnect device:" + address);
|
||||
PeripheralDisconnected(address, info);
|
||||
});
|
||||
},
|
||||
(address, error) =>
|
||||
{
|
||||
BleResponse s = new BleResponse
|
||||
{
|
||||
IsSuccess = false,
|
||||
Error = new BleHwInterfaceError(error)
|
||||
};
|
||||
callback?.Invoke(self, info, s);
|
||||
this.callbacks.Remove(info);
|
||||
if (this.callbacks.Any())
|
||||
{
|
||||
this.ConnectPeripheral(this.callbacks.First().Key, this.callbacks.First().Value);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#region 私有方法
|
||||
//服务发现
|
||||
private void ServicesDiscovered(string address, string service)
|
||||
@@ -305,7 +333,7 @@ namespace Assets.Scripts.Ble
|
||||
}
|
||||
|
||||
var characteristicsDiscoveredCallback = characteristicsDiscoveredCallbacks.Where(c => c.Key.MatchAddress(address));
|
||||
if (serviceCallback.Any())
|
||||
if (characteristicsDiscoveredCallback.Any())
|
||||
{
|
||||
characteristicsDiscoveredCallbacks.Remove(characteristicsDiscoveredCallback.First().Key);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user