2014-04-09 95 views
1

我想轉移Android和iOS之間的文件通過WIFI-DIRECT我可以通過WIFI-Direct連接Android和IOS嗎?

的Android有WifiP2p圖書館

和IOS有MultiPear連接庫WIFI-DIRECT

BUT它在兩個庫之間不兼容!

對方無法發現網絡服務

如何Android和iOS之間的連接來傳輸文件????

+0

你不能這樣做。 – Petar

回答

1

您必須使用NSInputStream & NSOutputStream才能與android連接。這不是一個簡單的任務。

首先,

@property (nonatomic, strong, readwrite) NSInputStream *inputStream; 
@property (nonatomic, strong, readwrite) NSOutputStream *outputStream; 

然後加入這種方法,

- (void)initNetworkCommunication { 

    uint portNo = 5555; 
    CFReadStreamRef readStream; 
    CFWriteStreamRef writeStream; 
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", portNo, &readStream, &writeStream); 
    inputStream = (__bridge NSInputStream *)readStream; 
    outputStream = (__bridge NSOutputStream *)writeStream; 

    [inputStream setDelegate:self]; 
    [outputStream setDelegate:self]; 

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [inputStream open]; 
    [outputStream open]; 
} 

你必須調用此方法來啓動流。

而從這個流發送的東西,這裏是一個例子。

- (IBAction)photoClick:(id)sender { 

    NSData *newData = UIImagePNGRepresentation([UIImage imageNamed:@"s.jpg"]); 
    int index = 0; 
    int totalLen = [newData length]; 
    uint8_t buffer[1024]; 
    uint8_t *readBytes = (uint8_t *)[newData bytes]; 

    while (index < totalLen) { 
     if ([outputStream hasSpaceAvailable]) { 
      NSLog(@"Sending"); 
      int indexLen = (1024>(totalLen-index))?(totalLen-index):1024; 

      (void)memcpy(buffer, readBytes, indexLen); 

      int written = [outputStream write:buffer maxLength:indexLen]; 

      if (written < 0) { 
       break; 
      } 

      index += written; 

      readBytes += written; 
     } 
    } 
} 
相關問題