In Delphi, System.Bluetooth is a unit that provides support for Bluetooth communication in your Delphi programs. You can use System.Bluetooth to perform tasks such as scanning for nearby Bluetooth devices, connecting to Bluetooth devices, and sending and receiving data over Bluetooth.
To use System.Bluetooth in your Delphi program, you will need to include the unit in your program using the uses clause:
1 2 |
uses System.Bluetooth; |
Once you have included the System.Bluetooth unit in your program, you can use the classes and functions provided by the unit to access the Bluetooth functionality of your device.
Here is an example of how to use System.Bluetooth to scan for nearby Bluetooth 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 41 42 43 44 45 46 |
uses System.Bluetooth; var Manager: TBluetoothManager; Adapter: TBluetoothAdapter; Device: TBluetoothDevice; Socket: TBluetoothSocket; begin //Create a TBluetoothManager instance Manager := TBluetoothManager.Current; //Get the default Bluetooth adapter Adapter := Manager.CurrentAdapter; //Scan for nearby Bluetooth 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 Socket := Device.CreateClientSocket(TBluetoothService.SerialPort, False); try Socket.Connect; try //Send a message over the socket Socket.SendData(TEncoding.UTF8.GetBytes(‘Hello’)); finally Socket.Close; end; finally Socket.Free; end; Break; end; finally Adapter.CancelDiscovery; end; end; |
This example code performs the following tasks:
- Creates a TBluetoothManager instance to manage the Bluetooth functionality of the device.
- Gets the default Bluetooth 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 CreateClientSocket method.
- Sends a message over the socket using the SendData method, and then closes the socket.
For more information about using System.Bluetooth in Delphi, you can consult the Delphi documentation or search online for examples and tutorials.
Leave a Reply