Divided the code into functional modules
This commit is contained in:
430
Services/BluetoothService.cs
Normal file
430
Services/BluetoothService.cs
Normal file
@ -0,0 +1,430 @@
|
||||
using System.Diagnostics;
|
||||
using HeartRateMonitorAndroid.Models;
|
||||
using Plugin.BLE;
|
||||
using Plugin.BLE.Abstractions;
|
||||
using Plugin.BLE.Abstractions.Contracts;
|
||||
using Plugin.BLE.Abstractions.EventArgs;
|
||||
|
||||
namespace HeartRateMonitorAndroid.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 蓝牙服务类,用于管理蓝牙设备连接和心率监测
|
||||
/// </summary>
|
||||
public class BluetoothService
|
||||
{
|
||||
private const string TAG = "BluetoothService";
|
||||
|
||||
// 心率服务和特征的UUID常量
|
||||
private static readonly Guid HEART_RATE_SERVICE_UUID = Guid.Parse("0000180D-0000-1000-8000-00805F9B34FB");
|
||||
private static readonly Guid HEART_RATE_MEASUREMENT_CHARACTERISTIC_UUID = Guid.Parse("00002A37-0000-1000-8000-00805F9B34FB");
|
||||
|
||||
private readonly IBluetoothLE _ble;
|
||||
private readonly IAdapter _adapter;
|
||||
private bool _isConnecting = false;
|
||||
private IDevice _connectedDevice = null;
|
||||
|
||||
/// <summary>
|
||||
/// 心率数据更新事件
|
||||
/// </summary>
|
||||
public event Action<int> HeartRateUpdated;
|
||||
|
||||
/// <summary>
|
||||
/// 蓝牙状态更新事件
|
||||
/// </summary>
|
||||
public event Action<string> StatusUpdated;
|
||||
|
||||
/// <summary>
|
||||
/// 设备发现事件
|
||||
/// </summary>
|
||||
public event Action<IDevice> DeviceDiscovered;
|
||||
|
||||
/// <summary>
|
||||
/// 当前连接的设备
|
||||
/// </summary>
|
||||
public IDevice ConnectedDevice => _connectedDevice;
|
||||
|
||||
/// <summary>
|
||||
/// 蓝牙是否可用
|
||||
/// </summary>
|
||||
public bool IsBluetoothAvailable => _ble.IsAvailable && _ble.IsOn;
|
||||
|
||||
/// <summary>
|
||||
/// 是否正在扫描
|
||||
/// </summary>
|
||||
public bool IsScanning => _adapter.IsScanning;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化蓝牙服务
|
||||
/// </summary>
|
||||
public BluetoothService()
|
||||
{
|
||||
_ble = CrossBluetoothLE.Current;
|
||||
_adapter = CrossBluetoothLE.Current.Adapter;
|
||||
|
||||
// 注册设备发现事件
|
||||
_adapter.DeviceDiscovered += OnDeviceDiscovered;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查蓝牙状态
|
||||
/// </summary>
|
||||
/// <returns>状态信息</returns>
|
||||
public string CheckBluetoothState()
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 检查 BLE 状态...");
|
||||
|
||||
if (!_ble.IsAvailable)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 设备不支持 BLE");
|
||||
StatusUpdated?.Invoke("设备不支持 BLE");
|
||||
return "设备不支持 BLE";
|
||||
}
|
||||
|
||||
if (!_ble.IsOn)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 蓝牙未开启");
|
||||
StatusUpdated?.Invoke("请开启蓝牙后再试");
|
||||
return "请开启蓝牙后再试";
|
||||
}
|
||||
|
||||
Debug.WriteLine($"{TAG}: BLE 可用且已开启");
|
||||
StatusUpdated?.Invoke("准备就绪,点击开始扫描");
|
||||
return "准备就绪,点击开始扫描";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始扫描设备
|
||||
/// </summary>
|
||||
public async Task StartScanAsync()
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 开始扫描附近设备...");
|
||||
StatusUpdated?.Invoke("正在扫描...");
|
||||
|
||||
try
|
||||
{
|
||||
// 先停止之前的扫描
|
||||
if (_adapter.IsScanning)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 停止之前的扫描");
|
||||
await _adapter.StopScanningForDevicesAsync();
|
||||
// 短暂延迟确保扫描完全停止
|
||||
await Task.Delay(200);
|
||||
}
|
||||
|
||||
// 设置扫描参数
|
||||
_adapter.ScanMode = ScanMode.LowLatency; // 使用低延迟模式提高响应速度
|
||||
|
||||
// 开始全扫描模式
|
||||
Debug.WriteLine($"{TAG}: 开始全扫描模式");
|
||||
await _adapter.StartScanningForDevicesAsync();
|
||||
|
||||
Debug.WriteLine($"{TAG}: 扫描已启动,将自动超时或在发现心率设备时停止");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 扫描出错: {ex.Message}");
|
||||
StatusUpdated?.Invoke($"扫描出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止扫描设备
|
||||
/// </summary>
|
||||
public async Task StopScanAsync()
|
||||
{
|
||||
if (_adapter.IsScanning)
|
||||
{
|
||||
try
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 停止扫描");
|
||||
await _adapter.StopScanningForDevicesAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 停止扫描时出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接到心率设备
|
||||
/// </summary>
|
||||
/// <param name="device">要连接的设备</param>
|
||||
public async Task ConnectToDeviceAsync(IDevice device)
|
||||
{
|
||||
// 防止重复连接
|
||||
if (_isConnecting)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 已有连接请求正在进行中,忽略此次连接");
|
||||
return;
|
||||
}
|
||||
|
||||
_isConnecting = true;
|
||||
|
||||
try
|
||||
{
|
||||
// 确保扫描已停止
|
||||
if (_adapter.IsScanning)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 连接前确保扫描已停止");
|
||||
await _adapter.StopScanningForDevicesAsync();
|
||||
await Task.Delay(300); // 确保扫描完全停止
|
||||
}
|
||||
|
||||
StatusUpdated?.Invoke($"正在连接到 {device.Name ?? "未知设备"}...");
|
||||
Debug.WriteLine($"{TAG}: 正在连接到设备: {device.Name ?? "未知设备"}...");
|
||||
|
||||
// 使用CancellationToken添加超时控制
|
||||
var cancelSource = new CancellationTokenSource();
|
||||
cancelSource.CancelAfter(TimeSpan.FromSeconds(15)); // 15秒连接超时
|
||||
|
||||
// 连接到设备
|
||||
try
|
||||
{
|
||||
await _adapter.ConnectToDeviceAsync(device,
|
||||
new ConnectParameters(autoConnect: false, forceBleTransport: true), cancelSource.Token);
|
||||
Debug.WriteLine($"{TAG}: 连接命令已发送,等待连接完成");
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 连接操作超时");
|
||||
StatusUpdated?.Invoke("连接超时,请重试");
|
||||
return;
|
||||
}
|
||||
|
||||
if (device.State == DeviceState.Connected)
|
||||
{
|
||||
StatusUpdated?.Invoke($"已连接到 {device.Name ?? "未知设备"}");
|
||||
Debug.WriteLine($"{TAG}: 已连接到设备: {device.Name ?? "未知设备"}");
|
||||
|
||||
// 保存连接的设备引用
|
||||
_connectedDevice = device;
|
||||
|
||||
// 获取心率服务
|
||||
Debug.WriteLine($"{TAG}: 尝试获取心率服务 {HEART_RATE_SERVICE_UUID}");
|
||||
var heartRateService = await device.GetServiceAsync(HEART_RATE_SERVICE_UUID);
|
||||
if (heartRateService == null)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 未找到心率服务");
|
||||
StatusUpdated?.Invoke("未找到心率服务");
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.WriteLine($"{TAG}: 已获取心率服务,尝试获取心率特征");
|
||||
// 获取心率特征
|
||||
var heartRateCharacteristic = await heartRateService.GetCharacteristicAsync(HEART_RATE_MEASUREMENT_CHARACTERISTIC_UUID);
|
||||
if (heartRateCharacteristic == null)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 未找到心率特征");
|
||||
StatusUpdated?.Invoke("未找到心率特征");
|
||||
return;
|
||||
}
|
||||
|
||||
// 订阅心率通知
|
||||
heartRateCharacteristic.ValueUpdated += (s, e) =>
|
||||
{
|
||||
// 解析心率数据
|
||||
var data = e.Characteristic.Value;
|
||||
if (data == null || data.Length == 0)
|
||||
return;
|
||||
|
||||
byte flags = data[0];
|
||||
bool isHeartRateValueFormat16Bit = ((flags & 0x01) != 0);
|
||||
int heartRate;
|
||||
|
||||
if (isHeartRateValueFormat16Bit && data.Length >= 3)
|
||||
{
|
||||
heartRate = BitConverter.ToUInt16(data, 1);
|
||||
}
|
||||
else if (data.Length >= 2)
|
||||
{
|
||||
heartRate = data[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
return; // 数据不完整
|
||||
}
|
||||
|
||||
// 触发心率更新事件
|
||||
HeartRateUpdated?.Invoke(heartRate);
|
||||
};
|
||||
|
||||
// 开始接收通知
|
||||
await heartRateCharacteristic.StartUpdatesAsync();
|
||||
StatusUpdated?.Invoke("正在监测心率...");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 连接失败");
|
||||
StatusUpdated?.Invoke("连接失败,请重试");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 连接错误: {ex.Message}");
|
||||
StatusUpdated?.Invoke($"连接错误: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 重置连接状态标志
|
||||
_isConnecting = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开设备连接
|
||||
/// </summary>
|
||||
public async Task DisconnectAsync()
|
||||
{
|
||||
if (_connectedDevice != null && _connectedDevice.State == DeviceState.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _adapter.DisconnectDeviceAsync(_connectedDevice);
|
||||
_connectedDevice = null;
|
||||
StatusUpdated?.Invoke("设备已断开连接");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 断开连接时出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备发现事件处理
|
||||
/// </summary>
|
||||
private void OnDeviceDiscovered(object sender, DeviceEventArgs args)
|
||||
{
|
||||
var device = args.Device;
|
||||
Debug.WriteLine($"{TAG}: 发现设备: {device.Name ?? "未知设备"} ({device.Id})");
|
||||
|
||||
foreach (var adv in device.AdvertisementRecords)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: Adv Type: {adv.Type}, Data: {BitConverter.ToString(adv.Data)}");
|
||||
}
|
||||
|
||||
// 检查广播数据是否包含心率服务 UUID (0x180D)
|
||||
bool hasHeartRateService = false;
|
||||
|
||||
// 检查16位UUID服务列表
|
||||
var serviceUuids16Bit = device.AdvertisementRecords.FirstOrDefault(r =>
|
||||
r.Type == AdvertisementRecordType.UuidsComplete16Bit ||
|
||||
r.Type == AdvertisementRecordType.UuidsIncomple16Bit);
|
||||
|
||||
if (serviceUuids16Bit != null)
|
||||
{
|
||||
// 心率服务UUID是0x180D,根据日志,数据存储顺序为18-0D
|
||||
string dataString = BitConverter.ToString(serviceUuids16Bit.Data);
|
||||
Debug.WriteLine($"{TAG}: 16位UUID数据: {dataString}");
|
||||
hasHeartRateService = dataString.Contains("18-0D");
|
||||
|
||||
if (hasHeartRateService)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 在16位UUID中找到心率服务(0x180D)");
|
||||
}
|
||||
}
|
||||
|
||||
// 如果16位列表中未找到,则检查128位UUID列表
|
||||
if (!hasHeartRateService)
|
||||
{
|
||||
var serviceUuids128Bit = device.AdvertisementRecords.FirstOrDefault(r =>
|
||||
r.Type == AdvertisementRecordType.UuidsComplete128Bit ||
|
||||
r.Type == AdvertisementRecordType.UuidsIncomplete128Bit);
|
||||
|
||||
if (serviceUuids128Bit != null)
|
||||
{
|
||||
// 心率服务在128位UUID中的格式通常是0000180D-0000-1000-8000-00805F9B34FB
|
||||
// 检查两种可能的排列方式
|
||||
string dataString = BitConverter.ToString(serviceUuids128Bit.Data);
|
||||
Debug.WriteLine($"{TAG}: 128位UUID数据: {dataString}");
|
||||
hasHeartRateService = dataString.Contains("18-0D") || dataString.Contains("0D-18");
|
||||
|
||||
if (hasHeartRateService)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 在128位UUID中找到心率服务(0x180D)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查设备名称,有些心率设备名称中包含相关信息
|
||||
if (!hasHeartRateService && !string.IsNullOrEmpty(device.Name))
|
||||
{
|
||||
string name = device.Name.ToLower();
|
||||
if (name.Contains("heart") || name.Contains("hr") || name.Contains("pulse") ||
|
||||
name.Contains("cardiac") || name.Contains("心率"))
|
||||
{
|
||||
hasHeartRateService = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (hasHeartRateService)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 检测到心率设备: {device.Name ?? "未知设备"}");
|
||||
|
||||
// 立即停止扫描 - 这是重要的,必须先停止扫描再连接
|
||||
if (_adapter.IsScanning)
|
||||
{
|
||||
try
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 停止扫描以准备连接设备");
|
||||
_adapter.StopScanningForDevicesAsync().ContinueWith(t =>
|
||||
{
|
||||
if (t.IsCompleted && !t.IsFaulted)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 扫描已停止,准备连接设备");
|
||||
DeviceDiscovered?.Invoke(device);
|
||||
}
|
||||
else if (t.IsFaulted && t.Exception != null)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 停止扫描失败: {t.Exception.Message}");
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 停止扫描时出错: {ex.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有扫描,直接通知发现了设备
|
||||
DeviceDiscovered?.Invoke(device);
|
||||
}
|
||||
|
||||
// 尝试从广播数据中解析心率值(有些设备可能在广播中包含数据)
|
||||
var manufacturerData = device.AdvertisementRecords.FirstOrDefault(r =>
|
||||
r.Type == AdvertisementRecordType.ManufacturerSpecificData);
|
||||
|
||||
if (manufacturerData != null && manufacturerData.Data.Length > 1)
|
||||
{
|
||||
int heartRate = manufacturerData.Data[1];
|
||||
Debug.WriteLine($"{TAG}: 广播心率值: {heartRate} bpm");
|
||||
HeartRateUpdated?.Invoke(heartRate);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine($"{TAG}: 未在广播中找到心率值,将尝试连接设备读取");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_adapter != null && _adapter.IsScanning)
|
||||
{
|
||||
_adapter.StopScanningForDevicesAsync().Wait();
|
||||
}
|
||||
|
||||
if (_connectedDevice != null && _connectedDevice.State == DeviceState.Connected)
|
||||
{
|
||||
_adapter.DisconnectDeviceAsync(_connectedDevice).Wait();
|
||||
}
|
||||
|
||||
_adapter.DeviceDiscovered -= OnDeviceDiscovered;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Services/INotificationService.cs
Normal file
36
Services/INotificationService.cs
Normal file
@ -0,0 +1,36 @@
|
||||
namespace HeartRateMonitorAndroid.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 通知服务接口
|
||||
/// </summary>
|
||||
public interface INotificationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化通知服务
|
||||
/// </summary>
|
||||
void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
/// <param name="currentHeartRate">当前心率</param>
|
||||
/// <param name="avgHeartRate">平均心率</param>
|
||||
/// <param name="minHeartRate">最低心率</param>
|
||||
/// <param name="maxHeartRate">最高心率</param>
|
||||
/// <param name="duration">监测时长</param>
|
||||
void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration);
|
||||
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
void CancelNotification();
|
||||
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
/// <param name="title">标题</param>
|
||||
/// <param name="message">消息内容</param>
|
||||
/// <param name="attemptCount">尝试次数</param>
|
||||
void ShowReconnectionNotification(string title, string message, int attemptCount);
|
||||
}
|
||||
}
|
||||
@ -1,119 +1,83 @@
|
||||
namespace HeartRateMonitorAndroid.Services
|
||||
using HeartRateMonitorAndroid.Services.Platform;
|
||||
|
||||
namespace HeartRateMonitorAndroid.Services
|
||||
{
|
||||
// 跨平台通知服务
|
||||
/// <summary>
|
||||
/// 通知服务工厂
|
||||
/// </summary>
|
||||
public static class NotificationService
|
||||
{
|
||||
// 常量定义
|
||||
private const string CHANNEL_ID = "HeartRateMonitorChannel";
|
||||
private const int NOTIFICATION_ID = 100;
|
||||
private static readonly INotificationService _instance;
|
||||
|
||||
// 初始化通知服务
|
||||
public static void Initialize()
|
||||
/// <summary>
|
||||
/// 静态构造函数,根据平台创建对应的通知服务实现
|
||||
/// </summary>
|
||||
static NotificationService()
|
||||
{
|
||||
// 根据平台初始化
|
||||
if (DeviceInfo.Platform == DevicePlatform.Android)
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.CreateNotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"心率监测",
|
||||
"显示实时心率数据");
|
||||
#endif
|
||||
}
|
||||
_instance = new AndroidNotificationService();
|
||||
else if (DeviceInfo.Platform == DevicePlatform.iOS)
|
||||
{
|
||||
#if IOS
|
||||
// 请求iOS通知权限
|
||||
Platforms.iOS.IosNotificationHelper.RequestNotificationPermission().ConfigureAwait(false);
|
||||
#endif
|
||||
}
|
||||
_instance = new IosNotificationService();
|
||||
else if (DeviceInfo.Platform == DevicePlatform.WinUI)
|
||||
_instance = new WindowsNotificationService();
|
||||
else _instance = new NullNotificationService();
|
||||
// 初始化通知服务
|
||||
_instance.Initialize();
|
||||
}
|
||||
|
||||
// 显示心率通知
|
||||
/// <summary>
|
||||
/// 获取当前平台的通知服务实例
|
||||
/// </summary>
|
||||
public static INotificationService Current => _instance;
|
||||
|
||||
#region 便捷方法
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
public static void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
string title = "心率监测";
|
||||
string content = $"当前心率: {currentHeartRate} bpm 平均: {avgHeartRate:0} bpm";
|
||||
string bigText = $"当前心率: {currentHeartRate} bpm\n监测时长: {duration.Hours:00}:{duration.Minutes:00}:{duration.Seconds:00}\n最低: {minHeartRate} bpm | 最高: {maxHeartRate} bpm";
|
||||
|
||||
if (DeviceInfo.Platform == DevicePlatform.Android)
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.ShowBigTextNotification(
|
||||
CHANNEL_ID,
|
||||
NOTIFICATION_ID,
|
||||
title,
|
||||
content,
|
||||
bigText,
|
||||
Resource.Drawable.notification_icon_background,
|
||||
true);
|
||||
#endif
|
||||
}
|
||||
else if (DeviceInfo.Platform == DevicePlatform.iOS)
|
||||
{
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.ShowNotification(title, content);
|
||||
#endif
|
||||
}
|
||||
else if (DeviceInfo.Platform == DevicePlatform.WinUI)
|
||||
{
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.ShowNotification(title, content);
|
||||
#endif
|
||||
}
|
||||
_instance.ShowHeartRateNotification(currentHeartRate, avgHeartRate, minHeartRate, maxHeartRate, duration);
|
||||
}
|
||||
|
||||
// 取消通知
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
public static void CancelNotification()
|
||||
{
|
||||
if (DeviceInfo.Platform == DevicePlatform.Android)
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.CancelNotification(NOTIFICATION_ID);
|
||||
#endif
|
||||
}
|
||||
else if (DeviceInfo.Platform == DevicePlatform.iOS)
|
||||
{
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.CancelAllNotifications();
|
||||
#endif
|
||||
}
|
||||
else if (DeviceInfo.Platform == DevicePlatform.WinUI)
|
||||
{
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.CancelNotification();
|
||||
#endif
|
||||
}
|
||||
_instance.CancelNotification();
|
||||
}
|
||||
|
||||
// 显示重连通知
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
public static void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
const int RECONNECTION_NOTIFICATION_ID = 101; // 使用不同的ID,避免覆盖心率通知
|
||||
_instance.ShowReconnectionNotification(title, message, attemptCount);
|
||||
}
|
||||
|
||||
if (DeviceInfo.Platform == DevicePlatform.Android)
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 空实现,用于不支持的平台
|
||||
/// </summary>
|
||||
private class NullNotificationService : INotificationService
|
||||
{
|
||||
public void Initialize() { }
|
||||
|
||||
public void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.ShowNormalNotification(
|
||||
CHANNEL_ID,
|
||||
RECONNECTION_NOTIFICATION_ID,
|
||||
title,
|
||||
message,
|
||||
Resource.Drawable.notification_icon_background,
|
||||
false); // 不使用前台服务,只显示普通通知
|
||||
#endif
|
||||
// 空实现
|
||||
}
|
||||
else if (DeviceInfo.Platform == DevicePlatform.iOS)
|
||||
|
||||
public void CancelNotification()
|
||||
{
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.ShowNotification(title, message);
|
||||
#endif
|
||||
// 空实现
|
||||
}
|
||||
else if (DeviceInfo.Platform == DevicePlatform.WinUI)
|
||||
|
||||
public void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.ShowNotification(title, message);
|
||||
#endif
|
||||
// 空实现
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
72
Services/Platform/AndroidNotificationService.cs
Normal file
72
Services/Platform/AndroidNotificationService.cs
Normal file
@ -0,0 +1,72 @@
|
||||
namespace HeartRateMonitorAndroid.Services.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// Android平台通知服务实现
|
||||
/// </summary>
|
||||
public class AndroidNotificationService : INotificationService
|
||||
{
|
||||
private const string CHANNEL_ID = "HeartRateMonitorChannel";
|
||||
private const int NOTIFICATION_ID = 100;
|
||||
private const int RECONNECTION_NOTIFICATION_ID = 101;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化通知服务
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.CreateNotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"心率监测",
|
||||
"显示实时心率数据");
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
public void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
string title = "心率监测";
|
||||
string content = $"当前心率: {currentHeartRate} bpm 平均: {avgHeartRate:0} bpm";
|
||||
string bigText = $"当前心率: {currentHeartRate} bpm\n监测时长: {duration.Hours:00}:{duration.Minutes:00}:{duration.Seconds:00}\n最低: {minHeartRate} bpm | 最高: {maxHeartRate} bpm";
|
||||
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.ShowBigTextNotification(
|
||||
CHANNEL_ID,
|
||||
NOTIFICATION_ID,
|
||||
title,
|
||||
content,
|
||||
bigText,
|
||||
Resource.Drawable.notification_icon_background,
|
||||
true);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
public void CancelNotification()
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.CancelNotification(NOTIFICATION_ID);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
public void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
#if ANDROID
|
||||
Platforms.Android.AndroidNotificationHelper.ShowNormalNotification(
|
||||
CHANNEL_ID,
|
||||
RECONNECTION_NOTIFICATION_ID,
|
||||
title,
|
||||
message,
|
||||
Resource.Drawable.notification_icon_background,
|
||||
false); // 不使用前台服务,只显示普通通知
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Services/Platform/IosNotificationService.cs
Normal file
52
Services/Platform/IosNotificationService.cs
Normal file
@ -0,0 +1,52 @@
|
||||
namespace HeartRateMonitorAndroid.Services.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// iOS平台通知服务实现
|
||||
/// </summary>
|
||||
public class IosNotificationService : INotificationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化通知服务
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
#if IOS
|
||||
// 请求iOS通知权限
|
||||
Platforms.iOS.IosNotificationHelper.RequestNotificationPermission().ConfigureAwait(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
public void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
string title = "心率监测";
|
||||
string content = $"当前心率: {currentHeartRate} bpm 平均: {avgHeartRate:0} bpm";
|
||||
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.ShowNotification(title, content);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
public void CancelNotification()
|
||||
{
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.CancelAllNotifications();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
public void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
#if IOS
|
||||
Platforms.iOS.IosNotificationHelper.ShowNotification(title, message);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
49
Services/Platform/WindowsNotificationService.cs
Normal file
49
Services/Platform/WindowsNotificationService.cs
Normal file
@ -0,0 +1,49 @@
|
||||
namespace HeartRateMonitorAndroid.Services.Platform
|
||||
{
|
||||
/// <summary>
|
||||
/// Windows平台通知服务实现
|
||||
/// </summary>
|
||||
public class WindowsNotificationService : INotificationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化通知服务
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
// Windows平台不需要特殊初始化
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示心率通知
|
||||
/// </summary>
|
||||
public void ShowHeartRateNotification(int currentHeartRate, double avgHeartRate, int minHeartRate, int maxHeartRate, TimeSpan duration)
|
||||
{
|
||||
string title = "心率监测";
|
||||
string content = $"当前心率: {currentHeartRate} bpm 平均: {avgHeartRate:0} bpm";
|
||||
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.ShowNotification(title, content);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消通知
|
||||
/// </summary>
|
||||
public void CancelNotification()
|
||||
{
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.CancelNotification();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示重连通知
|
||||
/// </summary>
|
||||
public void ShowReconnectionNotification(string title, string message, int attemptCount)
|
||||
{
|
||||
#if WINDOWS
|
||||
Platforms.Windows.WindowsNotificationHelper.ShowNotification(title, message);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user