locked
Bluetooth File Transfer (Working OBEX Solution Posted ;-) RRS feed

  • Question

  • Are there any good samples or blogs available that show how to transfer files via Bluetooth on NETCF from one phone to the next programmatically ?

    I’ve searched the forums but all I can find is many suggestions and some desktop samples but no mobile samples that simply transfer a file from one phone to the next.

    Your help would really be appreciated ;-)

    thanks

    Wednesday, August 23, 2006 6:31 PM

Answers

  • First of all thanks Brendan for your response..
    Though sadly that’s the sort of answer you find on Google and spend hour trying to figure out how to use..

     

    Since I finally got it working, here’s the detailed answer I was hoping for, hope it helps others..

    1) Download Brecham OBEX library Beta 1 from this url: http://32feet.net/files/18/objectexchange/entry1518.aspx

    2) Extract the file then navigate to “\Brecham+Obex+Beta+1\Brecham Obex\NETCF\Samples”.

    3) Rename the Samples folder to “BT” and copy it to your WM5 Smartphone or PocketPC device (I tested it on both)..
    - For this demo I pasted it in the root directory: “\BT”
    - Make sure “PutCmdline.exe” and all the other exe’s and dll’s  from the sample directory are in their..

    4) Add the process launcher code below class file to your project ‘ApplicationLauncher.cs’..

    using System;

    using System.Collections.Generic;

    using System.Text;

    //** Custom Imports **

    using System.Runtime.InteropServices;

     

     

    /************* Start-Namespace ************/

    namespace Launcher {

        /************* Start-Class ************/

        class ApplicationLauncher {

            /**********************************************************************    

                * Vars

                **********************************************************************/

            ProcessInfo incProcessInfo;

            String progPath = "";

     

            /**********************************************************************

            * METHOD: LaunchApplication

            **********************************************************************/

            public void LaunchApplication(string progPath, String CmdLine) {

                /********************************************

                    * Vars..

                    ********************************************/

                this.progPath = progPath;

                try {

                    /********************************************

                        * Kill the process if it already exists..

                        ********************************************/

                    KillApplication();

     

                    /********************************************

                    * Create new process..

                    ********************************************/

                    incProcessInfo = new ProcessInfo();

                    if(CreateProcess(progPath, CmdLine, incProcessInfo)) {

                        //MessageBox.Show("Success! PID = " + pi.ProcessId.ToString());

                    } else {

                       // MessageBox.Show("Failed! System Error = " + GetLastError().ToString());

                    }//MessageBox.Show("Done Waiting");

                } catch(Exception) {}

            }/************* End-Method ************/

     

     

     

            /**********************************************************************

            * METHOD: KillApplication

            **********************************************************************/

            public void KillApplication() {

                /********************************************

                * Kill the process if it already exists..

                ********************************************/

                if(incProcessInfo != null) { TerminateProcess(incProcessInfo.hProcess, 1); }

            }/************* End-Method ************/

                     

     

     

            /**********************************************************************

            * Constructor: App Launcher DLL's

            **********************************************************************/

            [DllImport("CoreDll.DLL", SetLastError = true)]

            private extern static

                int CreateProcess(String imageName,

                String cmdLine,

                IntPtr lpProcessAttributes,

                IntPtr lpThreadAttributes,

                Int32 boolInheritHandles,

                Int32 dwCreationFlags,

                IntPtr lpEnvironment,

                IntPtr lpszCurrentDir,

                byte[] si,

                ProcessInfo pi);

     

            [DllImport("CoreDll.dll")]

            private extern static

                Int32 GetLastError();

     

            [DllImport("CoreDll.dll")]

            private extern static

                Int32 WaitForSingleObject(IntPtr Handle,

                Int32 Wait);

     

            [DllImport("coredll", EntryPoint = "TerminateProcess", SetLastError = true)]

            extern static int TerminateProcess(IntPtr hProcess, int uExitCode);

     

     

            /**********************************************************************

            * Constructor: ProcessInfo

            **********************************************************************/

            public class ProcessInfo {

                public IntPtr hProcess;

                public IntPtr hThread;

                public Int32 ProcessId;

                public Int32 ThreadId;

            }/************* End-Method ************/

     

     

            /**********************************************************************

            * METHOD: CreateProcess

            **********************************************************************/

            public static bool CreateProcess(String ExeName, String CmdLine, ProcessInfo pi) {

                //Int32 INFINITE;

                //unchecked { INFINITE = (int) 0xFFFFFFFF; }

                if(pi == null)

                    pi = new ProcessInfo();

                byte[] si = new byte[128];

                CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero,

                    0, 0, IntPtr.Zero, IntPtr.Zero, si, pi);

                //WaitForSingleObject(pi.hProcess, INFINITE);

                return true;

            }/************* End-Method ************/

        }/************* End-Class ************/

    }/************* End-Namespace *****

     

    5) Finally use the application launcher to call the ‘PutCmdline.exe’ and send your file..

    /**********************************************************************

    * METHOD: CreateProcess

    **********************************************************************/

    public void sendFile() {

          

            /********************************************

            * Send file..

            * "Command line syntax: [-bluetooth <deviceName>] filename [filename ...]"

            ********************************************/

            String curDeviceName = "Bob";

            string File2Send_01 = "\\SendMeNow.jpg";

            string File2Send_02 = "\\SendMeLate.jpg";

     

      Cursor.Current = Cursors.WaitCursor;

            String progPath = @"\BT\PutCmdline.exe";

            String CmdLine = "-bluetooth " + curDeviceName + " \"" + File2Send_01 + "\"" + " \"" + File2Send_02 + "\"";

            private ApplicationLauncher incApplicationLauncher = new ApplicationLauncher();

            incApplicationLauncher.LaunchApplication(progPath, CmdLine);

            Cursor.Current = Cursors.Default;

    }

     

     

    6) Enjoy ;-)

     

     

    Thursday, August 24, 2006 12:11 PM

All replies

  • I'd take a look at the personal area networking library available at 32feet.com which includes Bluetooth support.
    Wednesday, August 23, 2006 7:05 PM
  • First of all thanks Brendan for your response..
    Though sadly that’s the sort of answer you find on Google and spend hour trying to figure out how to use..

     

    Since I finally got it working, here’s the detailed answer I was hoping for, hope it helps others..

    1) Download Brecham OBEX library Beta 1 from this url: http://32feet.net/files/18/objectexchange/entry1518.aspx

    2) Extract the file then navigate to “\Brecham+Obex+Beta+1\Brecham Obex\NETCF\Samples”.

    3) Rename the Samples folder to “BT” and copy it to your WM5 Smartphone or PocketPC device (I tested it on both)..
    - For this demo I pasted it in the root directory: “\BT”
    - Make sure “PutCmdline.exe” and all the other exe’s and dll’s  from the sample directory are in their..

    4) Add the process launcher code below class file to your project ‘ApplicationLauncher.cs’..

    using System;

    using System.Collections.Generic;

    using System.Text;

    //** Custom Imports **

    using System.Runtime.InteropServices;

     

     

    /************* Start-Namespace ************/

    namespace Launcher {

        /************* Start-Class ************/

        class ApplicationLauncher {

            /**********************************************************************    

                * Vars

                **********************************************************************/

            ProcessInfo incProcessInfo;

            String progPath = "";

     

            /**********************************************************************

            * METHOD: LaunchApplication

            **********************************************************************/

            public void LaunchApplication(string progPath, String CmdLine) {

                /********************************************

                    * Vars..

                    ********************************************/

                this.progPath = progPath;

                try {

                    /********************************************

                        * Kill the process if it already exists..

                        ********************************************/

                    KillApplication();

     

                    /********************************************

                    * Create new process..

                    ********************************************/

                    incProcessInfo = new ProcessInfo();

                    if(CreateProcess(progPath, CmdLine, incProcessInfo)) {

                        //MessageBox.Show("Success! PID = " + pi.ProcessId.ToString());

                    } else {

                       // MessageBox.Show("Failed! System Error = " + GetLastError().ToString());

                    }//MessageBox.Show("Done Waiting");

                } catch(Exception) {}

            }/************* End-Method ************/

     

     

     

            /**********************************************************************

            * METHOD: KillApplication

            **********************************************************************/

            public void KillApplication() {

                /********************************************

                * Kill the process if it already exists..

                ********************************************/

                if(incProcessInfo != null) { TerminateProcess(incProcessInfo.hProcess, 1); }

            }/************* End-Method ************/

                     

     

     

            /**********************************************************************

            * Constructor: App Launcher DLL's

            **********************************************************************/

            [DllImport("CoreDll.DLL", SetLastError = true)]

            private extern static

                int CreateProcess(String imageName,

                String cmdLine,

                IntPtr lpProcessAttributes,

                IntPtr lpThreadAttributes,

                Int32 boolInheritHandles,

                Int32 dwCreationFlags,

                IntPtr lpEnvironment,

                IntPtr lpszCurrentDir,

                byte[] si,

                ProcessInfo pi);

     

            [DllImport("CoreDll.dll")]

            private extern static

                Int32 GetLastError();

     

            [DllImport("CoreDll.dll")]

            private extern static

                Int32 WaitForSingleObject(IntPtr Handle,

                Int32 Wait);

     

            [DllImport("coredll", EntryPoint = "TerminateProcess", SetLastError = true)]

            extern static int TerminateProcess(IntPtr hProcess, int uExitCode);

     

     

            /**********************************************************************

            * Constructor: ProcessInfo

            **********************************************************************/

            public class ProcessInfo {

                public IntPtr hProcess;

                public IntPtr hThread;

                public Int32 ProcessId;

                public Int32 ThreadId;

            }/************* End-Method ************/

     

     

            /**********************************************************************

            * METHOD: CreateProcess

            **********************************************************************/

            public static bool CreateProcess(String ExeName, String CmdLine, ProcessInfo pi) {

                //Int32 INFINITE;

                //unchecked { INFINITE = (int) 0xFFFFFFFF; }

                if(pi == null)

                    pi = new ProcessInfo();

                byte[] si = new byte[128];

                CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero,

                    0, 0, IntPtr.Zero, IntPtr.Zero, si, pi);

                //WaitForSingleObject(pi.hProcess, INFINITE);

                return true;

            }/************* End-Method ************/

        }/************* End-Class ************/

    }/************* End-Namespace *****

     

    5) Finally use the application launcher to call the ‘PutCmdline.exe’ and send your file..

    /**********************************************************************

    * METHOD: CreateProcess

    **********************************************************************/

    public void sendFile() {

          

            /********************************************

            * Send file..

            * "Command line syntax: [-bluetooth <deviceName>] filename [filename ...]"

            ********************************************/

            String curDeviceName = "Bob";

            string File2Send_01 = "\\SendMeNow.jpg";

            string File2Send_02 = "\\SendMeLate.jpg";

     

      Cursor.Current = Cursors.WaitCursor;

            String progPath = @"\BT\PutCmdline.exe";

            String CmdLine = "-bluetooth " + curDeviceName + " \"" + File2Send_01 + "\"" + " \"" + File2Send_02 + "\"";

            private ApplicationLauncher incApplicationLauncher = new ApplicationLauncher();

            incApplicationLauncher.LaunchApplication(progPath, CmdLine);

            Cursor.Current = Cursors.Default;

    }

     

     

    6) Enjoy ;-)

     

     

    Thursday, August 24, 2006 12:11 PM
  • Now that sending OBEX files works nicely thanks largely to “Brecham OBEX library”..

     

    On to stage 2: Does anyone know how to programaticaly handle the receiving of OBEX files so that I can store incoming file in different folders?

     

    thanks..

    Thursday, August 24, 2006 12:15 PM