通过this文档,我设法将附近的蓝牙设备显示在控制台中。
但我要做的是在UIViewController上显示发现的设备(在我的应用上显示一个列表)。
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);
}
}
}
我的CBCentralManagerDelegate类如下所示
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);
}
}
}
非常感谢任何有关如何实现这一目标的帮助。