About the author
With the BroadcastReceiver plugin, it is easy to write a Bluetooth discovery application with Delphi.
Drop the BroadcastReceiver component onto the form, double click on the BroadcastReceiver, and put in the code to recognize the Bluetooth device.
Here's what the code looks like:
procedure
TForm6
.
BroadcastReceiver1Receive(AContext: JContext;
AIntent: JIntent);
var
LJAction: JString;
LDeviceName, LAddress:
string
;
LParcel: JParcelable;
LDevice: JBluetoothDevice;
begin
LJAction := AIntent
getAction;
if
LJAction
compareTo(TJBluetoothDevice
JavaClass
ACTION_FOUND)=
0
then
LParcel := AIntent
getParcelableExtra(TJBluetoothDevice
EXTRA_DEVICE);
LDevice := TJBluetoothDevice
Wrap((LParcel
as
ILocalObject).GetObjectID);
LDevice
getBondState <> TJBluetoothDevice
BOND_BONDED
LDeviceName := JStringToString(LDevice
getName);
LAddress := JStringToString(LDevice
getAddress);
Log
d(Format(
'Bluetooth device name: %s'
, [LDeviceName]));
'Bluetooth device addr: %s'
, [LAddress]));
end
else
compareTo(TJBluetoothAdapter
ACTION_DISCOVERY_FINISHED) =
d(
'Bluetooth discovery finished'
);
btnEnableDiscoveryClick(Sender: TObject);
EnsureBroadcastReceiverSet;
CallInUIThread(
BluetoothAdapter: JBluetoothAdapter;
LFilter: JIntentFilter;
LFilter := TJIntentFilter
init;
LFilter
addAction(TJBluetoothAdapter
ACTION_DISCOVERY_FINISHED);
addAction(TJBluetoothDevice
ACTION_FOUND);
SharedActivity
registerReceiver(FBroadcastReceiver, LFilter);
'registered ACTION_FOUND and DISCOVERY_FINISHED'
'Enabling discovery'
BluetoothAdapter := TJBluetoothAdapter
getDefaultAdapter;
BluetoothAdapter
isDiscovering
cancelDiscovery;
while
do
Sleep(
10
startDiscovery;
'Discovery enabled!'
And that's it!