Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

xamarin.ios - ExternalAccessory on iOS at Xamarin

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

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.

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

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.

I've tried this:

var am = EAAccessoryManager.SharedAccessoryManager;

It just throws me an exception an InvaidCastException.

Any clues?

Thanks! I really appreciate the help.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
    }
}

and

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...