Author Topic: Async operation and SPP comunication  (Read 34373 times)

polipo

  • Guest
Async operation and SPP comunication
« on: March 15, 2016, 06:49:28 PM »
Dear,
i'm testing your library for connect to a BT device trought SPP for change the actual bt library "32feet in the hand"

Some issue:

1)the wcl works in async mode raising events.  It would be possible to work in sync mode ?
2)I can found the serial ports of a particolar (from bt name) associated bt device ? and understand which is in and out ?
3)the wcl offer a stream for read and write with serial port ?


Thanks





Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Async operation and SPP comunication
« Reply #1 on: March 15, 2016, 06:52:36 PM »
Hello,

1. Take a look on wclSyncClient. It provides synchronous methods to read data from SPP connection.
2. You do not need any serial ports when working with Bluetooth. Take a look on wclClient (or wclSyncClient) demos to find out how to connect to Bluetooth enabled device.
3. No, WCL works only with memory (pointer to bytes).

polipo

  • Guest
Re: Async operation and SPP comunication
« Reply #2 on: March 16, 2016, 03:41:36 PM »
thanks for your answer
it's possible discover in async method when

wclBluetoothDiscovery wclBluetoothDiscovery = new wcl.wclBluetoothDiscovery();
wclBluetoothDiscovery.OnDiscoveryComplete += new wcl.wclBluetoothDiscoveryCompleteEventHandler(wclBluetoothDiscovery_OnDiscoveryComplete);
int r = wclBluetoothDiscovery.Discovery(radio, 0, true);

if r == 0
-> retrieve from a wcl.wclBluetoothDiscoveryCompleteEventArgs ? or when launch discovery wait until it returns?

Thanks

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Async operation and SPP comunication
« Reply #3 on: March 16, 2016, 03:50:39 PM »
Hello,

Unfortunately no, discovering is always async.

polipo

  • Guest
Re: Async operation and SPP comunication
« Reply #4 on: March 17, 2016, 11:54:25 AM »
For business library the discovery will be sync..
i suggest to make another sync method for enumerate the paired devices without discovery

Thanks

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Async operation and SPP comunication
« Reply #5 on: March 17, 2016, 12:08:18 PM »
Its not a prolem to synchronize it in your application code. Just use any flag and DoMessage().

polipo

  • Guest
Re: Async operation and SPP comunication
« Reply #6 on: March 17, 2016, 12:27:29 PM »
Excuse, but i dont understand can you provide an example ?
Do a message ?
In my dll project (the wcl is referenced) this dll expose some method for comunicate with
an our BT device it expose SPP port:

-> Enumerate bt device
-> Connect
-> etc..
The problem is the discover async..


Another question :
From the "SerialClientDemo" when press Enumerate it returns "serial devices" (wclSerialDevice class)
Is possible to get the real name of the bluetooth device owned the serial port?
Is possible understand if the port is in or out ?

thanks a lot
Andrea F.


« Last Edit: March 17, 2016, 12:31:57 PM by polipo »

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Async operation and SPP comunication
« Reply #7 on: March 17, 2016, 12:41:22 PM »
Hello,

Unfortunately discovering is ansync and we have no plans to change it. As i told you can synchronize it internally in your code by lot of methods. One of them is to use threads. The other is to use DoMessage() and wait for discovering complete.

No, you can not do it. Also you do not need any COM ports when work with Bluetooth. Simple use wclClient to connect to a remote device with Transport property set to Bluetooth (refer to BluetoothConnection demo). Or use wclServer to receive incomming connections (refer to Bluechat server demo).

Bluetooth does not use any COM ports in its communication. The vCOM in windows is just for legacy application support and you must not use it when work with Bluetooth.

polipo

  • Guest
Re: Async operation and SPP comunication
« Reply #8 on: March 17, 2016, 01:45:51 PM »
Dear,
i have try your suggestion like this, BUT the event OnDiscovery Complete wasnt raised, why ?!?
(see my below code...)
i also try to start another thread and do it the discover but nothing the "discovery complete event" is not raised..



private void btnDiscover_Click(object sender, RoutedEventArgs e)
        {
            wcl.wclBluetoothRadio radio =  GetSelectedRadio();

            EventWaitHandle waitHandleFilter = new EventWaitHandle(false, EventResetMode.AutoReset);
            var discovery = new wcl.wclBluetoothDiscovery();
            discovery.OnDiscoveryComplete += new wcl.wclBluetoothDiscoveryCompleteEventHandler((s, ea) =>
            {
                waitHandleFilter.Set(); //*1
            }
            );

            discovery.Discovery(radio, 0, true);

            waitHandleFilter.WaitOne(); //IT WAIT INFINITY..... AND THE *1 IS NOT CATCH
}

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Async operation and SPP comunication
« Reply #9 on: March 17, 2016, 02:04:36 PM »
WCL requires messaging processing. You have to have message loop (as I told use DoEvents/DoMessage routine).

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Async operation and SPP comunication
« Reply #10 on: March 17, 2016, 02:48:42 PM »
Here are 2 demos show how to

1. wclindll - use WCL in DLL
2. BlockingDiscovery - how to execute discovering in blocking (sync) mode.

The demos are C++ but they may give you the idea. The same way can be used with wcl for .net

polipo

  • Guest
Re: Async operation and SPP comunication
« Reply #11 on: March 17, 2016, 04:55:42 PM »
Dear,
thanks for your support! i have opened the c++ but i must study them (my knoledge of c++ is poor..).

I found in c# two ways :

1) Application.DoEvenst() exists only in Windows Form, in WPF there isn't, but i found this and it works:

   [System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.Demand, Flags = System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
        public void DoEvents()
        {
            DispatcherFrame frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
                new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
        }

        public object ExitFrame(object f)
        {
            ((DispatcherFrame)f).Continue = false;

            return null;
        }

2) I have notice when i use lambda expression to assign OnDiscoveryComplete it is called when i Unload the wclApi with these approach i can:
(is not elegant but it works...)

 private wclBluetoothDevices EnumerateSync(bool discover = false)
        {
            wcl.wclAPI api = new wclAPI();
            api.Load();

            wclBluetoothDevices blueToothDevices = new wclBluetoothDevices();
            wcl.wclBluetoothRadio radio = GetSelectedRadio();
            bool stop = false;
            var discovery = new wcl.wclBluetoothDiscovery();
            discovery.OnDiscoveryComplete += new wcl.wclBluetoothDiscoveryCompleteEventHandler((s, ea) =>
            {
                for (uint y = 0; y < ((wclBluetoothDiscoveryCompleteEventArgs)ea).Devices.Count; y++)
                {
                    blueToothDevices.Add(((wclBluetoothDiscoveryCompleteEventArgs)ea).Devices[y]);
                }
                stop = true;
            }
            );

            if (discover == true)
                discovery.Discovery(radio, 5, false); //search in range
            else
                discovery.Discovery(radio, 0, true); //only pair

            int x = 0;
            int waitSecondsLimit = 0;
            if (discover == true)
                waitSecondsLimit = 50; //search in range for 5sec
            else
                waitSecondsLimit = 10; //only pair for 1 sec

            while (stop == false)
            {
                api.Unload();
                api.Dispose();
                api = null;
                Thread.Sleep(100);
                if (x++ >= waitSecondsLimit)
                {
                    //terminate..
                    break;
                }
            }

            return blueToothDevices;
        }


polipo

  • Guest
Re: Async operation and SPP comunication
« Reply #12 on: March 17, 2016, 05:50:09 PM »
Sorry this is the final version :

PS: in wclindll on
int CBtDiscovery::Discovery(LPBT_DEVICES* ppDevices)
you create a new thread...mhh i dont like it...



 private wclBluetoothDevices EnumerateSync(bool discover = false)
        {
            wcl.wclAPI api = new wclAPI();
            api.Load();

            EventWaitHandle waitHandleFilter = new EventWaitHandle(false, EventResetMode.AutoReset);
            wclBluetoothDevices blueToothDevices = new wclBluetoothDevices();
            wcl.wclBluetoothRadio radio = GetSelectedRadio();
            var discovery = new wcl.wclBluetoothDiscovery();
            discovery.OnDiscoveryComplete += new wcl.wclBluetoothDiscoveryCompleteEventHandler((s, ea) =>
            {
                for (uint y = 0; y < ((wclBluetoothDiscoveryCompleteEventArgs)ea).Devices.Count; y++)
                {
                    blueToothDevices.Add(((wclBluetoothDiscoveryCompleteEventArgs)ea).Devices[y]);
                }
                waitHandleFilter.Set();
            }
            );

            if (discover == true)
                discovery.Discovery(radio, 5, false); //search in range
            else
                discovery.Discovery(radio, 0, true); //only pair

            api.Unload();
            api.Dispose();
            api = null;

            waitHandleFilter.WaitOne(5000); //max 5 sec

            return blueToothDevices;
        }

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Async operation and SPP comunication
« Reply #13 on: March 17, 2016, 08:47:25 PM »
Your code looks very good and should do what you need.

 

Sitemap 1 2 3 4 5 6 7