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
|
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base,
FMX.StdCtrls, FMX.ListView, FMX.Controls.Presentation, FMX.MultiView,
FMX.Layouts, FMX.ListBox, Data.Bind.GenData, System.Rtti,
System.Bindings.Outputs, FMX.Bind.Editors, Data.Bind.EngExt,
FMX.Bind.DBEngExt, Data.Bind.Components, Data.Bind.ObjectScope,
FMX.Bind.GenData, FMX.Edit, Math, FMX.Colors, System.Bluetooth,
System.Bluetooth.Components, System.Permissions;
type
TForm1 = class(TForm)
Button1: TButton;
ListView1: TListView;
BluetoothLE1: TBluetoothLE;
Timer1: TTimer;
ProgressBar1: TProgressBar;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure ListView1ButtonClick(const Sender: TObject;
const AItem: TListItem; const AObject: TListItemSimpleControl);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BluetoothLE1EndDiscoverDevices(const Sender: TObject;
const ADeviceList: TBluetoothLEDeviceList);
procedure BluetoothLE1DiscoverLEDevice(const Sender: TObject;
const ADevice: TBluetoothLEDevice; Rssi: Integer;
const ScanResponse: TScanResponse);
procedure FormShow(Sender: TObject);
procedure BluetoothLE1ServicesDiscovered(const Sender: TObject;
const AServiceList: TBluetoothGattServiceList);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
Scanning: Boolean;
ScanningStart: Cardinal;
FLocationPermission: string;
procedure RequestPermissionsResult(Sender: TObject;
const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
procedure DisplayRationale(Sender: TObject;
const APermissions: TArray<string>; const APostRationaleProc: TProc);
procedure StartBLEDiscovery;
procedure StopBLEDiscovery;
{ Déclarations privées }
public
{ Déclarations publiques }
end;
type
// Declare a customer record
Tmybox = record
Company: string;
Serial: string;
Serial_Bin: string;
Firm: string;
Hard: string;
Box_type: string;
Remplissage: string;
Batterie: string;
Etat: string;
end;
const
ScanningTime = 10000; // 10s in msecs
var
Form1: TForm1;
Box: Tmybox;
buf_bin: string;
buf: string;
TDNB: string;
FMyDevice: TBluetoothLEDevice;
Ftest, Ftest2, Ftest3, Ftest4, Ftest5, Ftest6,
Ftest7: TBluetoothGattCharacteristic;
FService: TBluetoothGattService;
myServiceList: TBluetoothGattServiceList;
born: Integer;
function HexToBin(HexStr: string): string;
function BinToInt(AValue: string): Integer;
const
//HRSERVICE: TBluetoothUUID = '{0000180D-0000-1000-8000-00805F9B34FB}';
CHARACTERISTIC_ID: TGUID = '{9f1485f3-43ed-11e4-916c-0800200c9a66}';
// pour ce connecter
implementation
uses
{$IFDEF ANDROID}
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
{$ENDIF}
FMX.DialogService;
{$R *.fmx}
{$R *.GGlass.fmx ANDROID}
// {$R *.Surface.fmx MSWINDOWS}
procedure TForm1.DisplayRationale(Sender: TObject;
const APermissions: TArray<string>; const APostRationaleProc: TProc);
begin
TDialogService.showmessage
('We need to be given permission to discover BLE devices',
procedure(const AResult: TModalResult)
begin
APostRationaleProc;
end)
end;
procedure TForm1.RequestPermissionsResult(Sender: TObject;
const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
begin
// 1 permissions involved: ACCESS_COARSE_LOCATION
if (length(AGrantResults) = 1) and
(AGrantResults[0] = TPermissionStatus.Granted) then
StartBLEDiscovery
else
TDialogService.showmessage
('Cannot start BLE scan as the permission has not been granted');
end;
procedure TForm1.StartBLEDiscovery;
begin
if not Scanning then
begin
// ListBox1.Clear;
ScanningStart := TThread.GetTickCount;
BluetoothLE1.DiscoverDevices(ScanningTime);
ProgressBar1.Value := 0;
Timer1.Enabled := True;
Scanning := True;
end;
end;
procedure TForm1.StopBLEDiscovery;
begin
Timer1.Enabled := False;
Scanning := False;
BluetoothLE1.CancelDiscovery;
end;
function ByteToHex(InByte: byte): string;
const
Digits: array [0 .. 15] of char = '0123456789ABCDEF';
begin
result := Digits[InByte shr 4] + Digits[InByte and $0F];
end;
function HexToBin(HexStr: string): string;
const
BinArray: array [0 .. 15, 0 .. 1] of string = (('0000', '0'), ('0001', '1'),
('0010', '2'), ('0011', '3'), ('0100', '4'), ('0101', '5'), ('0110', '6'),
('0111', '7'), ('1000', '8'), ('1001', '9'), ('1010', 'A'), ('1011', 'B'),
('1100', 'C'), ('1101', 'D'), ('1110', 'E'), ('1111', 'F'));
HexAlpha: set of char = ['0' .. '9', 'A' .. 'F'];
var
i, j: Integer;
begin
result := '';
HexStr := AnsiUpperCase(HexStr);
for i := 0 to length(HexStr) do
if HexStr[i] in HexAlpha then
begin
for j := 1 to 16 do
if HexStr[i] = BinArray[j - 1, 1] then
result := result + BinArray[j - 1, 0];
end
else if HexStr[i] <> '' then
begin
result := '';
showmessage('This is not hexadecimal number');
Break;
end;
if result <> '' then
while (result[1] = '0') and (length(result) > 1) do
Delete(result, 1, 1);
end;
function BinToHex(BinStr: string): string;
const
BinArray: array [0 .. 15, 0 .. 1] of string = (('0000', '0'), ('0001', '1'),
('0010', '2'), ('0011', '3'), ('0100', '4'), ('0101', '5'), ('0110', '6'),
('0111', '7'), ('1000', '8'), ('1001', '9'), ('1010', 'A'), ('1011', 'B'),
('1100', 'C'), ('1101', 'D'), ('1110', 'E'), ('1111', 'F'));
var
Error: Boolean;
j: Integer;
BinPart: string;
begin
result := '';
Error := False;
for j := 1 to length(BinStr) do
if (BinStr[j]) <> '' then
if not(BinStr[j] in ['0', '1']) then
begin
Error := True;
showmessage('This is not binary number');
Break;
end;
if not Error then
begin
if length(BinStr) = 0 then
BinStr := '0000';
case length(BinStr) mod 4 of
1:
BinStr := '000' + BinStr;
2:
BinStr := '00' + BinStr;
3:
BinStr := '0' + BinStr;
end;
while length(BinStr) > 0 do
begin
BinPart := Copy(BinStr, length(BinStr) - 3, 4);
Delete(BinStr, length(BinStr) - 3, 4);
for j := 1 to 16 do
if BinPart = BinArray[j - 1, 0] then
result := BinArray[j - 1, 1] + result;
end;
end;
end;
Function HexToInt(Value: string): Integer;
begin
result := StrToInt('$' + Value);
end;
function BinToInt(AValue: string): Integer;
begin
result := HexToInt(BinToHex(AValue));
end;
function Data_serial(str: string): Tmybox;
var
buf_1, buf_0: string;
const
Table: array [0 .. 35] of char = ('0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
begin
Box.Serial_Bin := HexToBin(Copy(str, 5, 12));
if length(Box.Serial_Bin) < 48 then
while length(Box.Serial_Bin) < 48 do
Box.Serial_Bin := '0' + Box.Serial_Bin;
buf_0 := Copy(Box.Serial_Bin, 1, 5);
buf_1 := Table[BinToInt(Copy(Box.Serial_Bin, 1, 5)) + 9];
Box.Serial := Table[BinToInt(Copy(Box.Serial_Bin, 1, 5)) + 9] +
Table[BinToInt(Copy(Box.Serial_Bin, 6, 5)) + 9] + ' ' +
Table[BinToInt(Copy(Box.Serial_Bin, 11, 4))] +
Table[BinToInt(Copy(Box.Serial_Bin, 15, 4))] +
Table[BinToInt(Copy(Box.Serial_Bin, 19, 4))] + ' ' +
Table[BinToInt(Copy(Box.Serial_Bin, 23, 5)) + 9] +
Table[BinToInt(Copy(Box.Serial_Bin, 28, 5)) + 9] + ' ' + '20' +
inttostr(BinToInt(Copy(Box.Serial_Bin, 33, 7))) + '/' +
inttostr(BinToInt(Copy(Box.Serial_Bin, 40, 5)));
if (StrToInt(Copy(str, 28, 1)) = 1) then
Box.Box_type := 'Verre'
else if (StrToInt(Copy(str, 28, 1)) = 2) then
Box.Box_type := 'Papier'
else if (StrToInt(Copy(str, 28, 1)) = 3) then
Box.Box_type := 'Plastique'
else
Box.Box_type := 'Erreur';
if (StrToInt(Copy(str, 27, 1)) = 0) then
Box.Box_type := Box.Box_type + '(Maître)'
else if (StrToInt(Copy(str, 27, 1)) = 1) then
Box.Box_type := Box.Box_type + '(Esclave)'
else
Box.Box_type := Box.Box_type + '(Erreur)';
if (StrToInt('$' + Copy(str, 33, 2)) = $01) then
Box.Etat := 'Normal Disponible'
else if (StrToInt('$' + Copy(str, 33, 2)) = $02) then
Box.Etat := 'Normal Indisponible'
else if (StrToInt('$' + Copy(str, 33, 2)) = $11) then
Box.Etat := 'Dégradé Disponible'
else if (StrToInt('$' + Copy(str, 33, 2)) = $12) then
Box.Etat := 'Dégradé Indisponible'
else if (StrToInt('$' + Copy(str, 33, 2)) = $20) then
Box.Etat := 'Erreur'
else if (StrToInt('$' + Copy(str, 33, 2)) = $30) then
Box.Etat := 'Maintenance'
else if (StrToInt('$' + Copy(str, 33, 2)) = $40) then
Box.Etat := 'Programmation'
else if (StrToInt('$' + Copy(str, 33, 2)) = $50) then
Box.Etat := 'Sommeil'
else
Box.Etat := 'Erreur Inconnue';
Box.Company := Copy(str, 1, 4);
Box.Firm := inttostr(StrToInt('$' + (Copy(str, 17, 2)))) + '.' +
inttostr(StrToInt('$' + (Copy(str, 19, 2)))) + '.' +
inttostr(StrToInt('$' + (Copy(str, 21, 2))));
Box.Hard := inttostr(StrToInt('$' + (Copy(str, 23, 2)))) + '.' +
inttostr(StrToInt('$' + (Copy(str, 25, 2))));
Box.Remplissage := inttostr(StrToInt('$' + Copy(str, 29, 2))) + '%';
Box.Batterie := inttostr(StrToInt('$' + Copy(str, 31, 2))) + '%';
result := Box;
end;
procedure TForm1.BluetoothLE1DiscoverLEDevice(const Sender: TObject;
const ADevice: TBluetoothLEDevice; Rssi: Integer;
const ScanResponse: TScanResponse);
var
PrevDiscoveredDevicesCount: Integer;
DiscoveredDevicesCount: Integer;
DiscoveredDeviceIndex: Integer;
DiscoveredDevice: TBluetoothLEDevice;
DiscoveredDevice2: TBluetoothLEDevice;
DiscoveredDeviceName: string;
ListAjout: TListViewItem;
Value: string;
myArray: Tbytes; // dynamic array
begin
DiscoveredDevicesCount := BluetoothLE1.DiscoveredDevices.Count;
for DiscoveredDeviceIndex := 0 to DiscoveredDevicesCount - 1 do
begin
DiscoveredDevice := BluetoothLE1.DiscoveredDevices[DiscoveredDeviceIndex];
DiscoveredDeviceName := DiscoveredDevice.DeviceName;
if DiscoveredDeviceName = 'TDNB' then
begin
myArray := DiscoveredDevice.AdvertisedData.Items
[TScanResponseKey.ManufacturerSpecificData];
Value := ByteToHex(myArray[0]) + ByteToHex(myArray[1]) +
ByteToHex(myArray[2]) + ByteToHex(myArray[3]) + ByteToHex(myArray[4]) +
ByteToHex(myArray[5]) + ByteToHex(myArray[6]) + ByteToHex(myArray[7]) +
ByteToHex(myArray[8]) + ByteToHex(myArray[9]) + ByteToHex(myArray[10]) +
ByteToHex(myArray[11]) + ByteToHex(myArray[12]) + ByteToHex(myArray[13])
+ ByteToHex(myArray[14]) + ByteToHex(myArray[15]) +
ByteToHex(myArray[16]);
if TDNB <> Value then
begin
TDNB := Value;
Box := Data_serial(Value);
ListAjout := Form1.ListView1.Items.Add;
ListAjout.Data['Text1'] := Box.Serial;
ListAjout.Data['Text2'] := 'Type';
ListAjout.Data['Text3'] := 'Etat';
ListAjout.Data['Text4'] := 'Compagnie';
ListAjout.Data['Text5'] := 'Version';
ListAjout.Data['Text6'] := 'Remplissage';
ListAjout.Data['Text7'] := 'Batterie';
ListAjout.Data['Text8'] := Box.Box_type;
ListAjout.Data['Text9'] := Box.Etat;
ListAjout.Data['Text10'] := Box.Company;
ListAjout.Data['Text11'] := 'HW=' + Box.Hard + '/FW=' + Box.Firm;
ListAjout.Data['Text12'] := Box.Remplissage;
ListAjout.Data['Text13'] := Box.Batterie;
ListAjout.Data['TextButton14'] := inttostr(random(100));
ListAjout.Data['Text15'] := Value;
FMyDevice := BluetoothLE1.DiscoveredDevices[DiscoveredDeviceIndex];
born := DiscoveredDeviceIndex;
end;
end;
end;
end;
procedure TForm1.BluetoothLE1EndDiscoverDevices(const Sender: TObject;
const ADeviceList: TBluetoothLEDeviceList);
begin
if Scanning then
ProgressBar1.Value := ProgressBar1.Max;
Timer1.Enabled := False;
Scanning := False;
end;
procedure TForm1.BluetoothLE1ServicesDiscovered(const Sender: TObject;
const AServiceList: TBluetoothGattServiceList);
var
i, C: Integer;
begin
if AServiceList.Count > 0 then
begin
for i := 0 to AServiceList.Count - 1 do
begin
FService := AServiceList[i];
FService.Characteristics;
// showmessage('1');
if Ftest3 = nil then
begin
Ftest3 := FService.GetCharacteristic(CHARACTERISTIC_ID);
// showmessage('2');
if (Ftest3 <> NIL) then
begin
// showmessage('3');
// BluetoothLE1.SubscribeToCharacteristic(FMyDevice, Ftest3);
// BluetoothLE1.WriteCharacteristic(FMyDevice,Ftest3);
Form1.Button2.Text := 'good';
end;
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TDNB := '';
Form1.ListView1.Items.Clear;
StopBLEDiscovery;
//BluetoothLE1.DiscoverDevices(10000);
//BluetoothLE1.DiscoverDevices(10000, [HRSERVICE]);
PermissionsService.RequestPermissions([FLocationPermission],
RequestPermissionsResult, DisplayRationale);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
StopBLEDiscovery;
FMyDevice.Connect;
TThread.CreateAnonymousThread(
procedure
begin
if not BluetoothLE1.DiscoveredDevices[born].DiscoverServices then
TThread.Synchronize(nil,
procedure
begin
end);
end).Start;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Ftest3.setvalue(0);
// FMyDevice.WriteCharacteristic(Ftest3);
Form1.BluetoothLE1.WriteCharacteristic(FMyDevice, Ftest3);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{$IFDEF ANDROID}
FLocationPermission := JStringToString
(TJManifest_permission.JavaClass.ACCESS_COARSE_LOCATION);
{$ENDIF}
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Scanning := False;
end;
procedure TForm1.ListView1ButtonClick(const Sender: TObject;
const AItem: TListItem; const AObject: TListItemSimpleControl);
var
Data: Tbytes;
Data2: array [0 .. 1] of byte;
AChar: TBluetoothGattCharacteristic;
begin
SetLength(Data, 13);
Data[0] := $1;
Data[1] := $0;
Data[2] := $0;
Data2[0] := 0;
{$IFDEF ANDROID}
Ftest3.setvalue(Data);
BluetoothLE1.WriteCharacteristic(FMyDevice, Ftest3);
{$ENDIF}
{$IFDEF IOS}
// IOS specific code here
{$ENDIF}
{$IFDEF MACOS}
Ftest3.setvalue(Data);
BluetoothLE1.WriteCharacteristic(FMyDevice, Ftest3);
// OS X specific code here
{$ENDIF}
{$IFDEF WIN32}
// AChar := Ftest3.GetService.characteristics[2];
// AChar.SetValueAsInt64(Int64(0));
// FMyDevice.WriteCharacteristic(AChar);
Ftest3.setvalue(Data);
// BluetoothLE1.SubscribeToCharacteristic(FMyDevice, Ftest3);
BluetoothLE1.DiscoveredDevices.Items[0].Connect;
// BluetoothLE1.OnConnect(sender);
BluetoothLE1.WriteCharacteristic(FMyDevice, Ftest3);
{$ENDIF}
{$IFDEF WIN64}
Ftest3.setvalue(Data);
BluetoothLE1.WriteCharacteristic(FMyDevice, Ftest3);
{$ENDIF}
// FMyDevice.WriteCharacteristic(Ftest3);
// form1.ListView1.Items[Aitem.Index].Data['Text15'];
// showmessage(tlistitemtextbutton(AObject).Text);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
LElapsed: Cardinal;
begin
LElapsed := TThread.GetTickCount - ScanningStart;
ProgressBar1.Value := ProgressBar1.Max * (LElapsed.ToSingle / ScanningTime);
end;
end. |
Partager