Xamarian core bluetooth

2019-09-11 08:36发布

问题:

Through this documentation, I have managed to the get nearby bluetooth device to show in the console.

But what I'm looking to do is to display Discovered devices on UIViewController(display a list on my app).

using Foundation;
using System;
using UIKit;
using CoreBluetooth;
using CoreFoundation;

namespace project
{
    public partial class ScanBluetoothController : UIViewController
    {
        public ScanBluetoothController (IntPtr handle) : base (handle)
        {
        }

        MyCBCentralManagerDelegate myDel;


        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.

            myDel = new MySimpleCBCentralManagerDelegate();

            var mgr = new CBCentralManager(myDel, DispatchQueue.CurrentQueue);

    }


    }

}

My CBCentralManagerDelegate class looks as follows

using System;
using System.Timers;
using CoreBluetooth;
using Foundation;

namespace project
{
    public class MyCBCentralManagerDelegate: CBCentralManagerDelegate
    { 
        override public void UpdatedState(CBCentralManager central)
        {
            if (central.State == CBCentralManagerState.PoweredOn)
            {
                //Passing in null scans for all peripherals. Peripherals can be targeted by using CBUIIDs
                CBUUID[] cbuuids = null;
                central.ScanForPeripherals(cbuuids); //Initiates async calls of DiscoveredPeripheral
                //Timeout after 30 seconds
                var timer = new Timer(30  * 1000);
                timer.Elapsed += (sender, e) => central.StopScan();
            }
            else {
                //Invalid state -- Bluetooth powered down, unavailable, etc.
                Console.WriteLine("Bluetooth is not available");
            }
        }

        public override void DiscoveredPeripheral(CBCentralManager central, CBPeripheral peripheral, NSDictionary advertisementData, NSNumber RSSI)
        {
            Console.WriteLine("Discovered {0}, data {1}, RSSI {2}", peripheral.Name, advertisementData, RSSI);
        }

    }
}

Any help at all on how to achieve this would be greatly appreciated.