移动端蓝牙优化
This commit is contained in:
1 parent
72f254af2a
commit
2f64aa494f
10 files changed
+84
-16
No files matched your search
@@ -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