Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to Advertise up to 255 bytes using Bluetooth 5

CaptainXamtasticCaptainXamtastic GBUniversity ✭✭✭
edited January 11 in Xamarin.iOS

I want to use Bluetooth 5.0 to be able to advertise up to 255 bytes using Xamarin.iOS, does anyone have the equivalent to the following Android code, for iOS?

If not code, any pointers, methodology, snippets, doctrine, etc, please!

Kind regards!

// Xamarin.Android
            BluetoothAdapter bluetoothAdapter = _btManager.Adapter;
            BluetoothLeAdvertiser advertiser = bluetoothAdapter.BluetoothLeAdvertiser;

            var maxDataLength = bluetoothAdapter.LeMaximumAdvertisingDataLength;

// Merely determines capability
            var IsLe2MPhySupported = bluetoothAdapter.IsLe2MPhySupported;
            var IsLeCodedPhySupported = bluetoothAdapter.IsLeCodedPhySupported;
            var IsLeExtendedAdvertisingSupported = bluetoothAdapter.IsLeExtendedAdvertisingSupported;
            var IsLePeriodicAdvertisingSupported = bluetoothAdapter.IsLePeriodicAdvertisingSupported;
            var IsMultipleAdvertisementSupported = bluetoothAdapter.IsMultipleAdvertisementSupported;
            var IsOffloadedFilteringSupported = bluetoothAdapter.IsOffloadedFilteringSupported;
            var IsOffloadedScanBatchingSupported = bluetoothAdapter.IsOffloadedScanBatchingSupported;

            byte[] bytes = Encoding.ASCII.GetBytes("The quick brown fox jumped over the lazy dog"); // Bytes:  54 68 65 20 71 75 69 63 6b 20 62 72 6f etcetera

            var uuid = new Android.OS.ParcelUuid(Java.Util.UUID.FromString("6bf6b447-63a8-4cea-81bd-15d87f3b3e43"));

            var parameters = new AdvertisingSetParameters.Builder()
                    .SetLegacyMode(false)
                    .SetInterval(AdvertisingSetParameters.IntervalHigh)
                    .SetTxPowerLevel(AdvertiseTxPower.Medium)
                    .SetPrimaryPhy((ScanSettingsPhy)Android.Bluetooth.BluetoothPhy.Le1m)
                    .SetSecondaryPhy((ScanSettingsPhy)Android.Bluetooth.BluetoothPhy.Le2m);

            AdvertiseData data = new AdvertiseData.Builder()
                        .AddServiceData(uuid, bytes)
                        .Build();

            advertiser.StartAdvertisingSet(parameters.Build(), data, null, null, null, new AdvertisingSetCB());

The class AdvertisingSetCB is merely a required callback that must inherit from AdvertisingSetCallback, vis-a-vis:

        public class AdvertisingSetCB : AdvertisingSetCallback
        {
            public override void OnAdvertisingSetStarted(AdvertisingSet advertisingSet, int txPower, [GeneratedEnum] AdvertiseResult status)
            {
                base.OnAdvertisingSetStarted(advertisingSet, txPower, status);
                Debug.WriteLine($"Status: {status}");
                Debug.WriteLine($"TXPower: {txPower}");
                Debug.WriteLine($"AdvertisingSet: {advertisingSet}");
            }

            public override void OnAdvertisingSetStopped(AdvertisingSet advertisingSet)
            {
                base.OnAdvertisingSetStopped(advertisingSet);
            }
        }
Tagged:
Sign In or Register to comment.