Repository navigation
Description
This issue has been moved from a ticket on Developer Community.
[severity:I'm unable to use this version] [regression] [worked-in:VS2022 17.0.0]
Similar code previously confirmed working on VS2022 17.0.0 Xamarin.iOS deployment but now getting the MonoTouchException on scanning and writing request bytes to BLE devices. (using https://github.com/xabre/xamarin-bluetooth-le)
Scan (adapter. StartScanningForDevicesAsync())
Write Request Bytes (characteristic. WriteAsync(bytes))
Really hope for the fix or getting access to older 17.0.0 version again would be great.
Original Comments
Feedback Bot on 12/10/2021, 00:30 AM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Rachel Kang [MSFT] on 12/13/2021, 09:55 AM:
Hi, Mas - thanks for your feedback.
The attached screenshots both show large numbers being indexed or allocated, potentially suggesting a bug in the values being passed to write. However, the screenshots do not show the entire stack trace.
Please attach your project or a sample project reproducing the issue so that we can investigate further.
Mas Azalya Yusof on 12/14/2021, 05:31 AM:
Hi Rachel, thank you for getting back with the query. We just use similar code for iOS BLE Client example from https://github.com/xabre/xamarin-bluetooth-le - adapter.StartScanningForDevicesAsync() for scanning and characteristic.WriteAsync(bytes) to write request bytes. Same code confirmed working in previous Visual Studio 2022 17.0.0 on my PC and also same version currently on my colleague’s PC.
charles david young on 12/14/2021, 11:42 AM:
Sorry that this has debug code. If you would like me to clean it up and reduce it to a minimal project just let me know.
The exception is happening on
await _bluetoothAdapter.StartScanningForDevicesAsync();
charles david young on 12/14/2021, 01:32 PM:
This is a somewhat cleaned up version of the project. What I discovered is that even before calling _bluetoothAdapter.StartScanningForDevicesAsync() the _bluetoothAdapter.ConnectedDevices.Count == 0. If there are no connected devices then perhaps trying to make it scan is causing the exception.
So I have commented out the section that was causing the exception. Now the app does not doing anything except changes the button text to say “count = 0”.
So I guess the real question is why my app is not finding the connected bluetooth device which is active. I can use another bluetooth app runnning on this iPhone 12 to talk to my BLE peripheral.
Rachel Kang [MSFT] on 12/14/2021, 08:48 AM:
Hi, Mas - does the sample at https://github.com/xabre/xamarin-bluetooth-le reproduce the issue you are experiencing? If not, please attach a sample project that does, as we need more information to investigate.
charles david young on 12/14/2021, 11:37 AM:
I have a project that does exactly this. Should I post it?
I’ll also tried the https://github.com/xabre/xamarin-bluetooth-le and it has a similar exception except that it occurs after it starts scanning and has found several devices. I assume that this is a different problem but who knows. This project is also not using forms like I am so I have not tried to debug it.
Thanks
Charles
Mas Azalya Yusof on 12/14/2021, 11:20 PM:
Hi Rachel, yes the BLE.Client.iOS sample project in https://github.com/xabre/xamarin-bluetooth-le reproduce the issue. Scanning throws the error when try to add our BLE devices. Comment out the following code manage to get scanning works first so that I can try Write Bytes which also throwing similar error on Microsoft Visual Studio Community 2022 Version 17.0.2
Mas Azalya Yusof on 12/14/2021, 11:37 PM:
Confirmed issue persists in Microsoft Visual Studio Community 2022 Version 17.0.3
charles david young on 12/15/2021, 04:21 AM:
I also get an exception when I press the scan button, but only after several devices are found. Indeed, if I comment out the section of code indicated the scan detects my BLE peripheral and I am able to connect to it. It seems to work perfectly.
So I am confused as to whether this is the same error that I am getting. Should _bluetoothAdapter.ConnectedDevices.Count be greater than 0 before calling _bluetoothAdapter.StartScanningForDevicesAsync()? It is not clear to me if “ConnectedDevices” refers to the local BLE adapter device or remote peripheral devices.
If you could clarify this it might help me move forward. Thanks!
Charles
charles david young on 12/15/2021, 07:07 AM:
Ok, now that I have studied the code more I see that ConnectedDevices refers to the remote peripheral devices and the first time the scan button is pushed it is indeed 0.
So I am inclined to remove the Plugin.BLE and install the sources instead so that I can make the indicated fix. Is that the recommended solution or will there be an update to fix this problem?
Thanks
Charles
Rachel Kang [MSFT] on 12/15/2021, 10:01 AM:
Please provide a Xamarin.iOS sample reproducing this issue without using a third-party bluetooth library so that we can investigate how this issue may lie within Xamarin.iOS. If the issue exists only when using the referenced plugin, consider opening an issue in their repo.
charles david young on 12/15/2021, 11:07 AM:
Rachel, Is there a Xamarin.iOS bluetooth example that does not use the third-party library?
Mas, do you consider your fix the correct solution or is there a drawback to this fix? I guess I’ll try incorporating the xamarin-bluetooth-le source files and try your fix.
Charles
Mas Azalya Yusof on 12/15/2021, 05:59 PM:
Charles,
The fix can be considered just to get scanning works but still the write bytes throwing same error. And the BLE devices resulted from the scan also missing some details like whether they are already paired.
Mas Azalya Yusof on 12/15/2021, 06:49 PM:
Hi Rachel, while I understand why you are asking for the code without the plugin, I am very sure the failure has to do with updates made between 17.0.0 and the current version. As the BLE library was previously working on my PC (until i upgraded) and is still working on my colleagues PC where he is still using 17.0.0
charles david young on 12/16/2021, 06:27 AM:
Mas,
I decided to look closer as to why the code you identified was causing the exception. It is happening on the statement
var cbuuid = array.GetItem<CBUUID>(i);
Here is the entire context:
else if (key == CBAdvertisement.DataServiceUUIDsKey || key == CBAdvertisement.DataOverflowServiceUUIDsKey) { var array = (NSArray)advertisementData.ObjectForKey(key);for (nuint i = 0; i < array.Count; i++) { var cbuuid = array.GetItem<CBUUID>(i); switch (cbuuid.Data.Length) {
I find this comment about NSArray so it makes me suspicious that maybe this has something to do with the exception:
Due to the loose typed nature of NSArray, it is not generally used or surfaced by most APIs. Instead, strongly typed C# arrays are surfaced to developers. This type is available in the binding for some low-level operations that require the use of the type directly.
Charles
charles david young on 12/16/2021, 03:36 PM:
I am still trying to figure out why it is crashing when it tries to pull the CBUUID out of the array. I tried the following as well
var array = (NSArray)advertisementData.ObjectForKey(key);for (CBUUID cbuuid in array) {
but I am getting a Compiler Error CS1579 indicating that NSArray does not have an iterator. Perhaps that is why array.GetItem<CBUUID>(i) gets an exception.
On the positive side, I can see the advertisement coming from my BLE peripheral and the contents of the NSDictionary advertisementData look good. Parsing out that array of strings for CBAdvertisement.DataServiceUUIDsKey is proving to be difficult.
Any help would be appreciated.
Charles
charles david young on 12/17/2021, 04:16 AM:
Rachel,
I have opened an issue in the repo. There are additional details reported there that may help shed light on the problem.
dotnet-bluetooth-le/dotnet-bluetooth-le#555
I would point out that the exception is happening in the Native stack trace and not in the app. The application stack indicates [External Code]. Thus, the issue seems to be with Visual Studio. Please advise.
Charles
Mas Azalya Yusof on 12/17/2021, 07:26 AM:
Thank you Charles on the details you worked on.
In addition to that, error thrown on characteristic.WriteAsync(bytes) also confirmed due to the NSArray. The following line of code throws error on latest VS 2022.
var nsdata = NSData.FromArray(data);
Please note Rachel. Thank you.
Rachel Kang [MSFT] on 12/17/2021, 07:46 AM:
As discussed in this stack overflow post, External Code just means there is not debugging information for the library in question. Those could be any library you depend on without a matching pdb file.
As we currently do not have a sample project reproducing this issue without a 3rd party library, this issue will be assumed to be handled by dotnet-bluetooth-le/dotnet-bluetooth-le#555 until we get more actionable information.
charles david young on 12/17/2021, 09:54 AM:
Mas,
I never execute that instruction. I set a breakpoint there and the app can connect to my bluetooth peripheral normally without hitting it.
BTW, there is a section below the one that is crashing that looks similar but is using different code. I will try out similar code to see if it works better:
else if (key == CBAdvertisement.DataServiceDataKey)
Charles
charles david young on 12/18/2021, 11:10 AM:
Here is a little project with a few lines of code in the main() of the iOS app. They are not identical to the lines that are crashing in the BLE app but if we can resolve this exception it might help solve the other.
charles david young on 12/19/2021, 06:52 AM:
Mas,
Rather than download the zip you can create a new mobile app solution and put the following lines at the beginning of the Main() method in the Main.cs of the iOS app. When I run this on my iPhone 12 Pro it crashes on the .GetItem as it does in the BLE app. If you could confirm it does the same thing for you and also that it does not crash with 17.0.0 it would be great. Thanks, Charles
//var array = (NSArray)advertisementData.ObjectForKey(key);
//afterwards array = {("CBBFE0E2-F7F3-4206-84E0-84CBB3D09DFC")};
var testarray = NSArray.FromObjects("CBBFE0E2-F7F3-4206-84E0-84CBB3D09DFC", "2nd string");
for (nuint i = 0; i < testarray.Count; i++)
{
var cbuuid = testarray.GetItem<NSString>(i);
}
charles david young on 12/19/2021, 10:44 AM:
Rachel,
You asked for more actionable information and I have provided it. The project that I provided does not rely on a 3rd party library. You can either test it or implement your own project adding the few lines of code I described in the message to Mas.
Thanks
Charles
Rachel Kang [MSFT] on 12/20/2021, 01:14 PM:
Hi, @charles david young - thanks for attaching a sample. Unfortunately, I was not able to reproduce the issue; when I ran the app from VSMac to an iPhone, it deployed successfully without any errors.
The next time you experience this issue, please provide updated build logs and/or consider sharing a different sample that does not use the plugin but reproduces the issue. Without more information we are unable to investigate this issue further.
charles david young on 12/20/2021, 06:21 PM:
Rachel,
You used the like app I sent? That does not use 3rd party code.
I am using VS windows and deploying to an iPhone. Perhaps that is where the problem lies. Could you use windows instead?
Mas, have you duplicated the problem with my code?
Charles
Mas Azalya Yusof on 12/20/2021, 08:19 PM:
Hi All,
I can confirm the code given by Charles throw the Foundation.MonoTouchException: Objective-C exception on VS 2022 latest version and working fine as shown on my colleague’s PC using VS 2022 version 17.0.0.
And we both use VS windows deploying to iPhone.
charles david young on 12/21/2021, 06:05 AM:
Thanks for confirming, Mas!
Rachel, it is interesting that the problem seems to be isolated to VS Windows. I see that there is a newer version, 17.0.4. I’ll give that a try to see if there is an improvement.
Charles
Chris Hamons [MSFT] on 12/22/2021, 11:48 AM:
Hey folks,
I tested the sample (testNSArray.zip) on VS 2019 and 2022 deploying to my iOS device in Release and Debug and it worked for me every time.
Are there other configuration options that are needed to make this fail?
charles david young on 12/22/2021, 00:51 PM:
Are you deploying with VS Windows version 17.0.3 or 17.0.4? VS 2022 version 17.0.0 works but after the update it does not.
charles david young on 12/24/2021, 08:21 AM:
Mas and I have provided detailed information and a simple project to duplicate this error. Why does MS have such a hard time verifying it?
Just use VS Windows version 17.0.3 or 17.0.4 and deploy to an iPhone and testNSArray should fail.
In the meantime, I would love to go back to VS Windows version 17.0.0 to allow me to move forward on my project. Is that possible?
Charles
charles david young on 12/28/2021, 11:10 AM:
Chris,
Are you deploying with VS Windows version 17.0.3 or 17.0.4? For both Mas and me testNSArray fails when deployed to iPhone.
Thanks, Charles
charles david young on 12/31/2021, 10:49 AM:
Mas,
I can report success using this app just by commenting out the lines 318-337 you initially identified. I am not sure what these lines do but the app has been working perfectly. I can scan for beacons and connect with my peripheral BLE device. I can then start receiving updates in the form of UART strings from my peripheral. I even have the updates working in the background when the phone is locked.
I wish MS would acknowledge and fix the problem but for now I am moving forward. If you have any questions feel free to ask.
Charles
charles david young on 12/31/2021, 10:57 AM:
Actually, one can either comment out the lines or fake the service ID like I did:
for (nuint i = 0; i < array.Count; i++) { // var cbuuid = array.GetItem<CBUUID>(i); // fake UART service until can resolve exception from previous line var cbuuid = CBUUID.FromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");switch (cbuuid.Data.Length) {
Mas Azalya Yusof on 1/2/2022, 08:10 PM:
Thanks Charles for that.
To MSFT,
I hope the reported issue can be fixed so that we don’t need extra work to be able to use the plugin .dll files having commented out codes and stuff.
Best regards,
Mas
Chris Hamons [MSFT] on 1/4/2022, 02:23 PM:
I have tested this using 17.0.3 on iPhone 13 Pro Mac device running iOS 15.1.1 on both release and debug and am not seeing it. 😦
charles david young on 1/4/2022, 05:07 PM:
Más, are you using a real iPhone? I am using a real iPhone 12 Pro. Perhaps the bug only shows up in real iPhones?
Mas Azalya Yusof on 1/5/2022, 04:03 AM:
Yes I’m using real iPhone, confirm this issue happened on iPhone 8 running iOS 15.1 and iPhone SE running iOS 15.2.
Mas Azalya Yusof on 1/11/2022, 09:05 PM:
Hi All,
Just spotted today another issue with latest VS2022 17.0.4 getting similar exception as shown and crash the app when touching the plot (using OxyPlot library) while confirm working fine on my colleague’s VS2022 17.0.0
charles david young on 1/12/2022, 09:56 AM:
So I guess this is in the library so you don’t have a code snippet?
Charles
Alex Soto [MSFT] on 1/17/2022, 07:36 PM:
Hey folks can you try this Xamarin package on macOS?
We believe we have found the cause and I want to confirm with you this is the case. Please note that you will need to use VSMac in order to use and test this package.
Thank you!
charles david young on 1/18/2022, 03:50 AM:
While I appreciate the effort to fix this problem, I have got better things to do than test a fix in an environment that we did not describe. We have been very explicit that the problem we encountered was with VS 2022 for Windows. I will just wait for the fix for VS 2022 to be released and test it with the little test program I provided and afterwards test on the VSMac.
Charles
charles david young on 1/18/2022, 09:07 AM:
To further my point, I have been fighting another bug in the Xamarin.Essentials.FilePicker that other people have also reported:
However, the problem evidently only exists using VS 2022 on Windows. I tested the same code on VSMac and it works fine. That is why there is little point in testing this fix on VSMac without first testing it using VS 2022 on Windows.
Dave Chambers on 1/24/2022, 00:39 PM:
Alex Soto [MSFT] - Could you provide instructions for installing that package on the Mac? Thanks!
Adam Scharf on 2/9/2022, 05:25 AM:
This issue appears to still be still present in VS2022 v17.0.5 for Windows. I am deploying to an iPad 9th gen running iOS 15.3.
Dave Chambers on 2/9/2022, 06:22 AM:
I am also still seeing both issues using VS2022 v17.0.5 on Win10 deploying to a iPhone11 v15.2.1
As previous posts have clearly stated, this is VS Windows thing which has nothing to do with VSMac.
-
The testNSArray sample crashes with:
2022-02-09 08:42:49.503 Xamarin.PreBuilt.iOS[2423:1817688] Xamarin.iOS: Received unhandled ObjectiveC exception: NSRangeException *** boundsFail: index 4337103160 beyond bounds [0 … 1]
The 4337103160 value is curious; it’s not an obvious value. -
The very simple code that Mas provided:
var nsdata = NSData.FromArray(data);
which causes the particularly ugly Got a segv while executing native code crash
WRT Plugin.BLE: that lib isn’t the issue, it simply uses both bits of code that crash.
#1 occurs when you scan for devices (and other actions as well, but scan is simplest).
Scanning calls Plugin.BLE.iOS.Adapter.ParseAdvertismentData(), which calls NSArray.GetItem<T>(), which throws the boundsFail exception (shown in Mas’ screenshot on Dec 15). Commenting out the code (as in the screenshot) does indeed work around this problem in this one instance, though it’s scary to know that NSArray.GetItem() has this issue and some other usage could cause the exception. Given that testNSArray concisely demonstrates the issue, focusing on Plugin.BLE is a red herring.
#2 occurs when you write data to a Characteristic. The basic sequence is:
dvc = adapter.ConnectToKnownDeviceAsync()
svc = dvc.GetServiceAsync()
chx = svc.GetCharacteristicAsync()
then, finally:
chx.WriteAsync() <-- this fcn calls NSData.FromArray(), and crashes.
This one’s a show-stopper as there’s no work-around.
And again, given that Mas’ trivially simple NSData.FromArray() example causes the crash, focusing on Plugin.BLE is again a red herring
I’m able to repro both issues in about 10 minutes… why is this so hard for MSFT to test?
charles david young on 2/9/2022, 09:56 AM:
It is so hard for MSFT to fix because so far they have evidently not tested it in the environment where the failure occurs.
Charles
Dave Chambers on 2/9/2022, 10:12 AM:
charles david young I suspect that they’re all-hands-on-deck for MAUI, which is the much-hyped future but is months late, thus they have put Xamarin maintenance on the far back burner. Which is sad, because I’d guess that fixing this bug would take less than half a man-day because it’s sooooo easy to reproduce (thanks to this thread). In the meantime I’m TOTALLY screwed because my app can’t function w/o this fix. Co-workers have begun questioning my choice of Xamarin, and “I’m waiting on MSFT” is wearing thin with management.
Alex Soto [MSFT] Chris Hamons [MSFT] Rachel Kang [MSFT] do your customers (me, etc) a solid and get this fixed ASAP.
charles david young on 2/9/2022, 10:22 AM:
Hey Dave,
Are you trying to use the BLE.Client.iOS? Are you sure you can’t just comment out the offending code? My app works perfectly without it.
Charles
Adam Scharf on 2/9/2022, 10:25 AM:
Dave Chambers, is it possible for you to downgrade your VS2022 to 17.0.0? I did that and I was able to get my app working with Bluetooth as expected.
Dave Chambers on 2/9/2022, 01:18 PM:
Adam Scharf I can’t find an installer for 17.0.0 - got a link?
I was able to downgrade to VS2019 16.11.6 and that solves the NSData.FromArray() issue, so I’ll muddle along with VS2019 until MSFT deigns to fix their bug in VS2022.
Side note: VS2019 has a deployment bug, which was fixed by this:
https://developercommunity.visualstudio.com/t/Xamarin-iOS-deployment-failing-after-iOS/1541200#T-N1566189
Adam Scharf on 2/9/2022, 01:24 PM:
Dave Chambers This will only work if you are using Pro or Enterprise, so sorry if you’re not using those, but you can get the bootstrappers from here: https://docs.microsoft.com/en-us/visualstudio/releases/2022/release-history#updating-your-installation-to-a-specific-release.
Original Solutions
(no solutions)



Activity
wislon commentedon Jun 22, 2022
FYI for anyone who is running into this on VS for Mac (2019), in my particular instance, this started immediately after I upgraded to Xcode 13.4.1. After I used VS for Mac (still 2019) to upgrade all the Mono, .NET and Xamarin iOS SDKs, the problem quietly went away again.
I have not installed VS for Mac 2022.
rolfbjarne commentedon Sep 25, 2025
According to this comment: dotnet-bluetooth-le/dotnet-bluetooth-le#555 (comment), and reading through this issue, it seems the problem has been fixed.
If that's not the case, please attach a .NET test project we can use to reproduce the problem.
dotnet-policy-service commentedon Oct 3, 2025
Hi @@vsfeedback. Due to inactivity, we will be closing this issue. Please feel free to re-open this issue if the issue persists. For enhanced visibility, if over 7 days have passed, please open a new issue and link this issue there. Thank you.