This project has moved. For the latest updates, please go here.

How to make a blueetooth server properly

Topics: Bluetooth - Microsoft
Aug 16, 2012 at 6:14 PM

Hello,

I stuck and noob in bluetooth technologies.

what i try to made is a bluetooth server that broadcast itself and can be discover by others devices. Once, the client device found the bluetooth server, it make a connection to the server.

I follow the tutorial http://32feet.codeplex.com/wikipage?title=Bluetooth%20Server-side&referringTitle=Documentation

But, when i set my blackberry in "search" mode, in bluetooth option, it doesn't detect the server.

I dont know what is wrong in my piece of code and i seriously need help ......

I use the default bluetooth stack in windows 7 x64.



public
class Server { readonly Guid OurServiceClassId = new Guid("{29913A2D-EB93-40cf-BBB8-DEEE26452197}"); readonly string OurServiceName = "32feet.NET Chat2"; public BluetoothListener Bluetoothlistener = null; public delegate void InfoDelegate(object obj, string pstr); public event InfoDelegate DataAvailable; private NetworkStream Ns = null; public void Listen() { try { new BluetoothClient(); } catch (Exception ex) { var msg = "Bluetooth init failed: " + ex; MessageBox.Show(msg); throw new InvalidOperationException(msg, ex); } Bluetoothlistener = new BluetoothListener(OurServiceClassId); Bluetoothlistener.ServiceName = OurServiceName; Bluetoothlistener.Start(); Bluetoothclient = Bluetoothlistener.AcceptBluetoothClient(); byte[] data = new byte[1024]; Ns = Bluetoothclient.GetStream(); Ns.BeginRead(data, 0, data.Length, new AsyncCallback(ReadCallBack), data); DataAvailable(this, "Begin to read"); } private void ReadCallBack(IAsyncResult ar) { try { if (ar != null && ar.IsCompleted) { byte [] data = (byte[])ar.AsyncState; DataAvailable(this, System.Text.UTF8Encoding.ASCII.GetString(data)); if (Bluetoothclient != null) { if (Ns != null) { Ns.Flush(); Ns.BeginRead(data, 0, data.Length, new AsyncCallback(ReadCallBack), data); } } } } catch (Exception ex) { throw ex; } } }

Developer
Aug 19, 2012 at 8:49 PM

You probably want to set

BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;

Creating a server allows a device that connects to find the server. It does not "broadcast" anything.

Aug 22, 2012 at 10:42 AM

Perfect & Thank you !!!!

Aug 23, 2012 at 10:36 AM

Hello,

by your recommendation, i got that piece of code for my listen method now.

 

public void Listen()
{
     try { new BluetoothClient(); }
     catch (Exception ex)
     {
         var msg = "Bluetooth init failed: " + ex;
         throw new InvalidOperationException(msg, ex);
     }
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
 Bluetoothlistener = new BluetoothListener(OurServiceClassId); Bluetoothlistener.ServiceName = OurServiceName; Bluetoothlistener.Start(); Bluetoothclient = Bluetoothlistener.AcceptBluetoothClient(); byte[] data = new byte[1024]; Ns = Bluetoothclient.GetStream(); Ns.BeginRead(data, 0, data.Length, new AsyncCallback(ReadCallBack), data); DataAvailable(this, "Begin to read"); }

Right, i can detect the bluetooth server on my blackberry but i'm faced to a new problem, i can not connect to the server.

i got the message "connection failed" on the blacbkerry.

When, i put i break point, and do a debug step by step, i stuck at the line.

Bluetoothclient = Bluetoothlistener.AcceptBluetoothClient();

Can you help please ?

 

 

 

Developer
Aug 24, 2012 at 10:03 PM

Well if the Blackberry cannot connect, then of course no connection will be accepted, as no connection has arrived. If no connection is accepted then Accept will block.  (There are non-blocking versions, but still if no connection has arrived...)

Are you writing a program for the Blackberry? Your code there says also:  someJavaClass.Connect(OurServiceClassId); or equivalent?

 

 

Aug 25, 2012 at 12:16 PM

Hello,

I dont have write my own program on the blackberry, i use the bluetooth connection tools provide by default and in general by every smartphone device with bluetooth connection.

What i try to do is to simulate a handsfree device, so i got that piece of code :

public void Listen()
        {
            try { new BluetoothClient(); }
            catch (Exception ex)
            {
                var msg = "Bluetooth init failed: " + ex;
                throw new InvalidOperationException(msg, ex);
            }
            BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
            Bluetoothlistener = new BluetoothListener(BluetoothService.Handsfree);
            Bluetoothlistener.Start();
            Bluetoothclient = Bluetoothlistener.AcceptBluetoothClient();
            DataAvailable(this, "Ready to read.");
            Stream str = Bluetoothclient.GetStream();
            StreamReader sr = new StreamReader(str);
            DataAvailable(this, sr.ReadLine());
        }

How can i contact you in private ? I can explain more in details the project.

Rgds,

Developer
Aug 29, 2012 at 1:45 PM

However if you are hoping to get to use the PC to use the audio from the phone then that's quite difficult. Windows only supplies a low-level kernel API for Bluetooth SCO which can be used for audio.

If you're just wanting to do some control of the device without the audio, that might be possible.  Firstly you need to have a read of the Handsfree spec as the Service Name is HandsfreeAudioGateway. Also see http://stackoverflow.com/questions/5960208/bluetooth-dial-with-32feet-net-and-c-sharp when an iPhone was successfully controlled.

You can email at yahoo.com and alanjmcf.

Oct 1, 2014 at 4:45 PM
Edited Oct 1, 2014 at 4:47 PM
Hi ryosaeba,
I know it might be a little late,
but after that much of trouble searching google and other forums, your so called "piece of code"
saved me at least another week of development and therefore the least I can do is to thank you.

I had a problem creating a bluetooth server on a WinCE device. I managed to pair the device successfully and the listening service was visible to the client Computer. But I failed to connect to that service.

I began to use native functions to create my own library and even began to question my registry settings.
Nothing I tried succeeded.

Finally I came across your thread and recognized a little difference between your implementation and mine.

Simply the fact that you call the BluetoothListener's constructor with the GUID as the only parameter solved all my problems.

To cut a long story short,
even though your problem wasn't related to mine, your posted code solved mine.

Thanks