1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
{*******************************************************}
{ }
{ Delphi Runtime Library }
{ }
{ Files: bluetoothleapis.h bthledef.h }
{ Copyright (C) Microsoft Corporation. }
{ All Rights Reserved. }
{ }
{ Translator: Embarcadero Technologies, Inc. }
{ Copyright(c) 1995-2014 Embarcadero Technologies, Inc. }
{ }
{*******************************************************}
// Exemples: https://github.com/Appmethod/Appmethod115/blob/master/Object%20Pascal/Mobile%20Samples/Device%20Sensors%20and%20Services/Bluetooth/ExploreDevicesLE/uExploreDevices.pas
{$WARNING .$EXTERNALSYM n'est plus supporté}
unit WinBluetoothLE;
{$mode delphi}
{$ALIGN ON}
{$MINENUMSIZE 4}
{$WEAKPACKAGEUNIT}
interface
uses
Windows;
//{$HPPEMIT '#include <bthledef.h>'}
//{$HPPEMIT '#include <bluetoothleapis.h>'}
const LIB_BluetoothAPIs = 'BluetoothAPIs.dll';
//external LIB_BluetoothAPIs name
// Noms des fonction Bluetouffe
const BLUETOOTH_MAX_NAME_SIZE = 248;
const
PROC_NAME_BluetoothFindFirstDevice = 'BluetoothFindFirstDevice';
PROC_NAME_BluetoothFindNextDevice = 'BluetoothFindNextDevice';
PROC_NAME_BluetoothFindDeviceClose = 'BluetoothFindDeviceClose';
PROC_NAME_BluetoothRemoveDevice = 'BluetoothRemoveDevice';
// Noms des fonctions BLE
const
PROC_NAME_BluetoothGATTGetServices = 'BluetoothGATTGetServices';
PROC_NAME_BluetoothGATTGetIncludedServices = 'BluetoothGATTGetIncludedServices';
PROC_NAME_BluetoothGATTGetCharacteristics = 'BluetoothGATTGetCharacteristics';
PROC_NAME_BluetoothGATTGetDescriptors = 'BluetoothGATTGetDescriptors';
PROC_NAME_BluetoothGATTGetCharacteristicValue = 'BluetoothGATTGetCharacteristicValue';
PROC_NAME_BluetoothGATTGetDescriptorValue = 'BluetoothGATTGetDescriptorValue';
PROC_NAME_BluetoothGATTBeginReliableWrite = 'BluetoothGATTBeginReliableWrite';
PROC_NAME_BluetoothGATTEndReliableWrite = 'BluetoothGATTEndReliableWrite';
PROC_NAME_BluetoothGATTAbortReliableWrite = 'BluetoothGATTAbortReliableWrite';
PROC_NAME_BluetoothGATTSetCharacteristicValue = 'BluetoothGATTSetCharacteristicValue';
PROC_NAME_BluetoothGATTSetDescriptorValue = 'BluetoothGATTSetDescriptorValue';
PROC_NAME_BluetoothGATTRegisterEvent = 'BluetoothGATTRegisterEvent';
PROC_NAME_BluetoothGATTUnregisterEvent = 'BluetoothGATTUnregisterEvent';
///////////////////////////////
///
/// Start bthledef.h
///
///////////////////////////////
{ Bluetooth LE device interface GUID }
const
GUID_BLUETOOTHLE_DEVICE_INTERFACE : TGUID = '{781AEE18-7733-4CE4-ADD0-91F41C67B592}';
GUID_BLUETOOTH_GATT_SERVICE_DEVICE_INTERFACE: TGUID = '{6E3BB679-4372-40C8-9EAA-4509DF260CD8}'; { Bluetooth LE Service device interface GUID }
BTH_LE_ATT_BLUETOOTH_BASE_GUID : TGUID = '{00000000-0000-1000-8000-00805F9B34FB}';
{ ////////////////////////////////////////////////////////////////////////////// }
{ GATT Constants }
{ ////////////////////////////////////////////////////////////////////////////// }
const
E_BLUETOOTH_ATT_INVALID_HANDLE = $80650001;
E_BLUETOOTH_ATT_READ_NOT_PERMITTED = $80650002;
E_BLUETOOTH_ATT_WRITE_NOT_PERMITTED = $80650003;
E_BLUETOOTH_ATT_INVALID_PDU = $80650004;
E_BLUETOOTH_ATT_INSUFFICIENT_AUTHENTICATION = $80650005;
E_BLUETOOTH_ATT_REQUEST_NOT_SUPPORTED = $80650006;
E_BLUETOOTH_ATT_INVALID_OFFSET = $80650007;
E_BLUETOOTH_ATT_INSUFFICIENT_AUTHORIZATION = $80650008;
E_BLUETOOTH_ATT_PREPARE_QUEUE_FULL = $80650009;
E_BLUETOOTH_ATT_ATTRIBUTE_NOT_FOUND = $8065000A;
E_BLUETOOTH_ATT_ATTRIBUTE_NOT_LONG = $8065000B;
E_BLUETOOTH_ATT_INSUFFICIENT_ENCRYPTION_KEY_SIZE = $8065000C;
E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH = $8065000D;
E_BLUETOOTH_ATT_UNLIKELY = $8065000E;
E_BLUETOOTH_ATT_INSUFFICIENT_ENCRYPTION = $8065000F;
E_BLUETOOTH_ATT_UNSUPPORTED_GROUP_TYPE = $80650010;
E_BLUETOOTH_ATT_INSUFFICIENT_RESOURCES = $80650011;
E_BLUETOOTH_ATT_UNKNOWN_ERROR = $80651000;
{ Services UUIDs (Assigned numbers) }
const
BTH_LE_SERVICE_GAP = $1800;
BTH_LE_SERVICE_GATT = $1801;
{ GATT attribute types (Assigned numbers) }
BTH_LE_GATT_ATTRIBUTE_TYPE_PRIMARY_SERVICE = $2800;
BTH_LE_GATT_ATTRIBUTE_TYPE_SECONDARY_SERVICE = $2801;
BTH_LE_GATT_ATTRIBUTE_TYPE_INCLUDE = $2802;
BTH_LE_GATT_ATTRIBUTE_TYPE_CHARACTERISTIC = $2803;
{ GATT Characteristic Descriptors (Assigned numbers) }
BTH_LE_GATT_CHARACTERISTIC_DESCRIPTOR_EXTENDED_PROPERTIES = $2900;
BTH_LE_GATT_CHARACTERISTIC_DESCRIPTOR_USER_DESCRIPTION = $2901;
BTH_LE_GATT_CHARACTERISTIC_DESCRIPTOR_CLIENT_CONFIGURATION = $2902;
BTH_LE_GATT_CHARACTERISTIC_DESCRIPTOR_SERVER_CONFIGURATION = $2903;
BTH_LE_GATT_CHARACTERISTIC_DESCRIPTOR_FORMAT = $2904;
BTH_LE_GATT_CHARACTERISTIC_DESCRIPTOR_AGGREGATE_FORMAT = $2905;
{ GATT Characteristic Types (Assigned numbers) }
BTH_LE_GATT_CHARACTERISTIC_TYPE_DEVICE_NAME = $2A00;
BTH_LE_GATT_CHARACTERISTIC_TYPE_APPEARANCE = $2A01;
BTH_LE_GATT_CHARACTERISTIC_TYPE_PERIPHERAL_PRIVACY_FLAG = $2A02;
BTH_LE_GATT_CHARACTERISTIC_TYPE_RECONNECTION_ADDRESS = $2A03;
BTH_LE_GATT_CHARACTERISTIC_TYPE_PERIPHERAL_PREFERED_CONNECTION_PARAMETER = $2A04;
BTH_LE_GATT_CHARACTERISTIC_TYPE_SERVICE_CHANGED = $2A05;
BTH_LE_GAP_APPEARANCE_CATEGORY_OFFSET = $6;
BTH_LE_GAP_APPEARANCE_CATEGORY_MASK = $3ff;
function BTH_LE_GAP_APPEARANCE_GET_CATEGORY(a: Cardinal): Cardinal; inline;
const
BTH_LE_GAP_APPEARANCE_SUB_CATEGORY_MASK = $3f;
function BTH_LE_GAP_APPEARANCE_GET_SUB_CATEGORY(a: Cardinal): Byte; inline;
const
BTH_LE_GAP_APPEARANCE_CATEGORY_PHONE = $0001;
BTH_LE_GAP_APPEARANCE_CATEGORY_COMPUTER = $0002;
BTH_LE_GAP_APPEARANCE_CATEGORY_WATCH = $0003;
BTH_LE_GAP_APPEARANCE_CATEGORY_CLOCK = $0004;
BTH_LE_GAP_APPEARANCE_CATEGORY_DISPLAY = $0005;
BTH_LE_GAP_APPEARANCE_CATEGORY_REMOTE_CONTROL = $0006;
BTH_LE_GAP_APPEARANCE_CATEGORY_EYE_GLASSES = $0007;
BTH_LE_GAP_APPEARANCE_CATEGORY_TAG = $0008;
BTH_LE_GAP_APPEARANCE_CATEGORY_KEYRING = $0009;
BTH_LE_GAP_APPEARANCE_CATEGORY_MEDIA_PLAYER = $000a;
BTH_LE_GAP_APPEARANCE_CATEGORY_BARCODE_SCANNER = $000b;
BTH_LE_GAP_APPEARANCE_CATEGORY_THERMOMETER = $000c;
BTH_LE_GAP_APPEARANCE_CATEGORY_HEART_RATE = $000d;
BTH_LE_GAP_APPEARANCE_CATEGORY_BLOOD_PRESSURE = $000e;
BTH_LE_GAP_APPEARANCE_CATEGORY_HID = $000f;
BTH_LE_GAP_APPEARANCE_CATEGORY_GLUCOSE_METER = $0010;
BTH_LE_GAP_APPEARANCE_CATEGORY_RUNNING_WALKING_SENSOR = $0011;
BTH_LE_GAP_APPEARANCE_CATEGORY_CYCLING = $0012;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_KEYBOARD = $01;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_MOUSE = $02;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_JOYSTICK = $03;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_GAMEPAD = $04;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_DIGITIZER_TABLET = $05;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_CARD_READER = $06;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_DIGITAL_PEN = $07;
BTH_LE_GAP_APPEARANCE_HID_SUBCATEGORY_BARCODE_SCANNER = $08;
{ GATT Included Services Default Maximum Nested Depth }
BTH_LE_GATT_DEFAULT_MAX_INCLUDED_SERVICES_DEPTH = 3;
{ ////////////////////////////////////////////////////////////////////////////// }
{ ATT Constants }
{ ////////////////////////////////////////////////////////////////////////////// }
{ Transation timeout }
BTH_LE_ATT_TRANSACTION_TIMEOUT = 30; { seconds }
{ Maximum size of any attribute value }
BTH_LE_ATT_MAX_VALUE_SIZE = 512;
{ CID }
BTH_LE_ATT_CID = $0004;
{ MTU }
BTHLEENUM_ATT_MTU_MIN = 23;
BTHLEENUM_ATT_MTU_MAX = $FFFF;
BTHLEENUM_ATT_MTU_DEFAULT = BTHLEENUM_ATT_MTU_MIN;
BTHLEENUM_ATT_MTU_INITIAL_NEGOTIATION = 525;
{ ////////////////////////////////////////////////////////////////////////////// }
{ ATT-specific Error Codes }
{ ////////////////////////////////////////////////////////////////////////////// }
BTH_LE_ERROR_INVALID_HANDLE = $01;
BTH_LE_ERROR_READ_NOT_PERMITTED = $02;
BTH_LE_ERROR_WRITE_NOT_PERMITTED = $03;
BTH_LE_ERROR_INVALID_PDU = $04;
BTH_LE_ERROR_INSUFFICIENT_AUTHENTICATION = $05;
BTH_LE_ERROR_REQUEST_NOT_SUPPORTED = $06;
BTH_LE_ERROR_INVALID_OFFSET = $07;
BTH_LE_ERROR_INSUFFICIENT_AUTHORIZATION = $08;
BTH_LE_ERROR_PREPARE_QUEUE_FULL = $09;
BTH_LE_ERROR_ATTRIBUTE_NOT_FOUND = $0A;
BTH_LE_ERROR_ATTRIBUTE_NOT_LONG = $0B;
BTH_LE_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE = $0C;
BTH_LE_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH = $0D;
BTH_LE_ERROR_UNLIKELY = $0E;
BTH_LE_ERROR_INSUFFICIENT_ENCRYPTION = $0F;
BTH_LE_ERROR_UNSUPPORTED_GROUP_TYPE = $10;
BTH_LE_ERROR_INSUFFICIENT_RESOURCES = $11;
BTH_LE_ERROR_UNKNOWN = $1000;
{ ////////////////////////////////////////////////////////////////////////////// }
{ GATT Security and Other Flag-related Facilities }
{ ////////////////////////////////////////////////////////////////////////////// }
BLUETOOTH_GATT_FLAG_NONE = $00000000;
BLUETOOTH_GATT_FLAG_CONNECTION_ENCRYPTED = $00000001;
BLUETOOTH_GATT_FLAG_CONNECTION_AUTHENTICATED = $00000002;
BLUETOOTH_GATT_FLAG_FORCE_READ_FROM_DEVICE = $00000004;
BLUETOOTH_GATT_FLAG_FORCE_READ_FROM_CACHE = $00000008;
BLUETOOTH_GATT_FLAG_SIGNED_WRITE = $00000010;
BLUETOOTH_GATT_FLAG_WRITE_WITHOUT_RESPONSE = $00000020;
BLUETOOTH_GATT_FLAG_RETURN_ALL = $00000040;
BLUETOOTH_GATT_FLAG_VALID_MASK = BLUETOOTH_GATT_FLAG_NONE
or BLUETOOTH_GATT_FLAG_CONNECTION_ENCRYPTED
or BLUETOOTH_GATT_FLAG_CONNECTION_AUTHENTICATED
or BLUETOOTH_GATT_FLAG_FORCE_READ_FROM_DEVICE
or BLUETOOTH_GATT_FLAG_FORCE_READ_FROM_CACHE
or BLUETOOTH_GATT_FLAG_SIGNED_WRITE
or BLUETOOTH_GATT_FLAG_WRITE_WITHOUT_RESPONSE
or BLUETOOTH_GATT_FLAG_RETURN_ALL;
function IS_BLUETOOTH_GATT_FLAG_VALID(f: Cardinal): Boolean; inline;
{ ////////////////////////////////////////////////////////////////////////////// }
{ GATT Structures }
{ ////////////////////////////////////////////////////////////////////////////// }
type
BLUETOOTH_GATT_EVENT_HANDLE = THandle;
TBluetoothGattEventHandle = BLUETOOTH_GATT_EVENT_HANDLE;
PBluetoothGattEventHandle = ^TBluetoothGattEventHandle;
_BTH_LE_UUID = record
IsShortUuid: Boolean;
case Integer of
0: (LongUuid: TGUID);
1: (ShortUuid: USHORT);
2: (ForceAlign4: Int32);
end;
BTH_LE_UUID = _BTH_LE_UUID;
PBTH_LE_UUID = ^BTH_LE_UUID;
TBthLeUuid = BTH_LE_UUID;
PBthLeUuid = ^TBthLeUuid;
_BTH_LE_GATT_SERVICE = record
ServiceUuid: TBthLeUuid;
AttributeHandle: USHORT;
end;
BTH_LE_GATT_SERVICE = _BTH_LE_GATT_SERVICE;
PBTH_LE_GATT_SERVICE = ^BTH_LE_GATT_SERVICE;
TBthLeGattService = BTH_LE_GATT_SERVICE;
PBthLeGattService = ^TBthLEGattService;
type
BTH_LE_GATT_DESCRIPTOR_TYPE = (
CharacteristicExtendedProperties = 0,
CharacteristicUserDescription = 1,
ClientCharacteristicConfiguration = 2,
ServerCharacteristicConfiguration = 3,
CharacteristicFormat = 4,
CharacteristicAggregateFormat = 5,
CustomDescriptor = 6
);
PBTH_LE_GATT_DESCRIPTOR_TYPE = ^BTH_LE_GATT_DESCRIPTOR_TYPE;
TBthLeGattDescriptorType = BTH_LE_GATT_DESCRIPTOR_TYPE;
PBthLeGattDescriptorType = ^TBthLeGattDescriptorType;
_BTH_LE_GATT_CHARACTERISTIC = record
ServiceHandle: USHORT;
CharacteristicUuid: TBthLeUuid;
AttributeHandle: USHORT;
CharacteristicValueHandle: USHORT;
IsBroadcastable: BOOLEAN;
IsReadable: BOOLEAN;
IsWritable: BOOLEAN;
IsWritableWithoutResponse: BOOLEAN;
IsSignedWritable: BOOLEAN;
IsNotifiable: BOOLEAN;
IsIndicatable: BOOLEAN;
HasExtendedProperties: BOOLEAN;
end;
BTH_LE_GATT_CHARACTERISTIC = _BTH_LE_GATT_CHARACTERISTIC;
PBTH_LE_GATT_CHARACTERISTIC = ^BTH_LE_GATT_CHARACTERISTIC;
TBthLeGattCharacteristic = BTH_LE_GATT_CHARACTERISTIC;
PBthLeGattCharacteristic = ^TBthLeGattCharacteristic;
_BTH_LE_GATT_CHARACTERISTIC_VALUE = record
DataSize: ULONG;
Data: PByte;
//Data: array [0..0] of byte;
end;
BTH_LE_GATT_CHARACTERISTIC_VALUE = _BTH_LE_GATT_CHARACTERISTIC_VALUE;
PBTH_LE_GATT_CHARACTERISTIC_VALUE = ^BTH_LE_GATT_CHARACTERISTIC_VALUE;
TBthLeGattCharacteristicValue = BTH_LE_GATT_CHARACTERISTIC_VALUE;
PBthLeGattCharacteristicValue = ^TBthLeGattCharacteristicValue;
BTH_LE_GATT_DESCRIPTOR = record
ServiceHandle: USHORT;
CharacteristicHandle: USHORT;
DescriptorType: TBthLeGattDescriptorType;
DescriptorUuid: TBthLeUuid;
AttributeHandle: USHORT;
end;
_BTH_LE_GATT_DESCRIPTOR = BTH_LE_GATT_DESCRIPTOR;
PBTH_LE_GATT_DESCRIPTOR = ^BTH_LE_GATT_DESCRIPTOR;
TBthLeGattDescriptor = BTH_LE_GATT_DESCRIPTOR;
PBthLeGattDescriptor = ^TBthLeGattDescriptor;
AuxGattDescriptorType = record
case BTH_LE_GATT_DESCRIPTOR_TYPE of
CharacteristicExtendedProperties: (
IsReliableWriteEnabled: BOOLEAN;
IsAuxiliariesWritable: BOOLEAN;
);
ClientCharacteristicConfiguration: (
IsSubscribeToNotification: BOOLEAN;
IsSubscribeToIndication: BOOLEAN;
);
ServerCharacteristicConfiguration: (IsBroadcast: BOOLEAN);
CharacteristicFormat: (
Format: UCHAR;
Exponent: ShortInt;
&Unit: TBthLeUuid;
NameSpace: UCHAR;
Description: TBthLeUuid;
);
end;
_BTH_LE_GATT_DESCRIPTOR_VALUE = record
DescriptorType: TBthLeGattDescriptorType;
DescriptorUuid: TBthLeUuid;
DescriptorInfo: AuxGattDescriptorType;
DataSize: ULONG;
Data: PByte;
end;
BTH_LE_GATT_DESCRIPTOR_VALUE = _BTH_LE_GATT_DESCRIPTOR_VALUE;
PBTH_LE_GATT_DESCRIPTOR_VALUE = ^BTH_LE_GATT_DESCRIPTOR_VALUE;
TBthLeGattDescriptorValue = BTH_LE_GATT_DESCRIPTOR_VALUE;
PBthLeGattDescriptorValue = ^TBthLeGattDescriptorValue;
BTH_LE_GATT_EVENT_TYPE = (
CharacteristicValueChangedEvent = 0
);
TBthLeGattEventType = BTH_LE_GATT_EVENT_TYPE;
{ Callback function signature for Bluetooth GATT events. }
type
PFNBLUETOOTH_GATT_EVENT_CALLBACK = procedure(
EventType: TBthLeGattEventType; EventOutParameter: Pointer;
Context: Pointer); cdecl;
PFnBluetoothGattEventCallback = PFNBLUETOOTH_GATT_EVENT_CALLBACK;
{ Structure associated with a CharacteristicValueChanged Event }
PBluetoothGattValueChangedEventRegistration = ^TBluetoothGattValueChangedEventRegistration;
BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION = record
NumCharacteristics: USHORT;
Characteristics: array [0..0] of TBthLeGattCharacteristic;
end;
_BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION = BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION;
PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION = ^BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION;
TBluetoothGattValueChangedEventRegistration = BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION;
BLUETOOTH_GATT_VALUE_CHANGED_EVENT = record
ChangedAttributeHandle: USHORT;
CharacteristicValueDataSize: size_t;
CharacteristicValue: PBthLeGattCharacteristicValue;
end;
_BLUETOOTH_GATT_VALUE_CHANGED_EVENT = BLUETOOTH_GATT_VALUE_CHANGED_EVENT;
PBLUETOOTH_GATT_VALUE_CHANGED_EVENT = ^BLUETOOTH_GATT_VALUE_CHANGED_EVENT;
TBluetoothGattValueChangedEvent = BLUETOOTH_GATT_VALUE_CHANGED_EVENT;
PBluetoothGattValueChangedEvent = ^TBluetoothGattValueChangedEvent;
BTH_LE_GATT_RELIABLE_WRITE_CONTEXT = ULONG64;
PBTH_LE_GATT_RELIABLE_WRITE_CONTEXT = ^BTH_LE_GATT_RELIABLE_WRITE_CONTEXT;
TBthLeGattReliableWriteContext = BTH_LE_GATT_RELIABLE_WRITE_CONTEXT;
PBthLeGattReliableWriteContext = ^TBthLeGattReliableWriteContext;
function IsBthLEUuidMatch(uuid1: TBthLeUuid; uuid2: TBthLeUuid): Boolean; inline;
///////////////////////////////
///
/// End bthledef.h
///
///////////////////////////////
type
BTH_ADDR = UInt64;
BOOL = Longbool;
SYSTEMTIME = record
wYear : Word;
wMonth : Word;
wDayOfWeek : Word;
wDay : Word;
wHour : Word;
wMinute : Word;
wSecond : Word;
wMilliseconds : Word;
end;
BLUETOOTH_ADDRESS = record
case Integer of
0: (ullLong: BTH_ADDR);
1: (rgBytes: array [0..5] of Byte);
end;
BLUETOOTH_DEVICE_SEARCH_PARAMS = record
dwSize : DWORD;
fReturnAuthenticated : BOOL;
fReturnRemembered : BOOL;
fReturnUnknown : BOOL;
fReturnConnected : BOOL;
fIssueInquiry : BOOL;
cTimeoutMultiplier : Byte;
hRadio : THandle;
end;
BLUETOOTH_DEVICE_INFO = record
dwSize : DWORD;
Address : BLUETOOTH_ADDRESS;
ulClassofDevice : DWORD;
fConnected : BOOL;
fRemembered : BOOL;
fAuthenticated : BOOL;
stLastSeen : SYSTEMTIME;
stLastUsed : SYSTEMTIME;
szName : array[0..BLUETOOTH_MAX_NAME_SIZE-1] of WideChar;
end;
BLUETOOTH_FIND_RADIO_PARAMS = record
dwSize: DWORD;
end;
HBLUETOOTH_DEVICE_FIND = THandle;
// ces fonctions ne détectent pas le BLE
(*
The BluetoothFindFirstDevice function does not find Bluetooth Low Energy (LE) devices.
To access Bluetooth LE devices, use the Windows Runtime Bluetooth Low Energy APIs.
The Windows Runtime APIs for Bluetooth work in both UWP and classic desktop apps.
//*)
function BluetoothFindFirstDevice(const pbtsp: BLUETOOTH_DEVICE_SEARCH_PARAMS; var pbtdi: BLUETOOTH_DEVICE_INFO): HBLUETOOTH_DEVICE_FIND; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothFindFirstDevice;
function BluetoothFindNextDevice(hFind: HBLUETOOTH_DEVICE_FIND; var pbtdi: BLUETOOTH_DEVICE_INFO): BOOL; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothFindNextDevice;
function BluetoothFindDeviceClose(hFind: HBLUETOOTH_DEVICE_FIND): BOOL; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothFindDeviceClose;
function BluetoothRemoveDevice(const pAddress : BLUETOOTH_ADDRESS): BOOL; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothRemoveDevice;
//function BluetoothFindFirstRadio (const pbtfrp: BLUETOOTH_FIND_RADIO_PARAMS; phRadio: PHANDLE):external LIB_BluetoothAPIs name PROC_NAME_BluetoothFindFirstDevice;
///////////////////////////////
///
/// Start BluetoothLEAPIs.h
///
///////////////////////////////
function BluetoothGATTGetServices(hDevice: THandle; ServicesBufferCount : Word;
ServicesBuffer: PBthLeGattService; var ServicesBufferActual: Word; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTGetServices;
function BluetoothGATTGetIncludedServices(hDevice: THandle; ParentService: PBthLeGattService;
IncludedServicesBufferCount: Word; IncludedServicesBuffer: PBthLeGattService;
var IncludedServicesBufferActual: Word; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTGetIncludedServices;
function BluetoothGATTGetCharacteristics(hDevice: THandle; Service: PBthLeGattService;
CharacteristicsBufferCount : Word; CharacteristicsBuffer: PBthLeGattCharacteristic;
var CharacteristicsBufferActual : Word; Flags : Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTGetCharacteristics;
function BluetoothGATTGetDescriptors(hDevice: THandle; var Characteristic: TBthLeGattCharacteristic;
DescriptorsBufferCount: Word; DescriptorsBuffer: PBthLeGattDescriptor;
var DescriptorsBufferActual: Word; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTGetDescriptors;
function BluetoothGATTGetCharacteristicValue(hDevice: THandle; var Characteristic: TBthLeGattCharacteristic;
CharacteristicValueDataSize: Cardinal; CharacteristicValue: PBthLeGattCharacteristicValue;
CharacteristicValueSizeRequired: PWord; Flags: ULONG): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTGetCharacteristicValue;
function BluetoothGATTGetDescriptorValue(hDevice: THandle; var Descriptor: TBthLeGattDescriptor;
DescriptorValueDataSize: Cardinal; DescriptorValue: PBthLeGattDescriptorValue;
DescriptorValueSizeRequired: PWord; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTGetDescriptorValue;
function BluetoothGATTBeginReliableWrite(hDevice: THandle; var ReliableWriteContext: TBthLeGattReliableWriteContext; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTBeginReliableWrite;
function BluetoothGATTEndReliableWrite(hDevice: THandle; ReliableWriteContext: TBthLeGattReliableWriteContext; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTEndReliableWrite;
function BluetoothGATTAbortReliableWrite(hDevice: THandle; ReliableWriteContext: TBthLeGattReliableWriteContext; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTAbortReliableWrite;
function BluetoothGATTSetCharacteristicValue(hDevice: THandle; var Characteristic: TBthLeGattCharacteristic;
CharacteristicValue: PBthLeGattCharacteristicValue; ReliableWriteContext: TBthLeGattReliableWriteContext;
Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTSetCharacteristicValue;
function BluetoothGATTSetDescriptorValue(hDevice: THandle; var Descriptor: TBthLeGattDescriptor;
DescriptorValue: PBthLeGattDescriptorValue; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTSetDescriptorValue;
function BluetoothGATTRegisterEvent(hService: THandle; EventType: TBthLeGattEventType; EventParameterIn: Pointer;
Callback: PFnBluetoothGattEventCallback; CallbackContext: Pointer;
var EventHandle: TBluetoothGattEventHandle; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTRegisterEvent;
function BluetoothGATTUnregisterEvent(EventHandle: TBluetoothGattEventHandle; Flags: Cardinal): HRESULT; stdcall;
external LIB_BluetoothAPIs name PROC_NAME_BluetoothGATTUnregisterEvent;
///////////////////////////////
///
/// End BluetoothLEAPIs.h
///
///////////////////////////////
implementation
uses
SysUtils;
function BTH_LE_GAP_APPEARANCE_GET_CATEGORY(a: Cardinal): Cardinal;
begin
Result := (a shr BTH_LE_GAP_APPEARANCE_CATEGORY_OFFSET) and BTH_LE_GAP_APPEARANCE_CATEGORY_MASK;
end;
function BTH_LE_GAP_APPEARANCE_GET_SUB_CATEGORY(a: Cardinal): Byte;
begin
Result := a and BTH_LE_GAP_APPEARANCE_SUB_CATEGORY_MASK;
end;
function IS_BLUETOOTH_GATT_FLAG_VALID(f: Cardinal): Boolean;
begin
Result := (f and not BLUETOOTH_GATT_FLAG_VALID_MASK) = 0;
end;
function IsBthLEUuidMatch(uuid1: TBthLeUuid; uuid2: TBthLeUuid): Boolean;
var
tempLongUuid: TBthLeUuid;
function IsSameLongUUID(const LU1, LU2: TGUID): boolean;
var
EWE1, EWE2: TBytes;
i: Integer;
begin
// Comparaison en mode ACBM (abruti, con, bête et méchant)
EWE1 := LU1.ToByteArray();
EWE2 := LU2.ToByteArray();
if (high(EWE1) <> high(EWE2)) then exit(false); // longueurs différentes ? OK je sors
Result := True;
for i := 0 to High(EWE1) do
begin
if (EWE1[i] = EWE1[i]) then
Result := Result AND True
else // lazy eval
exit(False);
end;
end;
begin
Result := False;
tempLongUuid := Default(TBthLeUuid);
tempLongUuid.IsShortUuid := False;
tempLongUuid.LongUuid := BTH_LE_ATT_BLUETOOTH_BASE_GUID;
if (uuid1.IsShortUuid and uuid2.IsShortUuid) then
Result := uuid1.ShortUuid = uuid2.ShortUuid
else if ((not uuid1.IsShortUuid) and (not uuid2.IsShortUuid)) then
Result := IsSameLongUUID(uuid1.LongUuid, uuid2.LongUuid)
else if (uuid1.IsShortUuid) then
begin
Inc(tempLongUuid.LongUuid.D1, uuid1.ShortUuid);
Result := IsSameLongUUID(tempLongUuid.LongUuid, uuid2.LongUuid);
end
else if (uuid2.IsShortUuid) then
begin
Inc(tempLongUuid.LongUuid.D1, uuid2.ShortUuid);
Result := IsSameLongUUID(tempLongUuid.LongUuid, uuid1.LongUuid);
end;
end;
end. |
Partager