Home > Back-end >  After BLE found bluetooth devices can be connected
After BLE found bluetooth devices can be connected

Time:10-31

 
Procedure TForm1. BtnFindClick (Sender: TObject);
The begin
If btnfind. Caption='search' then
The begin
BluetoothLE1. Enabled:=true;
BluetoothLE1. DiscoverDevices (10000);
Btnfind. Caption:='stop';
End the else
The begin
Bluetoothle1. Enabled:=false;
Btnfind. Caption:='search';
end;
end;

Procedure TForm1. BitBtn1Click (Sender: TObject);
Var
It: TBluetoothLEDevice;
Itt: TBluetoothGattServer;
I, j, n: integer;
The begin
Listbox2. The Clear;
//positioning listbox1 selected device index
J:=1;
For I:=0 to listbox1. Count the do
The begin
If listbox1. Selected [I] then
The begin
J:=I;
break;
end;
end;
If j & lt; 0 then
The begin
Tag:=1;
The exit;
end;

//stop found
BluetoothLE1. CancelDiscovery;

//get the device object element
It:=LST. Items [strtoint (leftStr (listbox1. Items [j], 2)) - 1);
//showmessage (inttostr (it) Services) Count));//show zero service
//connected devices
If it. Connect then
Showmessage (' connect ok ')
The else
Showmessage (' connnect fail ");

end;

//BLE found event
Procedure TForm1. BluetoothLE1DiscoverLEDevice (const Sender: TObject;
Const ADevice: TBluetoothLEDevice; Rssi: Integer;
Const ScanResponse: TScanResponse);
Var
N, I: integer;
It: TBluetoothLEDevice;
Flag: a Boolean;
The begin
//to find whether the address repeat
Flag:=true;
For I:=0 to LST. Do the Count - 1
The begin
It:=LST. Items [I];
If it. The Address=aDevice. Address then
The begin
Flag:=false;
break;
end;
end;

//no repeat equipment join the LST and listbox1
If flag then
The begin
I:=listbox1. Count + 1;
Listbox1. Items. The Add (format (' %. 02 d ', [I]) + ', '+ ADevice. DeviceName +' [] '+ ADevice. Address +' ');
LST. Add (Adevice);
end;
End


Tried many times, can search to the BLE devices, but call the connect immediately return failure, do not call the connect directly access service will quote without matching errors.
Operating system, WIN10 adapter CSR4.0,
  • Related