2011-05-06 42 views
15

我想要做的就是通過USB端口發送/接收數據。 除了MFi,使用官方SDK是不可能的。iPhone上的USB編程

其他與其他設備進行通信的其他方法是可能的,您會提出建議嗎?

很多預先感謝!不需要爲軟件開發人員的,才需要進行硬件開發

GD

回答

15

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

感謝您的回覆 - 是的,我指的是iPhone 32針連接器,並希望通過此連接發送和接收數據。所有的無線可能性都不存在。 – user741330 2011-05-06 08:23:11

+0

我的問題是我如何寫數據並從iphone 32針連接器讀取數據流。 – user741330 2011-05-06 08:58:35

+0

檢查編輯答案 – Saurabh 2011-05-06 09:23:15

6

退房http://redpark.com

他們賣USB電纜和iOS SDK用於讀/寫數據,以通過USB連接的設備。

我已經成功地將iPad連接到各種硬件。

+1

您是否曾獲得應用程序批准在這個包含功能的App Store上? – elsurudo 2013-08-21 12:23:29

+2

您確定關於redpark.com銷售USB電纜嗎? – CAMOBAP 2014-08-30 19:23:41