delphi 蓝牙 TBluetoothLE

本文详细介绍了使用Delphi实现蓝牙低能耗(BLE)连接的过程,包括搜索设备、服务和特征,订阅与发送接收数据,以及断开连接的操作。

delphi 蓝牙 TBluetoothLE、TBluetoothLEManager BLE

http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_Bluetooth_Low_Energy

指定UUID

HRSERVICE: TBluetoothUUID = '{0000180D-0000-1000-8000-00805F9B34FB}';
HRMEASUREMENT_CHARACTERISTIC: TBluetoothUUID = '{00002A37-0000-1000-8000-00805F9B34FB}';
BODY_SENSOR_LOCATION_CHARACTERISTIC: TBluetoothUUID = '{00002A38-0000-1000-8000-00805F9B34FB}';

1、搜索设备

方法一、

   BluetoothLE1.DiscoverDevices(4000);

方法二、

  HRSERVICE: TBluetoothUUID = '{0000180D-0000-1000-8000-00805F9B34FB}';

  BluetoothLE1.DiscoverDevices(2500, [HRSERVICE]);

触发事件

BluetoothLE1EndDiscoverDevices

BluetoothLE1DiscoverLEDevice

2、搜索服务

BluetoothLE1.DiscoverServices(FCurrentDevice)//搜索所有服务

BluetoothLE1.DiscoveredDevices[ListBox1.ItemIndex].DiscoverServices

 

FGattService:=BluetoothLE1.GetService(FBLEDevice, HRSERVICE);//搜索指定UUID服务,同于第一步的UUID

3、查询特征

BluetoothLE1.GetCharacteristics(FWeightGattService);//特征列表

BluetoothLE1.GetCharacteristic(FWeightGattService, UUIDchar);//UUID指定的某个特征

FWeightMeasurementGattCharacteristic := BluetoothLE1.GetCharacteristic(FGattService,Weight_CHARACTERISTIC);

触发事件

BluetoothLE1EndDiscoverServices

 

搜索Characteristics

for C := 0 to AServiceList[I].Characteristics.Count - 1 do
ListBox2.Items.Add(' - ' + AServiceList[I].Characteristics[C].UUIDName + ' - ' + AServiceList[I].Characteristics[C].UUID.ToString);

 

4、订阅

BluetoothLE1.SubscribeToCharacteristic(FBLEDevice, FHRMeasurementGattCharact);

BluetoothLE1.SubscribeToCharacteristic(FBLEDevice, FWeightMeasurementGattCharacteristic);

ADevice.SetCharacteristicNotification(ACharacteristic, True)

 5、发送数据

BluetoothLE1.WriteCharacteristic(TBluetoothLEDevice ADevice,TBluetoothGattCharacteristic ACharacteristic);

ADevice.WriteCharacteristic(AChar);

 

 

FGattChar.SetValue(sbytes);
bflag := FCurrentDevice.WriteCharacteristic(FGattChar)

 

6、接收数据

procedure BluetoothLE1CharacteristicRead(const Sender: TObject; const ACharacteristic: TBluetoothGattCharacteristic;
AGattStatus: TBluetoothGattStatus);

 

 

7、断开

BluetoothLE1.UnSubscribeToCharacteristic(FBLEDevice, FWeightMeasurementGattCharacteristic);

BluetoothLE1.UnSubscribeToCharacteristic(FBLEDevice, FHRMeasurementGattCharact);

 ADevice.SetCharacteristicNotification(ACharacteristic, False)

 

http://blogs.embarcadero.com/sarinadupont/2014/10/20/creating-a-bluetooth-le-cloud-enabled-luggage-scale-application/

转载于:https://www.cnblogs.com/cb168/p/4845183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值