Author Topic: Delphi 2007 Example for Discovering, open a Bluetooth device and receiving data.  (Read 2595 times)

Offline GDavidShaw

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
The Demo program is great but I would like a simple example of how to connect to a Bluetooth device behind the scenes.  I know the Class Device name, its pairing number and the type of data it is sending.  I want to be able to connect using the VCL components on a form without the User every knowing I am doing it.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Its easy. You need 2 components on the form: TBFBluetoothDiscovery and TBFObjectPushClient. Lets say it named as BFBluetoothDiscovery and BFObjectPushClient.
The you must write code like this:

Code: [Select]
procedure SendSomthing;
var
  Devices:  TBFBluetoothDevices;
begin
  // Discovering devices
  Devices := BFBluetoothDiscovery.Discovery(nil, False, False);
  // Check is there is at leat one device
  if Assigned(Devices) then begin
    try
      if Devices.Count > 0 then begin
        // Get first device, for example, and send fil to it.
        ObjectPushClient.BluetoothTransport.Device := Devices[0];
        // By default OPP ues Bluetoth transport so we do not need change somthing else
        ObjectPushClient.Open;
        // Put may cause the exception. Using try/finally.
        try
          ObjectPushClient.Put('somefilename.ext');
        finally
          ObjectPushClient.Close;
        end;
      end;
    // The error can appears above so use try/finally block
    finally
      // Clean up
      Devices.Free;
    end;
  end;
end;
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline fgi

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Hi all!

I tried this code, but it fails with 'Invalid argument to date encode' on line "Devices := form1.BFBluetoothDiscovery.Discovery(nil, False, False);"
Can anybody help me?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
Good day!

Does that error appears under IDE (Delphi). If yes - it is OK In stand alone exe (running without IDE) you will not see the exception, because it is in try/except block.
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager

Offline fgi

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Thanks for quick answer.
How does it work backwards?
My problem: i send a file to a mobile (done). The mobile processes the file, and create a result-file (done). But how can i get the result-file?
I tried the code with this: "BFFileTransferClient1.Get('result.txt', ..... );", but didn't work...

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2467
  • Karma: 1000
    • Wireless Communication Library
You should use Server on PC side. Or write own protocol and use Write/Read methods to send data to/from device
WCL - Wireless Communication Library
TextBlue - Bluetooth Proximity Marketing Software
WCL Phone Explorer - Free Phone Content Manager