App Inventor 2 低功耗蓝牙BLE 两种通信方式:扫描和广播

低功耗蓝牙,也称为蓝牙 LE 或简称 BLE,是一种类似于经典蓝牙的新通信协议,不同之处在于它旨在消耗更少的功耗,同时保持可比的功能。

因此,低功耗蓝牙是与耗电资源有限的物联网设备进行通信的首选。BluetoothLE 扩展需要 Android 5.0 或更高版本。

2种通信方式:扫描和广播

扫描

图片

最常见的通信方式,扫描BLE设备列表,连接指定设备,与设备进行读写通信。绝大多数的低功耗蓝牙通信都是采用这种先连接设备,然后与硬件特定的服务uuid和特征uuid进行数据通信。

广播

图片

现在常规便宜芯片并无蓝牙协议栈,只能通过人工拼包用最简单的蓝牙广播方式通信,可以做到手机与设备进行简单的交互。

  • 无连接的

  • 是单向的,可以向任何人或者接收器发送数据。

  • 使用广播方式通信没有任何安全措施,任何监听设备都能收到,因此它可能不适合于敏感数据,除非在广播数据中采用某种加密方法。

点击“阅读原文”查看详细接入步骤及demo源码:

App Inventor 2 低功耗蓝牙 BlueToothLE 拓展 · App Inventor 2 中文网

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的C# WinForms程序,实现BLE设备的搜索和数据读写功能。需要使用NuGet包管理器安装`Windows.Devices.Bluetooth`和`Windows.Devices.Bluetooth.GenericAttributeProfile`两个库。 ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.Advertisement; using Windows.Devices.Bluetooth.GenericAttributeProfile; namespace BLETest { public partial class Form1 : Form { private BluetoothLEAdvertisementWatcher _watcher; private GattCharacteristic _characteristic; public Form1() { InitializeComponent(); // 初始化BLE搜索器 _watcher = new BluetoothLEAdvertisementWatcher(); _watcher.Received += OnAdvertisementReceived; } private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) { // 获取广告中的设备名称 string deviceName = args.Advertisement.LocalName; // 如果设备名称包含“BLE设备”字样,则连接该设备 if (deviceName.Contains("BLE设备")) { // 停止搜索 _watcher.Stop(); // 连接设备 BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress); // 获取设备的服务和特征 GattDeviceServicesResult result = await device.GetGattServicesAsync(); GattDeviceService service = result.Services.FirstOrDefault(s => s.Uuid == new Guid("0000FFF0-0000-1000-8000-00805F9B34FB")); GattCharacteristicsResult charsResult = await service.GetCharacteristicsAsync(); _characteristic = charsResult.Characteristics.FirstOrDefault(c => c.Uuid == new Guid("0000FFF1-0000-1000-8000-00805F9B34FB")); // 订阅特征的通知 GattCommunicationStatus status = await _characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); if (status == GattCommunicationStatus.Success) { _characteristic.ValueChanged += OnCharacteristicValueChanged; // 显示连接成功的提示信息 MessageBox.Show("连接成功!"); } else { // 显示连接失败的提示信息 MessageBox.Show("连接失败!"); } } } private async void OnCharacteristicValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { // 读取设备发送的数据 byte[] buffer = args.CharacteristicValue.ToArray(); string message = Encoding.ASCII.GetString(buffer); // 将数据显示在文本框中 textBox1.Text = message; } private void btnSearch_Click(object sender, EventArgs e) { // 开始搜索BLE设备 _watcher.Start(); } private async void btnConnect_Click(object sender, EventArgs e) { // 发送数据到设备 byte[] buffer = Encoding.ASCII.GetBytes(textBox2.Text); GattCommunicationStatus status = await _characteristic.WriteValueAsync(buffer.AsBuffer()); if (status == GattCommunicationStatus.Success) { // 显示发送成功的提示信息 MessageBox.Show("发送成功!"); } else { // 显示发送失败的提示信息 MessageBox.Show("发送失败!"); } } } } ``` 在上面的代码中,我们使用`BluetoothLEAdvertisementWatcher`类进行BLE设备的搜索,使用`BluetoothLEDevice`类连接设备,使用`GattDeviceService`类获取设备的服务和特征,使用`GattCharacteristic`类订阅特征的通知,并使用`WriteValueAsync`方法向设备发送数据。当设备发送数据时,我们通过`ValueChanged`事件读取设备的数据,并将数据显示在文本框中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI2中文网

真诚赞赏,手留余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值