gpt4 book ai didi

android - 我可以通过 WIFI-Direct 在 Android 和 IOS 之间连接吗?

转载 作者:行者123 更新时间:2023-11-29 12:48:45 24 4
gpt4 key购买 nike

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

Android 有WifiP2p 库

IOS 有 MultiPear 连接库 用于 WIFI-DIRECT

但是它在两个库之间不兼容!!!

彼此找不到网络服务

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

最佳答案

你必须使用 NSInputStreamNSOutputStream 来连接 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;
}
}
}

关于android - 我可以通过 WIFI-Direct 在 Android 和 IOS 之间连接吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com