In Delphi, System.Beacon is a unit that provides support for beacon communication in your Delphi programs. You can use System.Beacon to perform tasks such as scanning for nearby beacon devices, connecting to beacon devices, and sending and receiving data over beacon.
To use System.Beacon in your Delphi program, you will need to include the unit in your program using the uses clause
1 2 |
uses System.Beacon; |
Once you have included the System.Beacon unit in your program, you can use the classes and functions provided by the unit to access the beacon functionality of your device.
Here is an example of how to use System.Beacon to scan for nearby beacon devices and connect to one of them:
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 |
uses System.Beacon; var Manager: TBeaconManager; Adapter: TBeaconAdapter; Device: TBeaconDevice; begin //Create a TBeaconManager instance Manager := TBeaconManager.Current; //Get the default beacon adapter Adapter := Manager.CurrentAdapter; //Scan for nearby beacon devices Adapter.StartDiscovery(5000); try Wait for the discovery process to complete while Adapter.Discovering do begin Sleep(100); end; //Iterate through the list of discovered devices for Device in Adapter.GetDiscoveredDevices do begin Connect to the first device in the list Device.Connect; try //Send a message to the device Device.SendData(TEncoding.UTF8.GetBytes(‘Hello’)); finally Device.Disconnect; end; Break; end; finally Adapter.CancelDiscovery; end; end; |
This example code performs the following tasks:
- Creates a TBeaconManager instance to manage the beacon functionality of the device.
- Gets the default beacon adapter using the CurrentAdapter property.
- Starts the discovery process using the StartDiscovery method, and waits for it to complete.
- Iterates through the list of discovered devices using the GetDiscoveredDevices method, and connects to the first device in the list using the Connect method.
- Sends a message to the device using the SendData method, and then disconnects from the device.
For more information about using System.Beacon in Delphi, you can consult the Delphi documentation or search online for examples and tutorials.
Leave a Reply