software you can take with you

Sending and receiveing string using Spp Profile

Last post 10-04-2011 1:02 AM by ramesh.shivakumar. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 09-28-2011 2:53 AM

    Sending and receiveing string using Spp Profile

     Hello Viwer,

    My application is currently woking fine by using Obex(OPP)  but i want to do same using SPP profile.

    so what are the nessary changes i need to do in coding in windows xp PC using c#.

     

    Code C#

     public List<BluetoothDeviceInfo> DiscoverAllDevices()
            {

                List<BluetoothDeviceInfo> _devices = new List<BluetoothDeviceInfo>();

                if (BluetoothRadio.PrimaryRadio != null)
                {

                    BluetoothClient bc = new BluetoothClient();
               
                    try
                    {
                        Status  = "Discovering the Devices.....";

                        BluetoothDeviceInfo[ dev = bc.DiscoverDevices();

                        _devices.AddRange(dev);
                        Status = "Discovering the Devices Completed.....";
                    }
                    catch (Exception ex)
                    {
                        Status = ex.Message;
                    }


                }
                else
                {
                    Status = "The file could not be sent because the connection could not be established. Check to make sure the Bluetooth radio is plugged in.";
                }

                return _devices;
            } 

     

    void bgWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                if (OBEXOpenStream())
                {
                    BlueToothState = ConnectionState.Connecting;
                        BlueToothState = ConnectionState.Streaming;
                        ol = new ObexListener ( ObexTransport.Bluetooth );
                        ol.Start();
                        while (ol.IsListening)
                        {
                            try
                            {
                                ObexListenerContext obexListenerContext = ol.GetContext ( );
                                 
                                if(obexListenerContext != null)
                                {
                                    ObexListenerRequest obexListenerRequest = obexListenerContext.Request;
                                    InTheHand.Net.BluetoothEndPoint btEndPoint = (InTheHand.Net.BluetoothEndPoint) obexListenerRequest.RemoteEndPoint;
                                    BluetoothDeviceInfo l = new BluetoothDeviceInfo ( btEndPoint.Address );
                                    InTheHand.Net.BluetoothAddress theSenderAddress = btEndPoint.Address;
                                    string filename = System.Environment.GetFolderPath ( System.Environment.SpecialFolder.Personal ) + "\\" +                       obexListenerRequest.RawUrl.Substring ( 1, 12 );//Uri.UnescapeDataString(obexListenerRequest.RawUrl.TrimStart(new char[ { '/' }));
                                    obexListenerRequest.WriteFile ( filename );
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show ( ex.Message );
                            }
                        }
                   // }
                   // else
                   // {
                        //addtolog("Fail to get the File");
                   /// }
                    //OBEXCloseStream();
                }
            }
           
            private bool OBEXOpenStream()
            {
                 string BTMAC = _deviceInfo.DeviceAddress.ToString();
                 string BTName = _deviceInfo.DeviceName.ToString();
                 string DeviceParingPin = "0000";
                 BlueToothState = ConnectionState.Connecting;
                Status = "Paring with  Selected Device.... ";
                Guid spguid = BluetoothService.ObexObjectPush ;
                btaddress = BluetoothAddress.Parse(BTMAC);
                client = new BluetoothClient();
                //client.Authenticate = true;
               
                //BluetoothSecurity.RemoveDevice(btaddress);
                 
                 
                BlueToothState = ConnectionState.Pairing;
                if (BluetoothSecurity.PairRequest(btaddress, DeviceParingPin))
                {
                    if(OnDevicePaired != null)
                    {
                        OnDevicePaired ( );
                    }
                    Status = "Device Paired and reday to Receive the data  ";
                    endpoint = new BluetoothEndPoint(btaddress, spguid,1);
                     
                    try
                    {
                        client.Connect(endpoint);
                    }
                    catch (System.Exception e)
                    {
                        //unable to connect (server not listening on spguid)
                        Status = e.Message;
                        return false;
                    }

                    //connect socket
                    stream = client.GetStream();
                    return true;
                }
                else
                {
                    /// addtolog("Invalid Pin");
                    Status = "Pairing failed/Client device is not enabled BT";
                    BlueToothState = ConnectionState.Disconnected;
                    return false;
                }
            }

            private void OBEXCloseStream()
            {
               
                if(stream != null)
                    stream.Close();
                if(client != null)
                    client.Close();
            }
     

     Please Replay Me ASAP

    Thanks In advance

  • 09-29-2011 5:37 AM In reply to

    Re: Sending and receiveing string using Spp Profile

  • 09-29-2011 7:54 AM In reply to

    Re: Sending and receiveing string using Spp Profile

     Hello viwer,

    thanks for your replay. i tryied that code it is hanging at -- Bl.AcceptBluetoothClient ( );

    Please refer below code.

     

        Guid spguid = new Guid ( "{00112233-4455-6677-8899-aabbccddeeff}" );

    Bl = new BluetoothListener ( spguid );
          Bl.Start ( );

     conn = Bl.AcceptBluetoothClient ( );
                                stream =  conn.GetStream ( );

     

     

    I followed folling steps 

    when the pc appliocation wating data from external device ,

    when i am trying to send file from extrernal file .it shows bluetooth is busy.

      Thanks 

  • 09-30-2011 5:08 AM In reply to

    Re: Sending and receiveing string using Spp Profile

    If the remote device connects to the SPP then you'll want to use:

       Guid spguid = BluetoothService.SerialPort;

    (I've updated the example in the help to show that case).

  • 09-30-2011 5:54 AM In reply to

    Re: Sending and receiveing string using Spp Profile

     Hello viwer,

    Thnaks for your replay.i tried that option still its not  woking.

    is that nessary that spp has to their in Remote Device also?

  • 10-03-2011 11:27 AM In reply to

    Re: Sending and receiveing string using Spp Profile

    What is the remote device?  What does its documentation say it connects to?  Have you got it working with another PC set-up?
  • 10-04-2011 1:02 AM In reply to

    Re: Sending and receiveing string using Spp Profile

     Hello viwer ,

    thank you for your replay.Remote device is leica disto.

    i am trying to connect to leica  device from pc.after connectingleica will send data to PC.

    currently i dont have leica insted of i am using nokia express mobile  .

    in that  i dont know what pfofile is using ?

    so my question is if my Application in pc is woking in SPP means the remove device which is  sending data also be an SPP implemented?

     how can i send the string from PC to Remote device using SPP?

    Thank you.

Page 1 of 1 (7 items)
Copyright © 2001-2013 In The Hand Ltd. All rights reserved. Terms of Use and Privacy Policy. OrcsWeb's Windows Cloud Server Hosting