Bug 60696 - After updating to OSX High Sierra, the CoreBluetooth.CBCentralManager is not working: BLE devices are not discovered
Summary: After updating to OSX High Sierra, the CoreBluetooth.CBCentralManager is not ...
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 3.8.0 (d15-4)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Manuel de la Peña [MSFT]
Depends on:
Reported: 2017-11-15 14:50 UTC by IndraXamarin
Modified: 2018-02-22 17:02 UTC (History)
4 users (show)

Tags: OSX High Sierra, CBCentralManager
Is this bug a regression?: ---
Last known good build:

Working Objc Sample (1.95 MB, application/zip)
2017-11-21 16:35 UTC, Manuel de la Peña [MSFT]

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 IndraXamarin 2017-11-15 14:50:58 UTC
# Steps to reproduce
Compile and run Xamarin test program: https://developer.xamarin.com/samples/mac/HeartRateMonitor/ 

# Expected behavior
The program should show the list of heart rate monitor devices.

# Actual behavior
The list of devices is empty because the CBCentralManager object is not raising events when Bluetooth devices are discovered.

# Supplemental info (logs, images, videos)

# Test environment (full version information)
=== Xamarin Studio Community ===

Version 6.3 (build 864)
Installation UUID: 7ed6772a-18a8-486e-a3bd-8ef48fe3a12d
	Mono (2017-06/1f4613aa1ac) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 504010006

=== NuGet ===


=== Xamarin.Profiler ===

'/Applications/Xamarin Profiler.app' not found

=== Xamarin.Android ===

Version: (Xamarin Studio Community)
Android SDK: /Users/same/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.4   (API level 19)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

Android Designer EPL code available here:

=== Apple Developer Tools ===

Xcode 9.0 (13247)
Build 9A235

=== Xamarin.iOS ===

Version: (Xamarin Studio Community)
Hash: 51128b8c
Branch: xcode9.1
Build date: 2017-10-31 22:42:13-0400

=== Xamarin.Mac ===

Version: (Xamarin Studio Community)

=== Xamarin Inspector ===

Not Installed

=== Build Information ===

Release ID: 603000864
Git revision: 6c2f6737278ccc3e81e12276d49c0d92f975f189
Build date: 2017-04-24 11:26:01-04
Xamarin addins: d8d46e577d8507c35260ce9d73df3c33415bb214
Build lane: monodevelop-lion-d15-1

=== Operating System ===

Mac OS X 10.13.0
Darwin sames-MacBook-Pro.local 17.0.0 Darwin Kernel Version 17.0.0
    Thu Aug 24 21:48:19 PDT 2017
    root:xnu-4570.1.46~2/RELEASE_X86_64 x86_64
Comment 1 Manuel de la Peña [MSFT] 2017-11-21 11:24:47 UTC
I can confirm the bug. I used a Polar H7 for the testing. The pairing does not show any devices in the stable release as well as beta that would be:

Stable - Xamarin.Mac
Beta - Xamarin.Mac

I made sure that the heart rate monitor worked with the Polar iPhone application and the heart rate monitor was found and correctly paired (this is quite important, heart monitors are a little annoying when pairing since the electrodes need to be wet).
Comment 2 Manuel de la Peña [MSFT] 2017-11-21 16:35:38 UTC
Created attachment 25779 [details]
Working Objc Sample

I have modified the Apple sample to compile in High Sierra to ensure that it works in Objc. The original sample, with compilation errors, can be found here => https://developer.apple.com/library/content/samplecode/HeartRateMonitor/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011322-Intro-DontLinkElementID_2 

The modified sample does pair the heart rate monitor and shows the HRPM.
Comment 3 IndraXamarin 2017-11-27 12:25:20 UTC
Hi Manuel,

What I need it's the bluetooth functionality working from c# using Xamarin.Mac library. Do you know why this library is not working with High Sierra?

The source code of this library seems to be here: https://github.com/xamarin/xamarin-macios/blob/master/src/corebluetooth.cs

Best regards
Comment 4 IndraXamarin 2018-02-15 11:43:06 UTC
Hello all,

We have a Mac app in production that uses bluetooth and that needs a very urgent fix for thi issue

Could Xamarin confirms if it's woking on it and give an estimate on timescales?

Comment 5 Manuel de la Peña [MSFT] 2018-02-20 13:06:14 UTC

I have digging what might be happening, but I have not been able yet to pin point why or custom C# like events do not work. Nevertheless I'm able to give you a work around:

In the sample code you will find the following C# code =>

void InitializeCoreBluetooth ()
     manager.UpdatedState += OnCentralManagerUpdatedState;

     manager.DiscoveredPeripheral += (sender, e) => {
	LogPeripheral ("Discovered", e.Peripheral);
	heartRateMonitors.Add (e.Peripheral);
	deviceTableView.ReloadData ();

     manager.FailedToConnectPeripheral += (sender, e) => {
	LogPeripheral ($"Failed to connect ({e.Error})", e.Peripheral);
	DisconnectMonitor ();

     manager.ConnectedPeripheral += (sender, e) => {
	LogPeripheral ("Connected", e.Peripheral);
	statusLabel.StringValue = "Discovering services...";
	disconnectButton.Hidden = false;
	e.Peripheral.DiscoverServices ();

     manager.DisconnectedPeripheral += (sender, e) => {
	LogPeripheral ("Disconneceted", e.Peripheral);
	heartRateMonitors.Remove (e.Peripheral);
	deviceTableView.ReloadData ();
	DisconnectMonitor ();

     HeartRateMonitor.ScanForHeartRateMonitors (manager);

You will need to remove all the event registration, and move to implement a CBCentralManagerDelegate which you can pass in the creation of the manager. For example (not all the methods are complete, just the discovery one:

class MyDelegate : CBCentralManagerDelegate
    bool scanned = false;
    public override void UpdatedState(CBCentralManager central)
    	var state = central.State;
    	if(state == CBCentralManagerState.PoweredOn && ! scanned)
            Console.WriteLine("Scanning for devices");
            scanned = true;

     public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
        Console.WriteLine($"Found {peripheral.Name}");

And create the manager as:

CBCentralManager manager = new CBCentralManager (new MyDelegate(), null);

I have tested the above with High Sierra and the following Xamarin.Mac master.

Will be testing the workaround with the current stable Xamarin.Mac to make sure it also works.
Comment 6 Manuel de la Peña [MSFT] 2018-02-20 13:16:22 UTC
More info, filtering by peripheral with services does work, that is, you can scan ALL peripherals via:

central.ScanForPeripherals((CBUUID [])null);

or filter for (in this case heart sensors) via:

central.ScanForPeripherals(CBUUID.FromPartial (0x180D));
Comment 7 Manuel de la Peña [MSFT] 2018-02-20 13:56:43 UTC
PR: https://github.com/xamarin/mac-samples/pull/55

Shows a nicer way to deal with the issue. We had a race condition when registering to event.
Comment 8 IndraXamarin 2018-02-22 12:30:42 UTC
Hello Manuel, 

Thanks very much for the workaround. Our Mac app is running again with Bluetooth.

But in our case, the workaound seems only to work if the app is compiled to 64-bits, so we had to work and convert old libraries and the UI, written in GTK, to 64-bits.

Thanks again.

Comment 9 Chris Hamons 2018-02-22 17:02:42 UTC
Given Apple's recent announcements related to 64-bit macOS (https://developer.apple.com/news/?id=06282017a) moving that way is time well spent.