当前位置: 首页 > news >正文

收费底的网站有吗无锡百度推广公司哪家好

收费底的网站有吗,无锡百度推广公司哪家好,网站建设及优化 赣icp,嘉善建设工程质量监督站网站在开发MAUI应用程序时,蓝牙协议的应用是一个重要的环节,尤其是在需要与外部设备如智能跳绳进行数据交换的场景中。以下是我在开发过程中的一些经验和心得,希望能为你的项目提供帮助。 1. 蓝牙协议基础 蓝牙协议是无线通信的一种标准&#x…

在开发MAUI应用程序时,蓝牙协议的应用是一个重要的环节,尤其是在需要与外部设备如智能跳绳进行数据交换的场景中。以下是我在开发过程中的一些经验和心得,希望能为你的项目提供帮助。

1. 蓝牙协议基础

蓝牙协议是无线通信的一种标准,允许设备之间进行短距离的数据交换。在MAUI开发中,我们主要关注的是BLE(Bluetooth Low Energy,低功耗蓝牙)协议,它以其低功耗和低成本的特点被广泛应用于智能设备中。

2. 使用Ble蓝牙助手

在开发过程中,我使用了“BLE蓝牙助手”这款应用来辅助调试和理解蓝牙协议。这款应用可以帮助我们扫描周围的BLE设备,查看设备的信号强度(RSSI),连接设备,并查看服务和特征。通过这个工具,我们可以更直观地理解BLE协议的工作原理和数据交换过程。

3. MAUI中的蓝牙开发

        MAUI(.NET Multi-platform App UI)是一个跨平台框架,它允许开发者使用C#和XAML创建跨平台的移动和桌面应用。MAUI以其性能优异、可扩展性强和结构简单而受到开发者的青睐。在本次开发中,MAUI作为主要的开发框架,提供了丰富的控件和API,使得与蓝牙设备的对接成为可能。

        MAUI支持多种平台,包括Android、iOS、macOS和Windows,这为开发跨平台应用提供了极大的便利。在本项目中,我们将重点利用MAUI的跨平台特性,开发一款能够在不同操作系统上运行的跳绳计数应用。

        在MAUI中,我们可以通过Plugin.BLE来实现蓝牙功能。以下是一些关键步骤:

3.1 扫描设备

首先,我们需要扫描周围的BLE设备。在MAUI中,我们可以使用以下代码来启动扫描:

await CurrentAdapter.StartScanningForDevicesAsync();public  async Task<bool> StartScanAsync(){//检查获取蓝牙权限bool isPermissionPass = await CheckAndRequestBluetoothPermission();if (!isPermissionPass)return false;// 在使用之前,确保 _scanForAedCts 已经被实例化ListDevice.Clear();try{if(CurrentAdapter == null){CurrentAdapter = CrossBluetoothLE.Current.Adapter;}await CurrentAdapter.StopScanningForDevicesAsync();CurrentAdapter.DeviceDiscovered += Adapter_DeviceDiscovered;CurrentAdapter.ScanTimeoutElapsed += Adapter_ScanTimeoutElapsed;//蓝牙扫描时间CurrentAdapter.ScanTimeout = 30 * 1000;//默认LowPowerCurrentAdapter.ScanMode = Plugin.BLE.Abstractions.Contracts.ScanMode.LowPower;Debug.WriteLine($"开始扫描外设, IsAvailable={CrossBluetoothLE.Current.IsAvailable}, IsOn={CrossBluetoothLE.Current.IsOn}, State={CrossBluetoothLE.Current.State}, ScanMode={CurrentAdapter.ScanMode}, ScanTimeout={CurrentAdapter.ScanTimeout}");await CurrentAdapter.StartScanningForDevicesAsync(cancellationToken: _scanForAedCts.Token);Debug.WriteLine($"结束扫描外设");}catch (OperationCanceledException){Debug.WriteLine($"扫描外设任务取消");}catch (Exception ex){Debug.WriteLine($"扫描外设出错, {ex.Message}");}finally{CurrentAdapter.DeviceDiscovered -= Adapter_DeviceDiscovered;CurrentAdapter.ScanTimeoutElapsed -= Adapter_ScanTimeoutElapsed;//_scanForAedCts.Dispose();}return true;}

在扫描过程中,我们可以通过DeviceDiscovered事件来获取发现的设备信息。

3.2 连接设备

一旦找到目标设备,我们就可以建立连接。在MAUI中,连接设备的过程如下:

await CurrentAdapter.ConnectToDeviceAsync(device, new ConnectParameters(false, true));/// <summary>/// 连接设备/// </summary>/// <param name="uuid"></param>/// <returns></returns>public async Task<IDevice?> ConnectDeviceAsync(Guid uuid){try{if (CurrentAdapter == null){CurrentAdapter = CrossBluetoothLE.Current.Adapter;}var connectedDevices = CurrentAdapter.ConnectedDevices;if (connectedDevices.Count > 0){// 至少有一个设备已经连接foreach (var device in connectedDevices){// 可以在这里处理每个已连接的设备if (device.Id == uuid){await StartNotify(device);return device;}Console.WriteLine(device.Name);}}else{try{using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15))){var device = await CurrentAdapter.ConnectToKnownDeviceAsync(uuid, default(ConnectParameters), cts.Token);await StartNotify(device);return device;}}catch (Exception ex) { Debug.WriteLine($"蓝牙连接设备失败, {ex.Message}"); }}}catch(Exception ex){Debug.WriteLine($"蓝牙连接设备失败, {ex.Message}");}return null;}

这里device是我们通过扫描得到的设备对象,ConnectParameters用于设置连接参数。

3.3 获取服务和特征

连接成功后,我们可以获取设备提供的服务和特征,这对于数据交换至关重要:

var services = await device.GetGattServicesAsync();

3.4 数据读写

通过获取的特征,我们可以进行数据的读写操作。例如,读取跳绳的次数和时间:

var characteristic = services.First().GetCharacteristics().First();
var readResult = await characteristic.ReadValueAsync();/// <summary>/// 读取数据/// </summary>/// <param name="characteristic"></param>/// <returns></returns>public async Task<byte[]?> ReadDataAsync(ICharacteristic characteristic){//根据Plugin.BLE要求,在主线程读写数据var result = await MainThread.InvokeOnMainThreadAsync(async () =>{try{//读取数据var ary = await characteristic.ReadAsync();Debug.WriteLine($"读取成功,长度={ary.data.Length}");return (ary.data);}catch (Exception ex){Debug.WriteLine($"读取错误, 目标设备蓝牙连接状态={BleDevice?.State}, {ex.Message}");return null;}});return result;}/// <summary>/// 读取蓝牙设备数据/// </summary>/// <param name="guid"></param>/// <returns></returns>public async Task<byte[]?> ReadDataAsync(IDevice device){var service = await device.GetServiceAsync(new Guid("0000180D-0000-1000-8000-00805F9B34FB"));if (service != null){var characteristic = await service.GetCharacteristicAsync(new Guid("00002A37-0000-1000-8000-00805F9B34FB"));if (characteristic != null){var ary = await characteristic.ReadAsync();return (ary.data);}}return null;}/// <summary>/// 写入蓝牙设备数据/// </summary>/// <param name="device"></param>/// <param name="ary"></param>/// <returns></returns>public async Task<int> SendDataAsync(IDevice device, byte[] ary){var service = await device.GetServiceAsync(_serviceUuid);if (service != null){var characteristic = await service.GetCharacteristicAsync(_characteristicUuid);if (characteristic != null && characteristic.CanWrite==true){return await characteristic.WriteAsync(ary);}}return 0;}

3.5事件订阅

通过获取的特征,我们可以进行数据的通知操作。

#region 订阅事件private async Task<int> StartNotify(IDevice device){try{var service = await device.GetServiceAsync(_serviceUuid);if (service != null){var notifyCharacteristic = await service.GetCharacteristicAsync(_characteristicUuid);if (notifyCharacteristic.Properties.HasFlag(CharacteristicPropertyType.Notify)){// 特性支持通知// 订阅事件notifyCharacteristic.ValueUpdated += NotifyCharacteristic_ValueUpdated;// 启用通知await notifyCharacteristic.StartUpdatesAsync();}}}catch(Exception ex){}return 0;}// 处理特性值更新事件private void NotifyCharacteristic_ValueUpdated(object sender, Plugin.BLE.Abstractions.EventArgs.CharacteristicUpdatedEventArgs e){// 处理特性值更新NotifyQueue.Enqueue( e.Characteristic.Value);// ...}public async Task<byte[]?> ReadNotify(IDevice device){if (device.IsConnectable == true){if (NotifyQueue.Count > 0){return NotifyQueue.Dequeue();}}return null;}private async Task<int> StopNotify(IDevice device){var service = await device.GetServiceAsync(_serviceUuid);if (service != null){// 获取服务和特性var notifyCharacteristic = await service.GetCharacteristicAsync(_characteristicUuid);if (notifyCharacteristic.Properties.HasFlag(CharacteristicPropertyType.Notify)){// 特性支持通知// 订阅事件notifyCharacteristic.ValueUpdated -= NotifyCharacteristic_ValueUpdated;// 启用通知await notifyCharacteristic.StopUpdatesAsync();}}return 0;}#endregion

 

4. 跳绳设备对接实践

        在实际对接跳绳设备时,我们需要根据设备的技术文档来确定服务和特征的UUID。一旦确定,就可以按照上述步骤进行连接和数据交换。例如,读取跳绳次数的特征可能有一个特定的UUID,我们可以通过这个UUID来读取或写入数据。

5. 注意事项

  • 确保在开发过程中,手机的蓝牙功能处于开启状态。
  • 在配对设备时,确保手机与跳绳设备的距离足够近,以保证信号的稳定性。
  • 在读取和写入数据时,要注意数据格式和编码方式,确保数据的正确解析。

通过上述步骤和注意事项,可以在MAUI中顺利实现与BLE设备的对接,记录跳绳的次数与时间。希望这些经验能够帮助你在开发过程中少走弯路,快速实现功能

http://www.hkea.cn/news/40725/

相关文章:

  • 北京做百度网站建设电商平台如何推广运营
  • 电脑个人网站怎么做网络销售新手入门
  • 海口网站建设 小黄网络手机百度搜索
  • 太原百度网站建设网站应该如何进行优化
  • 烟台市做网站uc浏览网页版进入
  • 工程信息网站哪家做的较好提高工作效率心得体会
  • 建站平台入口徐州网站设计
  • 出口手工艺品网站建设方案站长统计app下载
  • 提升学历骗局武汉搜索引擎排名优化
  • wordpress+park主题上海全国关键词排名优化
  • 潍坊最早做网站的公司短链接生成网址
  • 东莞化工网站建设爱站网ip反域名查询
  • 做网站赚钱 2017哈尔滨关键词排名工具
  • 建设的网站首页微信怎么做推广
  • 建设网站导航百度信息流推广和搜索推广
  • 深圳室内设计公司招聘信息流广告优化
  • 旅游网站首页四种营销模式
  • 负责网站建设如何在百度发广告推广
  • 联通的网站是谁做的营销的主要目的有哪些
  • 衡阳微信网站地推的方法和技巧
  • 南阳做网站公司哪家好自动发外链工具
  • 潍坊网站制作最低价格网络营销案例有哪些
  • 做网站有谁做谷歌seo视频教程
  • 资深的网站推广完美日记网络营销策划书
  • 90设计网站免费素材网站seo培训
  • 整形美容网站源码上海seo优化bwyseo
  • 武威市住房和建设局网站百度app下载安装普通下载
  • 网站物理结构天津百度推广排名
  • 美容平台网站建设百度指数查询移动版
  • 工程公司手机网站建立网站怎么搞