移动端蓝牙优化
This commit is contained in:
1 parent
5b54255777
commit
6542dd9882
10 files changed
+84
-16
No files matched your search
@@ -39,6 +39,10 @@ namespace Assets.Scripts.Devices
|
||||
/// </summary>
|
||||
public int SignalStrength { get; internal set; }
|
||||
|
||||
public DateTime LastActiveTime { get; internal set; }
|
||||
|
||||
public string ErrorMsg { get; internal set; }
|
||||
|
||||
protected AbstractDevice(string id, NetworkType networkType)
|
||||
{
|
||||
this.Id = id;
|
||||
@@ -46,7 +50,7 @@ namespace Assets.Scripts.Devices
|
||||
}
|
||||
|
||||
|
||||
public abstract void Connect();
|
||||
public abstract void Connect(Action<string> callback = null);
|
||||
|
||||
public abstract void Disconnect(bool save = true);
|
||||
}
|
||||
|
||||
@@ -166,12 +166,12 @@ namespace Assets.Scripts.Devices.Ant
|
||||
// base.reset();
|
||||
//}
|
||||
|
||||
public override void Connect()
|
||||
public override void Connect(Action<string> callback = null)
|
||||
{
|
||||
if (State == DeviceState.Connected || State == DeviceState.Connecting)
|
||||
return;
|
||||
Debug.Log("连接" + this.DeviceNumber);
|
||||
AntConnector.Instance().ConnectDevice(this);
|
||||
AntConnector.Instance().ConnectDevice(this, callback);
|
||||
}
|
||||
|
||||
public override void Disconnect(bool save = true)
|
||||
|
||||
@@ -294,6 +294,14 @@ namespace Assets.Scripts.Devices.Ant
|
||||
|
||||
void antChannel_channelResponse_FeSearch(ANT_Response response)
|
||||
{
|
||||
//删除扫描不到的设备
|
||||
var now = DateTime.Now;
|
||||
var needRemoveList = discoveredDevices.Where(c => (now - c.LastActiveTime).TotalSeconds > 5 && c.State == DeviceState.Disconnected).ToList();
|
||||
foreach (var item in needRemoveList)
|
||||
{
|
||||
discoveredDevices.Remove(item);
|
||||
}
|
||||
|
||||
if (IsBackgroundScanning == false)
|
||||
{
|
||||
searchChannel.closeChannel();
|
||||
@@ -318,6 +326,7 @@ namespace Assets.Scripts.Devices.Ant
|
||||
var device2 = discoveredDevices.FirstOrDefault(d => d.searchProfile.deviceNumber == deviceNumber && d.searchProfile.deviceType == deviceTypeId);
|
||||
if (device2 != null)
|
||||
{
|
||||
device2.LastActiveTime = DateTime.Now;
|
||||
//ReSearch();
|
||||
_action?.Invoke(device2);
|
||||
if (device2.State == DeviceState.Connected)
|
||||
@@ -371,6 +380,7 @@ namespace Assets.Scripts.Devices.Ant
|
||||
if (device != null)
|
||||
{
|
||||
//device.searchProfile.deviceNumber = (ushort)deviceNumber;
|
||||
device.LastActiveTime = DateTime.Now;
|
||||
device.DeviceNumber = (ushort)deviceNumber;
|
||||
discoveredDevices.Add(device);
|
||||
|
||||
@@ -412,9 +422,10 @@ namespace Assets.Scripts.Devices.Ant
|
||||
searchChannel.setChannelSearchTimeout(0);
|
||||
searchChannel.openChannel(500);
|
||||
}
|
||||
|
||||
public void ConnectDevice(AbstractAntDevice device)
|
||||
Action<string> connectCallBack;
|
||||
public void ConnectDevice(AbstractAntDevice device,Action<string> callBack = null)
|
||||
{
|
||||
this.connectCallBack = callBack;
|
||||
if (usedChannels.Any(u => u.DeviceNumber == device.searchProfile.deviceNumber.ToString() &&
|
||||
u.DeviceTypeId == device.searchProfile.deviceType.ToString()))
|
||||
return;
|
||||
|
||||
@@ -19,6 +19,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
{
|
||||
protected IBleWinHwInterface hwInterface;
|
||||
protected BlePeripheralInfo peripheralInfo;
|
||||
protected Action<string> connectCallBack;
|
||||
private readonly HashSet<BleService> services = new HashSet<BleService>();
|
||||
|
||||
private readonly HashSet<BleServiceInfo> pendingServices = new HashSet<BleServiceInfo>();
|
||||
@@ -98,6 +99,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
Debug.Log($"连接{ this.Name }"+response.IsSuccess);
|
||||
if (response.IsSuccess)
|
||||
{
|
||||
ErrorMsg = string.Empty;
|
||||
State = DeviceState.Connected;
|
||||
//Debug.Log("连接成功"+this.Name);
|
||||
hwInterface.DiscoverServices(this.peripheralInfo, ServicesDiscoveredAction);//, this.ServicesDiscoveredAction);
|
||||
@@ -106,6 +108,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
|
||||
if (response.Error != null)
|
||||
{
|
||||
ErrorMsg = App.GetLocalString("connect failed");
|
||||
this.State = DeviceState.Disconnected;
|
||||
if (response.Error.Code == WclBleErrors.WCL_E_BLUETOOTH_LE_DEVICE_NOT_FOUND)
|
||||
{
|
||||
@@ -113,7 +116,9 @@ namespace Assets.Scripts.Devices.Ble
|
||||
App.MainDeviceAdapter.ClearDevice(this.peripheralInfo.Address);
|
||||
}
|
||||
}
|
||||
|
||||
//无法与xx连接请检查后重拾
|
||||
var errorMsg = App.GetLocalLanguage() == "zh" ? $"无法与设备{Name}-{DeviceNumber}连接,请检查后重试。": $"failed to connect {Name}-{DeviceNumber},please check and retry.";
|
||||
connectCallBack?.Invoke(response.IsSuccess ? string.Empty : errorMsg);
|
||||
}
|
||||
|
||||
private void Disconnect()
|
||||
@@ -234,8 +239,9 @@ namespace Assets.Scripts.Devices.Ble
|
||||
}
|
||||
}
|
||||
|
||||
public override void Connect()
|
||||
public override void Connect(Action<string> callback = null)
|
||||
{
|
||||
this.connectCallBack = callback;
|
||||
this.ConnectToPeripheralIfPossible();
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
if(bleState == BleState.Off)
|
||||
{
|
||||
discoveredDevices.Clear();
|
||||
this.StopScan();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -158,8 +159,16 @@ namespace Assets.Scripts.Devices.Ble
|
||||
if (discoveredDevices.ContainsKey(device.Peripheral.Address))
|
||||
{
|
||||
discoveredDevices[device.Peripheral.Address].SignalStrength = device.Rssi;
|
||||
discoveredDevices[device.Peripheral.Address].LastActiveTime = DateTime.Now;
|
||||
//Debug.Log($"设备{ device.Peripheral.Name }信号量:{ device.Rssi }");
|
||||
}
|
||||
//移除到5s没有扫描到的设备
|
||||
var now = DateTime.Now;
|
||||
var needRemove = discoveredDevices.Where(c => (now - c.Value.LastActiveTime).TotalSeconds > 5 && c.Value.State == Ant.DeviceState.Disconnected).ToList();
|
||||
foreach (var item in needRemove)
|
||||
{
|
||||
discoveredDevices.Remove(item.Key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace Assets.Scripts.Devices.Ble
|
||||
this.managerStatusChanged -= value;
|
||||
}
|
||||
}
|
||||
WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOn;
|
||||
WclBleManagerStatus statusEnum = WclBleManagerStatus.RadioOff;
|
||||
internal BleMobileThread() {
|
||||
var self = this;
|
||||
//初始蓝牙
|
||||
@@ -69,12 +69,12 @@ namespace Assets.Scripts.Devices.Ble
|
||||
|
||||
public void StartWatcher() {
|
||||
var self = this;
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
if (statusEnum == WclBleManagerStatus.RadioOff)
|
||||
{
|
||||
BluetoothLEHardwareInterface.BluetoothEnable(true);
|
||||
BluetoothLEHardwareInterface.Initialize(true, false, () =>
|
||||
{
|
||||
statusEnum = WclBleManagerStatus.RadioOn;
|
||||
managerInitialized?.Invoke(self);
|
||||
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(ServiceUuids.GetServiceUuidList().ToArray(), null, (address, name, rssi, bytes) =>
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user