【问题标题】:USB-Programming on iPhoneiPhone 上的 USB 编程
【发布时间】:2011-05-06 07:59:14
【问题描述】:

我要做的就是通过 USB 端口发送/接收数据。 使用官方 SDK 是不可能的 - 除了 MFi。

还有哪些其他方法可以与其他设备进行有线通信,您会建议吗?

提前非常感谢!

GD

【问题讨论】:

    标签: iphone usb mfi


    【解决方案1】:

    软件开发人员不需要 MFI 会员资格,只有硬件开发人员需要。另外你指的是什么USB端口?你指的是 iPhone 的 32 针连接器吗?您始终可以使用蓝牙将数据发送到硬件设备。

    更新 1 -

    首先抱歉,代码可能看起来很复杂,我是从我当前的项目中粘贴的。如果您想要一个示例项目,请发表评论。

    首先你需要在视图中注册通知加载-

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidConnect:) name:EAAccessoryDidConnectNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_accessoryDidDisconnect:) name:EAAccessoryDidDisconnectNotification object:nil];
    [[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
    

    然后你应该找到附带的配件 -

         _accessoryList = [[NSMutableArray alloc] initWithArray:[[EAAccessoryManager sharedAccessoryManager] connectedAccessories]];
    

    然后检查这是您正在寻找的正确配件 -

    for(EAAccessory *obj in _accessoryList)
        {
            if ([[obj protocolStrings] containsObject:@"com.bluebamboo.p25i"])//if find the accessory(p25) record it
            {
                [accessoryLabel setText:@"P25mi connected"]; // yup this is correct accessory!
                [obj release];
                break;
            }
        }
    

    然后打开一个会话 -

    //if accessory not null
    if(accessory)
    {
        session = [[EASession alloc] initWithAccessory:accessory forProtocol:@"com.bluebamboo.p25i"];//initial session that pair with protocol string
        [[session outputStream] setDelegate:self];//set delegate class for output stream 
        [[session outputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; //set outputstream loop
        [[session outputStream] open]; //open session
        [[session inputStream] setDelegate:self];
        [[session inputStream] open];
        [[session inputStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    
    }
    

    然后将调用流委托 -

    //this is a stream listener function that would actived by system while steam has any event
    - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
    {   
    
    
    
        switch(streamEvent)
        {
            case NSStreamEventOpenCompleted:
                if(theStream==[session outputStream])//to identify which stream has been opend
                {
                    [self addLabel:@"outputNSStream open;"];
                }
                else
                {
                    [self addLabel:@"inputNSStream open:"];
                }
    
    
                break;
            case NSStreamEventHasBytesAvailable:
                //if system has stream data comes in
                [self addLabel:@"receiving Data;"];
    
                uint8_t buf2[100];//create a buffer
                unsigned int len = 0;
                //read buffer commands return actuall length of return data
                len = [[session inputStream] read:buf2 maxLength:100];
    
                if (len>0 )
                {
                    if (buf2[4]==0x03&&buf2[5]==0x00)//if two bytes are 0x03 and 0x00, that means print success 
                    {
                        //display success message
                        alertMessage = [[UIAlertView alloc] initWithTitle:@"SUCCESS:" message:@"P25i have printed Text successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alertMessage show];
                        [alertMessage release];
    
                        [self addLabel:@"received success"]; 
                    }
    
                }
    
    
                [self enableButton];
                //operation finished then close all streams and session
                [[session outputStream] close];
                [[session outputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                [[session outputStream] setDelegate:nil];
                [[session inputStream] close];
                [[session inputStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
                [[session inputStream] setDelegate:nil];
                [session release];
                session = nil;
                //[self connectSession];
                //switcher2=TRUE;
    
                break;
            case NSStreamEventHasSpaceAvailable:
    
                [self addLabel:@" NSStreamEventHasSpaceAvailable"];
                if (switcher2)//we send loop mode so application would keep sending data to p25, the switcher2 identify the data has send to P25m
                {
    
    
                             //[self addLabel:@"buffer is selected"];
                     int contentLength=[printContent.text length];
                     unsigned char buffer[contentLength+7];
                     buffer[0] = 0X55; 
                             buffer[1]=0x66; 
                             buffer[2]=0x77;  
                             buffer[3]=0x88; 
                             buffer[4]=0x44;//print command
                    for (int i=5;i<contentLength+5;i++)//add print content
                    {
                   buffer[i] =[printContent.text characterAtIndex:i-5];
                      }
                   buffer[contentLength+5]=0x0A;
                   buffer[contentLength+6]=0x0A;
                  [[session outputStream] write:(const uint8_t *)buffer maxLength:contentLength+7];//send print package
    
    
    
                    switcher2 =FALSE;
                }
    
    
    
                break;
            case NSStreamEventErrorOccurred:
                alertMessage = [[UIAlertView alloc] initWithTitle:@"ERROR:" message:@"NSSTream Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertMessage show];
                [alertMessage release];
    
                [self enableButton];
                [self addLabel:@"NSSTream error"];
                break;
            default:
    
                break;
        }
    }
    

    【讨论】:

    • 感谢您的回复 - 是的,我指的是 iphone 32 针连接器,并希望通过此连接发送和接收数据。所有的无线可能性都是不可能的。
    • 我的问题是如何将数据写入和读取来自 iphone 32 针连接器的传入数据流。
    • 这适用于任何 USB 设备吗?即使不是 MFI 设备?
    • 我试过这个并得到“使用未声明的标识符'EAAccessoryDidConnectNotification'”,我做错了什么,或者?
    • 嗨 Saurabh - 感谢您的代码 sn-p。您能否按照帖子中的建议发布示例项目?那将不胜感激。
    【解决方案2】:

    查看http://redpark.com

    他们出售 USB 数据线和 iOS SDK,用于向通过 USB 连接的设备读取/写入数据。

    我已经成功地使用它来将 iPad 连接到各种硬件。

    【讨论】:

    • 您是否曾在 App Store 上获得过具有此功能的应用程序获得批准?
    • 您确定 redpark.com 销售 USB 数据线吗?
    猜你喜欢
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多