Xamarin 上 iOS 上的 ExternalAccessory [英] ExternalAccessory on iOS at Xamarin

查看:25
本文介绍了Xamarin 上 iOS 上的 ExternalAccessory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在 Xamarin.iOS 上使用 ExternalAccessory API 吗?

Anybody has a clue on how to use the ExternalAccessory API on Xamarin.iOS?

我的 Xamarin Studio 版本是 4.0.12(build 3),Xamarin.Android 版本 4.8.1,Xamarin.iOS 版本 6.4.5.0 和 Xcode 版本 5.0 (5A1413),我尝试了 6.1 和 7.0 iPad/iPhone.

My Xamarin Studio version is 4.0.12(build 3), Xamarin.Android version 4.8.1, Xamarin.iOS version 6.4.5.0 and Xcode is Version 5.0 (5A1413) and I tried target both 6.1 and 7.0 iPad/iPhone.

我浏览了互联网,没有太多文档.甚至 MonoTouch 文档也有损坏的链接.

I've walked the internet and there is not much documentation. Even the MonoTouch docs have broken links.

我想要的是,列出连接的蓝牙设备,按名称获取其中一个,然后连接到它,这样我就可以打开一个套接字并开始向它发送数据.它是使用串行通信的设备,是的,它具有 Apple 外部附件协议 ID.

What I want is, list the connected bluetooth devices, get one of then by name and then connect to it so I can open a socket and start sending data to it. It is a device that uses Serial communication and yes, it has the Apple external accessory protocol ID.

我已经试过了:

var am = EAAccessoryManager.SharedAccessoryManager;

它只是向我抛出一个 InvaidCastException 异常.

It just throws me an exception an InvaidCastException.

有什么线索吗?

谢谢!我非常感谢您的帮助.

Thanks! I really appreciate the help.

PS:Xamarin 详细信息

PS: Xamarin Details

Xamarin Studio
Version 4.0.12 (build 3)
Installation UUID: 7348d641-ed6d-4c8a-b59a-116674e06dfd
Runtime:
    Mono 3.2.0 ((no/7c7fcc7)
    GTK 2.24.20
    GTK# (2.12.0.0)
    Package version: 302000000

[...]

Apple Developer Tools
Xcode 5.0 (3332.25)
Build 5A1413

[...]

Xamarin.iOS
Version: 6.4.5.0 (Trial Edition)
Hash: 1336a36
Branch: 
Build date: 2013-10-09 11:14:45-0400

Build Information
Release ID: 400120003
Git revision: 593d7acb1cb78ceeeb482d5133cf1fe514467e39
Build date: 2013-08-07 20:30:53+0000
Xamarin addins: 25a0858b281923e666b09259ad4746b774e0a873

Operating System
Mac OS X 10.8.5
Darwin Gutembergs-MacBook-Pro.local 12.5.0 Darwin Kernel Version 12.5.0
    Mon Jul 29 16:33:49 PDT 2013
    root:xnu-2050.48.11~1/RELEASE_X86_64 x86_64

推荐答案

虽然看起来你已经解决了这个问题,但我想我会展示一些展示基础知识的代码片段(在这种情况下连接到 Sphero 和变成绿色):

Although it seems like you've worked this out, I thought I'd show some code snippets that show the basics (in this case connecting to a Sphero and turning it green):

EAAccessoryManager mgr = EAAccessoryManager.SharedAccessoryManager;
var accessories = mgr.ConnectedAccessories;
foreach(var accessory in accessories)
{
    myLabel.Text = "Got me an accessory";
    Console.WriteLine(accessory.ToString());
    Console.WriteLine(accessory.Name);
    var protocol = "com.orbotix.robotprotocol";

    if(accessory.ProtocolStrings.Where(s => s == protocol).Any())
    {
        myLabel.Text = "Got me a Sphero";

        var session = new EASession(accessory, protocol);
        var outputStream = session.OutputStream;
        outputStream.Delegate = new MyOutputStreamDelegate(myLabel);
        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)
    {
        if(streamEvent == NSStreamEvent.HasSpaceAvailable && ! hasWritten)
        {
            //Set the color of the Sphero
            var written = ((NSOutputStream)theStream).Write(new byte[] {0xFF, 0xFF, 0x02, 0x20, 0x0e, 0x05, 0x1F, 0xFF, 0x1B, 0x00, 0x91}, 11);
            if(written == 11)
            {
                label.Text = "Sphero should be green";
            }
            hasWritten = true;
        }
    }
}

这篇关于Xamarin 上 iOS 上的 ExternalAccessory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆