digidotcom / xbee-csharp

C# library to interact with Digi International's XBee radio frequency modules from mobile devices.
Mozilla Public License 2.0
14 stars 8 forks source link

Does not connect to the XBRR module with iOS/iPhone13, but does connect with Android #25

Closed nfvelado closed 1 year ago

nfvelado commented 1 year ago

Attempting to use 1.0.3 of DigiDotCom XBee Sharp library in a Xamarin iOS application on .NET core 2 to connect to the XBRR module. This application works using Android, however, on an IPhone 13, the connection fails.

The application is able to resolve advertising devices prior to the call below.

Modifying the following code to show debug statements:

public void Open() { Debug.WriteLine("----- Open");

                // Do nothing if the device is already open.
                if (IsOpen)
                       return;

                string connectExceptionMessage = null;

                // Create a task to connect the device.
                Task task = Task.Run(async () =>
                {
                       try
                       {
                             // Abort the connect operation if the given timeout expires.
                             CancellationTokenSource token = new CancellationTokenSource();
                             token.CancelAfter(CONNECTION_TIMEOUT);

                             // Connect the device. Try to connect up to 3 times.
                             var retries = BT_CONNECT_RETRIES;
                             while (!IsOpen && retries > 0)
                             {
                                    // Force connection transport to be BLE. This fixes an issue with some Android devices
                                    // throwing the error '133' while connecting with a BLE device and requesting the GATT
                                    // server services and characteristics.
                                    var parameters = new ConnectParameters(forceBleTransport: true);
                                    if (device == null)
                                    {
                                           Console.WriteLine("BLEInterface: ConnectToKnownDeviceAsync " + deviceGuid);
                                           device = await adapter.ConnectToKnownDeviceAsync(deviceGuid, parameters, token.Token);
                                    }
                                    else
                                    {
                                           Console.WriteLine("BLEInterface: ConnectToDeviceAsync " + device.Name);
                                           await adapter.ConnectToDeviceAsync(device, parameters, token.Token);
                                    }
                                    await Task.Delay(1000);
                                    Console.WriteLine(string.Format("BLEInterface: after delay, device: {0}, state: {1}" + device == null ? "null" : device.Name, device == null ? "Unknown" : device.State.ToString()));
        if (device != null && device.State == DeviceState.Connected)
                                           IsOpen = true;
                                    retries--;
                             }

Produces the following output:

2023-02-26 20:44:57.872 iOS[864:113846] 26 Feb 20:44:57 BLEAdvertised Device: NA-9602980

2023-02-26 20:44:57.872 iOS[864:113846] 26 Feb 20:44:57 BLEAdvertised Connecting to device: NA-9602980

Resolved pending breakpoint at 'BLE.cs:220,1' to void BLE.BLEObj.d33.MoveNext () [0x0001f]. Resolved pending breakpoint at 'BLE.cs:217,1' to void BLE.BLEObj.d33.MoveNext () [0x00009]. Resolved pending breakpoint at 'BLE.cs:253,1' to void BLE.BLEObj.d__33.MoveNext () [0x00115]. 2023-02-26 20:45:01.435 iOS[864:113956] 26 Feb 20:45:01 BLEPurge: stale entry removed NA-9602980

[0:] ----- Open Thread started: #5 Thread started: #6 Thread started: #7 Thread started: #8 2023-02-26 20:45:02.714 iOS[864:114029] BLEInterface: ConnectToDeviceAsync NA-9602980

[0:] ----- Close 2023-02-26 20:45:22.699 iOS[864:113846] 26 Feb 20:45:22 ConnectToDevice: General Exception XBeeLibrary.Core.Exceptions.XBeeException: Error opening the connection interface > Could not connect to the XBee BLE device > A task was canceled. at XBeeLibrary.Core.AbstractXBeeDevice.Open () [0x00045] in C:\xbee-csharp-master\XBeeLibrary.Core\AbstractXBeeDevice.cs:2399 at XBeeLibrary.Xamarin.XBeeBLEDevice.Open () [0x00001] in C:\xbee-csharp-master\XBeeLibrary.Xamarin\XBeeBLEDevice.cs:228 at BLE.BLEObj.ConnectToDevice () [0x00009] in C:\BLE\BLE.cs:217

From info.plist, have the following snippit:

NSBluetoothPeripheralUsageDescription
   <string>This app needs Bluetooth access to discover nearby Nautic Alert devices.</string>
   <key>NSBluetoothAlwaysUsageDescription</key>
   <string>This app needs Bluetooth access to discover nearby Nautic Alert devices.</string>
   <key>UIBackgroundModes</key>
     <array>
<!--for connecting to devices (client)-->
<string>bluetooth-central</string>

Not sure if there's any other iOS setup needed, assuming this may be a bug in this or the dependency dotnet-bluetooth-le library.

nfvelado commented 1 year ago

OK to close this. Appears the issue is that on iOS, the code calling the library functions must execute it it's own task or thread space and not from the main application thread context. On a .NET standard 2.0 Xamarin app things work.

However, on a .NET MAUI application using .NET core 6 or 7, there seems to still be some issue with some underlying runtime dependency not being met. Will save that for another issue though.