简体   繁体   English

通过wifi使用gcdasyncsocket发送和接收键/值对

[英]Send and receive key/value pair using gcdasyncsocket over wifi

I've made an iOS app that receives RSSI values from a BLE Beacon and sends (using GCDAsynSocket) it to a connected MacBook over wifi. 我已经制作了一个iOS应用程序,该应用程序从BLE Beacon接收RSSI值,并通过wifi将其发送(使用GCDAsynSocket)到连接的MacBook。 My next app on the MacBook receives the RSSI values and saves (using NSOutpuStream class) it in .txt format. 我在MacBook上的下一个应用程序接收RSSI值,并将其保存(使用NSOutpuStream类)为.txt格式。 Now, I need to send RSSI values from eight different BLE beacons and their respective MAC address in key/value pairs. 现在,我需要以键/值对的形式发送来自八个不同BLE信标的RSSI值及其各自的MAC地址。 I tried to solve it using NSKeyedArchiver to encode NSDictionary into NSData. 我试图使用NSKeyedArchiver将NSDictionary编码为NSData来解决它。

NSData *data = [NSKeyedArchiver archivedDataWithRootObject: NSDictionaryContaningKeyValuePair];

And on the receiver's end (MacBook), I used NSUnarchiver to decode. 在接收器端(MacBook),我使用NSUnarchiver进行解码。 I am unable to extract the NSDictionary containing key/value (MAC address/RSSI values) pair and store it to the .txt file. 我无法提取包含键/值(MAC地址/ RSSI值)对的NSDictionary,并将其存储到.txt文件。 Moreover, since NSoutputStrteam write method takes nonnull const uint8_t value, how can I write the Key/value pair in the .txt file? 此外,由于NSoutputStrteam写入方法采用非空const uint8_t值,如何在.txt文件中写入键/值对? I use Objective-C and Xcode (7.0). 我使用Objective-C和Xcode(7.0)。

Thank you. 谢谢。

Got the solution. 得到了解决方案。 NSDictionary can be sent using NSJSONSerialization class. 可以使用NSJSONSerialization类发送NSDictionary。 At transmitter's end (iOS): 在发射器端(iOS):

NSDictionary* dictInfo = [NSDictionary dictionaryWithObjectsAndKeys:self.txtInfo.text,@"data", nil];
NSData* dataDict = [NSJSONSerialization dataWithJSONObject:dictInfo options:NSJSONWritingPrettyPrinted error:nil];
[self.socket writeData:dataDict withTimeout:-1.0f tag:0];

At receiver's end (MacBook): 在接收方的终端(MacBook):

 if ([self getSelectedSocket]== sock) {
    [_dataBuffer appendData:data];
    if ([sock socketAvailableBytes] == 0) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_dataBuffer options:NSJSONReadingMutableLeaves error:nil];
        DLog("Dictionary Info: %@", dict);
        NSString* strInfo =  (NSString*)[dict objectForKey:@"data"];
        [_dataBuffer setLength:0];
        self.txtLogs.stringValue= strInfo;

For further information please visit https://github.com/boobalaninfo/Bonjour-iOS-MAC-Apps . 有关更多信息,请访问https://github.com/boobalaninfo/Bonjour-iOS-MAC-Apps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM