USB-Programming on iPhone

18,606

Solution 1

MFI membership is not needed for software developers its only needed for hardware developer. Also what USB port are you referring? are you referring the iPhone's 32 pin connector? You can always send data to hardware device using bluetooth.

update 1 -

First of all Sorry the code may look complex I am pasting this from my current project. Post comment if you want a sample project.

first you need to register for notifications in view did load-

[[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];

Then you should find the attached accessories -

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

Then check this is the correct accessory you are looking for -

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

Then open a session -

//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];


}

Then the stream delegate will be called -

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

Solution 2

check out http://redpark.com

they sell an USB cable and iOS SDK for reading/writing data to a device connected via USB.

i have used it successfully to interface the iPad to various hardware.

Share:
18,606
user741330
Author by

user741330

Updated on June 09, 2022

Comments

  • user741330
    user741330 almost 2 years

    all i want to do is to sent/receive data over the USB Port. With the official SDK it is not possible - apart from MFi.

    Which other methods to communicate wired with other devices are possible and would you propose?

    Many Thanks in advance!

    GD

  • user741330
    user741330 almost 13 years
    thanks for reply - yes i refer the iphone 32 pin connector and want to send and receive data over this connection. all wireless possibilities are out of the question.
  • user741330
    user741330 almost 13 years
    My Question is how can i write data to and read incomming datastreams from the iphone 32 pin connector.
  • elsurudo
    elsurudo over 10 years
    Did you ever get an app approved on the App Store with this included functionality?
  • douglasd3
    douglasd3 over 10 years
    Will this work for any USB devices? Even if not a MFI device?
  • SomeNorwegianGuy
    SomeNorwegianGuy about 10 years
    I tried this and got "Use of undeclared identifier 'EAAccessoryDidConnectNotification'", me doing something wrong, or?
  • Alex
    Alex about 10 years
    Hi Saurabh - Thanks for your code snippet. Would you be able to post a sample project as you suggested in your post? That would be greatly appreciated.
  • CAMOBAP
    CAMOBAP over 9 years
    Are you sure about redpark.com sells USB cable?
  • ekscrypto
    ekscrypto about 9 years
    You can read the official Apple documentation on EASession and using external hardware on iOS: developer.apple.com/library/ios/documentation/ExternalAccess‌​ory/…