gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:52:53 26 4
gpt4 key购买 nike

我在论坛上和 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com