Author Topic: Object disposed exception if I try to user a timer after OnDeviceFound event  (Read 17831 times)

cagiodelfo

  • Guest
Goodmornig,
I'm using wclBluetoothDiscovery monitoring service to find bluetooth devices near to my application; when a device is found event OnDeviceFound is correctly called, but if I want to know the name of device through the method device.GetName(), sometimes it returns a string empty.
I've found that i've to wait 3 or 5 seconds before ask the device name (http://forum.btframework.com/index.php/topic,3330.msg8505.html#msg8505), but if I use a thread, a timer (also the timer of Windows.Forms that work on UI Thread), or somethings similar, when I access device object program raises an "Object disposed exception", probably because bt framework, due to bt stack, is not completely thread-safe.
So, how can i wait for some seconds, without using a blocking UI-Thread while cycle?

Thanks in advance!

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Hello,

Yes, to read a device's name you have wait a bit. It is because of internal Bluetooth implementation (not WCL, but Bluetooth). Discovering returns only device's MAC. To read its name the BT chip sends other command. That is why it sometimes not available immediately.

With regards to your problem with Object Disposed message: all the objects passed into the event handlers are valid only inside the event handler. If you would like to use it outside the event handler (in thread or by timer in your case) you should create a local object's copy by creating new object's instance and calling Assignd method.

For example you got e.Device in your event handler. So create new object instance

wcl.wclBluetoothDevice MyDevice = new wcl.wclBluetoothDevice();

and call its assign method

MyDevice.Assign(e.Device);

Or (for BluetoothDevice) you can simple store its MAC and then use the MAC

String MAC = e.Device.Address;

then in your timer:

wcl.wclBluetoothDevice Device = new wcl.wclBluetoothDevice();
Device.Address = MAC;
// Do what you need
Device = null;

 

Sitemap 1 2 3 4 5 6 7