我可以通过WIFI-Direct连接Android和IOS吗?

时间:2014-04-09 13:05:55

标签: android ios wifi-direct

我想在Android和IOS之间传输文件 WIFI-DIRECT

Android有 WifiP2p图书馆

和IOS为 WIFI-DIRECT

MultiPear连接库

但两个图书馆之间不兼容!!!

彼此无法找到网络服务

如何连接Android和IOS以传输文件????

1 个答案:

答案 0 :(得分: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];
}

您必须调用此方法来启动Stream。

要从此流中发送内容,这是一个示例。

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