ios - Xamarin.iOS CoreBluetooth/外部附件问题

标签 ios bluetooth xamarin.ios xamarin

我在论坛上和 monotouch 示例 GIT 中心上查找,但从未找到真正实用的示例来使用 CoreBluetooth 以实现以下目标: 1.检查是否有匹配标准(按名称或设备的某些标识符)配对和连接的设备 2.如果已配对但未连接,请尝试连接 3.如果连接失败,则显示符合主题 1 条件的蓝牙设备列表,以便用户选择并连接到它

注意:我尝试连接的设备使用 SPP,但已通过 Apple MFi 认证。它是一个通过蓝牙的信用卡读卡器,其中一些甚至实现了 ExternalAccessory 协议(protocol)

CoreBluetooth 示例页面为空 http://developer.xamarin.com/samples/ios/CoreBluetooth/

我正在尝试这个非常简单的示例,它永远不会在扫描后调用事件:

public static class BTHelper
    {
        private static CBCentralManager manager;
        private static CBUUID UUID;

        static BTHelper()
        {
            manager =
            manager.DiscoveredPeripheral += OnDiscovery;
            manager.ConnectedPeripheral += OnConnected;
            manager.DisconnectedPeripheral += OnDisconnected;
            UUID = CBUUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
        }

        public static void CheckBluetooth()
        {
            manager.ScanForPeripherals(new[] { UUID });
        }

        static void OnDisconnected(object sender, CBPeripheralErrorEventArgs e)
        {
            Console.WriteLine("Disconnected - " + e.Peripheral.Name);
        }

        static void OnConnected(object sender, CBPeripheralEventArgs e)
        {
            Console.WriteLine("Connected - " + e.Peripheral.Name);
        }

        static void OnDiscovery(object sender, CBDiscoveredPeripheralEventArgs e)
        {
            Console.WriteLine("Found - " + e.Peripheral.Name);
        }
    }

有人可以帮忙吗?我真的厌倦了在 SO 上搜索和寻找许多问题却没有真正的答案。

@XamarinTeam,你们应该提供一个关于如何使用它的示例……我们迷路了,没有引用……

谢谢,非常感谢任何帮助...

古腾堡

最佳答案

看来您正在查看错误的文档。Core Bluetooth 仅允许您使用 GATT 配置文件与蓝牙低功耗 (BLE) 设备进行通信。你不能用 corebluetooth 扫描 SPP 设备。

对于您的 MFI 设备,您需要检查外部附件框架,它允许使用串行端口协议(protocol) (SPP) 等配置文件与“传统”蓝牙设备进行通信。

回答你的问题: : 1.检查是否有符合条件(按名称或设备的某些标识符)配对和连接的设备

You can use showBluetoothAccessoryPicker function of EAAccessoryManager to get list of Available devices, read more here

2.如果已配对但未连接,请尝试连接

There is not any documented way to check for this. You can not initiate connect from app without showBluetoothAccessoryPicker . You can monitor for EAAccessoryDidConnect notification. if this method is not called, and showbluetoothaccessorypicker 's complition get called, your device is not connected.

3.如果连接失败,则显示符合主题 1 条件的蓝牙设备列表,以便用户可以选择并连接到它 1)

After completion of showbluetoothaccessorypicker You can check in ConnectedAccessories . If its not avaiable, call showbluetoothaccessorypicker to display list of accessories.

在您的代码中使用外部附件框架的示例代码

EAAccessoryManager manager= EAAccessoryManager.SharedAccessoryManager;
var allaccessorries= manager.ConnectedAccessories;
foreach(var accessory in allaccessorries)
{
    yourlable.Text = "find accessory";
    Console.WriteLine(accessory.ToString());
    Console.WriteLine(accessory.Name);
    var protocol = "com.Yourprotocol.name";

    if(accessory.ProtocolStrings.Where(s => s == protocol).Any())
    {
        yourlable.Text = "Accessory  found";
        //start session
        var session = new EASession(accessory, protocol);
        var outputStream = session.OutputStream;
        outputStream.Delegate = new MyOutputStreamDelegate(yourlable);
        outputStream.Schedule(NSRunLoop.Current, "kCFRunLoopDefaultMode");
        outputStream.Open();
    }
}

public class MyOutputStreamDelegate : NSStreamDelegate
{
    UILabel label;
    bool hasWritten = false;

    public MyOutputStreamDelegate(UILabel label)
    {
        this.label = label;
    }
    public override void HandleEvent(NSStream theStream, NSStreamEvent streamEvent)
    {
         //write code to handle  stream.

    }
}

Exeternal Accessory框架没有具体的demo, 但您可以查看此示例代码以了解其工作原理。:

Whole Project

AccessoryBrowser class

关于ios - Xamarin.iOS CoreBluetooth/外部附件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25695735/

相关文章:

ios - 使用自定义 getter 和 setter 通过 ARC 发送消息到已释放实例

iOS IAP 不适用于仅 ipv6 网络

ios - 如何在每个页面 IOS 上包含滑出菜单(删除后退按钮)

android - 是否有可能改进 BLE 服务发现

Android 2.1 : Grateful for crash analysis help: signal 11 (SIGSEGV), 故障地址 deadbaad

c# - iOS 版本 Xamarin 应用程序兼容性

objective-c - 为 iOS 创建静态链接库

ios - Google Maps iOS didTapInfoWindowOfMarker 在其他标记之上时未触发

ios - 我怎样才能使 iBeacons 私有(private)

ios - 覆盖方法 `GetSizeForItem` 返回 "no suitable method found to override"