Bug 15055 - CBCentralManager delegate never called
Summary: CBCentralManager delegate never called
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.0.0.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-09-27 20:06 UTC by dj_technohead
Modified: 2013-09-28 21:16 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description dj_technohead 2013-09-27 20:06:44 UTC
After creating an instance of CBCentralManager with a CBCentralManagerDelegate, the delegate's UpdatedState method is never called.

This doesnt work:
    cbCentralManager = new CBCentralManager(new CentralManager(), null);

nor did this:
	cbCentralManager = new CBCentralManager();
	cbCentralManager.Delegate = new CentralManager();

I created the same code in Objective C:

    manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

And saw that the delegate's centralManagerDidUpdateState method getting called.
Comment 1 dj_technohead 2013-09-27 23:12:55 UTC
Not entirely sure why, but it now appears to be working, the delegate method is being called.
Comment 2 dj_technohead 2013-09-28 01:47:57 UTC
Found what causes it to not be fired, if trying to connect the events right after creation:

this.centralManager = new CoreBluetoothCentralManager(uuid);
			//this.centralManager.CentralManager.DiscoveredPeripheral += handleDiscoveredPeripheral;

Uncommenting the line where the discoveredperipheral event is hooked up will cause the CBCentralManagerDelegate to not be called.
Comment 3 Sebastien Pouliot 2013-09-28 10:32:44 UTC
The .NET events that Xamarin.iOS provides are possible because an internal *Delegate type is used, see [1] for more details. That internal *Delegate type is called by ObjC and the code routes those call those the .NET events.

That also means you cannot mix those .NET events with your own custom *Delegate type. One (the later) will replace the other (the former) - because it's not possible to have two *Delegate set on the same instance.

So if you want to use events you must not assign a delegate, e.g.

>   var cb = new CBCentralManager ();
>   cb.DiscoveredPeripheral += handleDiscoveredPeripheral;

Otherwise you use your own *Delegate type and override the methods you need (or are required to implement).

>   var cb = new CBCentralManager (new MyCBCentrealManagerDelegate ());

[1] http://spouliot.wordpress.com/2012/03/26/events-vs-objective-c-delegates/
Comment 4 dj_technohead 2013-09-28 19:01:21 UTC
Ah ok, that makes sense. Because I didn't see an event to handle the update status, I assumed that you still had to create the delegate class to handle it.
Comment 5 Sebastien Pouliot 2013-09-28 20:35:35 UTC
There's a `UpdatedState` event for that :-)
Comment 6 dj_technohead 2013-09-28 21:16:40 UTC
Then I see that you guys have done all the hard work for me. I should have never doubted you.