android ble蓝牙接收不到数据_Android BLE蓝牙开发-读写数据 获取UUID

最近在做一个蓝牙智能锁的项目,需要在Android APP 上使用 Ble低功耗蓝牙 和单片机蓝牙设备进行通信,网上关于搜索  连接 读写数据的资料太多了,我在最后放了一个Demo,完整实现搜索  连接 读写数据,在文章结尾重点说一下UUID

首先:

1.BLE介绍

BLE是Bluetooth Low Energy的缩写,又叫蓝牙4.0,区别于蓝牙3.0和之前的技术。BLE前身是NOKIA开发的Wibree技术,主要用于实现移动智能终端与周边配件之间的持续连接,是功耗极低的短距离无线通信技术,并且有效传输距离被提升到了100米以上,同时只需要一颗纽扣电池就可以工作数年之久。BLE是在蓝牙技术的基础上发展起来的,既同于蓝牙,又区别于传统蓝牙。BLE设备分单模和双模两种,双模简称BR,商标为Bluetooth Smart Ready,单模简称BLE或者LE,商标为Bluetooth Smart。Android是在4.3后才支持BLE,这说明不是所有蓝牙手机都支持BLE,而且支持BLE的蓝牙手机一般是双模的。双模兼容传统蓝牙,可以和传统蓝牙通信,也可以和BLE通信,常用在手机上,android4.3和IOS4.0之后版本都支持BR,也就是双模设备。单模只能和BR和单模的设备通信,不能和传统蓝牙通信,由于功耗低,待机长,所以常用在手环的智能设备上。

2.连接流程

3711cfbf7128

android的ble连接流程图

3.什么是GATT?

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值