Author Topic: Receiving files  (Read 2057 times)

Offline Hussain

  • Moderator
  • Newbie
  • *****
  • Posts: 43
  • Karma: 0
Receiving files
« on: April 17, 2007, 04:03:52 PM »
The BFObjectPushServer Object

Now that we know how to send files, let?s try the next step: How to receive files sent over Bluetooth.

There are a number of considerations here. The prime one has to do with security; do you want everyone to send you files, or only those devices which are paired? For starters, let?s just accept files from everyone without further checks.

There is a minor thing to note here: Your computer?s Bluetooth radio may not be visible to senders. Once a set of devices are paired, they don?t need to be visible ? or using Bluetooth nomenclature: Discoverable ? but to start off you need to change your radio to be discoverable to devices. This is a simple matter of setting its ?Discoverable? property to ?True?.

The BFObjectPushServerX object has a ?FilesDir? property that you need to set to a valid folder on your computer. Once that is done just call its ?Open? method. This allows all devices to send files to the specified folder on your computer until the ?Close? method is called. Your program will get these events:
  • OnConnect: When a device connects. You can use the RemoteAddress and RemoteName only from this event onwards.
  • OnProgress: While a file is being received. You can set the ?Abort? property to cancel the transfer. This event is fired multiple times.
  • OnObject: When the file finishes transferring
  • OnDisconnect: When the remote device disconnects

Sample Code: Receiving Files
Code: [Select]
'Placed in your menu or button handler
BFObjectPushServerX1.FilesDir = ?C:\Temp\?
BFObjectPushServerX1.BluetoothTransport.Radio = Radios.Radio(0)
BFObjectPushServerX1.Open

Private Sub BFObjectPushServerX1_OnConnect()
   Debug.Print "Connected to ? & BFObjectPushServerX1.RemoteName
End Sub

Private Sub BFObjectPushServerX1_OnDisconnect()
    Debug.Print ?Disconnect " & BFObjectPushServerX1.RemoteName
End Sub

Private Sub BFObjectPushServerX1_OnObject(ByVal AName As String)
    Debug.Print "OBEX Server has received " & AName
End Sub

Private Sub BFObjectPushServerX1_OnProgress(ByVal AName As String, ByVal APosition As Long, ByVal ASize As Long, AAbort As Boolean)
    Dim strFile As String
   
    strFile = Left(AName,len(AName)-1)
    Debug.Print "File: " & strFile & " Position: " & APosition & " Size: " & ASize
End Sub

Don?t forget to call the ?Close? method.

As mentioned, the OnProgress event is where you use the RemoteAddress and the name of the file to decide if you want to abort receiving the file. Alternatively, you can show the user a ?Cancel? button to allow this manually.

Multiple connections

Here are a few words about how to organize multiple connection to your server. In fact, that BFObjectPushServer can accept only one incomming connection. To make it possible to accept more then one incomming connection you must create several instances of the BFObjectPushServer component.

Also, you must assign different BluetoothTransport.Port numbers for each instances and then you can Open it all. All other stuff (manage which server should accept first connection and so on) will be done by low-level API.
« Last Edit: April 17, 2007, 05:11:26 PM by Mike Petrichenko »