添加连接失败后重新连接及Z轴校准功能

This commit is contained in:
MoYue 2023-03-30 09:10:06 +08:00
parent 2f4b6c0ed1
commit 2886676945
5 changed files with 90 additions and 37 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 280 KiB

View File

@ -222,6 +222,7 @@ namespace MonitoringTechnology.Ble
GattCharacteristics.Clear(); GattCharacteristics.Clear();
GattDeviceServicesResult servicesResult = await _device.GetGattServicesAsync().GetResultAsync(); GattDeviceServicesResult servicesResult = await _device.GetGattServicesAsync().GetResultAsync();
if (servicesResult.Status == GattCommunicationStatus.Success) if (servicesResult.Status == GattCommunicationStatus.Success)
{ {
foreach (GattDeviceService service in servicesResult.Services) foreach (GattDeviceService service in servicesResult.Services)
@ -267,9 +268,15 @@ namespace MonitoringTechnology.Ble
{ {
Log.Info("订阅通知成功!" + CharacteristicNotify.Uuid); Log.Info("订阅通知成功!" + CharacteristicNotify.Uuid);
} }
} }
ConnectionStateChanged?.Invoke(this, true); ConnectionStateChanged?.Invoke(this, true);
} }
else
{
string _message = "连接失败,请重新尝试连接!";
ReceiveValueChangedChanged(null, _message);
}
} }
#endregion #endregion

View File

@ -125,6 +125,9 @@ namespace MonitoringTechnology.Ble
}*/ }*/
} }
/// <summary>
/// 开始扫描
/// </summary>
public async void StartScan() public async void StartScan()
{ {
if (advertisementWatcher.Status != BluetoothLEAdvertisementWatcherStatus.Started) if (advertisementWatcher.Status != BluetoothLEAdvertisementWatcherStatus.Started)
@ -134,11 +137,15 @@ namespace MonitoringTechnology.Ble
advertisementWatcher.Start(); advertisementWatcher.Start();
deviceWatcher.Start(); deviceWatcher.Start();
Started?.Invoke(this, null); Started?.Invoke(this, null);
await Task.Delay(5000); await Task.Delay(1000);
StopScan(); StartScan();
} }
} }
/// <summary>
/// 停止扫描
/// </summary>
public void StopScan() public void StopScan()
{ {
//判断状态,防止 InvalidOperationException //判断状态,防止 InvalidOperationException
@ -149,7 +156,5 @@ namespace MonitoringTechnology.Ble
Log.Info("停止扫描"); Log.Info("停止扫描");
} }
} }
//
} }
} }

View File

@ -95,6 +95,7 @@
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
@ -113,6 +114,8 @@
Click="buttonRead_Click" Template="{StaticResource ButtonTemplate}"/> Click="buttonRead_Click" Template="{StaticResource ButtonTemplate}"/>
<Button Grid.Column="8" x:Name="TimeCheck" Content="系统校时" Width="90" Height="30" Margin="10,-2,10,6" <Button Grid.Column="8" x:Name="TimeCheck" Content="系统校时" Width="90" Height="30" Margin="10,-2,10,6"
Click="TimeCheck_Click" Template="{StaticResource ButtonTemplate}"/> Click="TimeCheck_Click" Template="{StaticResource ButtonTemplate}"/>
<Button Grid.Column="9" x:Name="ZCheck" Content="Z轴校准" Width="90" Height="30" Margin="10,-2,10,6"
Click="ZCheck_Click" Template="{StaticResource ButtonTemplate}"/>
</Grid> </Grid>
<Label Grid.Row="1" Content="日志记录" Foreground="White"/> <Label Grid.Row="1" Content="日志记录" Foreground="White"/>
<!-- Log区禁止 ListBox 横向滚动 TextBlock 才能实现自动换行 --> <!-- Log区禁止 ListBox 横向滚动 TextBlock 才能实现自动换行 -->

View File

@ -98,7 +98,7 @@ namespace MonitoringTechnology.Views
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void buttonConnect_Click(object sender, RoutedEventArgs e) private async void buttonConnect_Click(object sender, RoutedEventArgs e)
{ {
if (isSelect) if (isSelect)
{ {
@ -115,8 +115,13 @@ namespace MonitoringTechnology.Views
} }
} }
else else
{
while (!isConnection)
{ {
systemOperationViewModel.BleDevice.Connect(); systemOperationViewModel.BleDevice.Connect();
AddLog("正在尝试重新连接设备,请稍等!");
await Task.Delay(10000);
}
} }
} }
else else
@ -555,6 +560,37 @@ namespace MonitoringTechnology.Views
AddLog("请搜索附近蓝牙设备并连接后重试!!!"); AddLog("请搜索附近蓝牙设备并连接后重试!!!");
} }
} }
/// <summary>
/// Z轴校准
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void ZCheck_Click(object sender, RoutedEventArgs e)
{
if (isConnection)
{
byte[] _send = new byte[] { 0xFE, 0xEF, 0x00, 0x0E, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16 };
int a = 0;
for (int i = 0; i < _send.Length - 2; i++)
{
a += _send[i];
}
a = a & 0xFF;
_send[16] = (byte)a;
//调用发送的那个方法
bool success = await systemOperationViewModel.BleDevice.Write(_send);
if (success)
{
AddLog("已进行Z轴校准!");
PreservationData(_send.ToList(), "Z轴校准指令");
}
}
else
{
AddLog("请搜索附近蓝牙设备并连接后重试!!!");
}
}
#endregion #endregion
@ -583,6 +619,7 @@ namespace MonitoringTechnology.Views
else else
{ {
device.Connect(); device.Connect();
AddLog("正在连接设备,请稍等!");
} }
} }
/// <summary> /// <summary>
@ -762,5 +799,6 @@ namespace MonitoringTechnology.Views
#endregion #endregion
} }
} }