I want to develop an iOS app which should be able to send messages to other WiFi direct enabled devices (no matter if it is an iOS device or Android device) and receive messages from them.
I found a suitable library ZeroMQ which can be used to accomplish the task. I have compiled the library for iOS successfully.
I reffered szehnder's sample code but the app doesn't do anything. It shows a blank table view only. I checked in the code and found that the code to receive a message is present there but the method to send the message is empty.
How can we detect other WiFi direct devices? Send and receive messages to them? Any pointers are highly appreciated.
As suggested by Jason, I am adding whatever I have achieved till now: I have an android app which connects to other android devices using WiFi direct and sends and receive messages from them, and uses ZeroMQ. I need to communicate with that app with my iOS app.
Below is my code to receive the messages:
-(void) setupSockets:(ZMQContext*)myContext {
ZMQSocket *subscriber = [myContext socketWithType:ZMQ_SUB];
[subscriber setData:nil forOption:ZMQ_SUBSCRIBE];
[subscriber connectToEndpoint: [NSString stringWithFormat:@"tcp://%@:%@", @"192.168.49.1", @"26665"]];
while (1) {
NSData *m = [subscriber receiveDataWithFlags:0];
if (m!=NULL) {
NSString *s = [NSString stringWithUTF8String:[m bytes]];
NSLog(@"Message: %@", s);
[self performSelectorOnMainThread:@selector(postMessageNotification:) withObject:s waitUntilDone:NO];
}
sleep(3);
}
}
Here 192.168.49.1 is the ip address of the host android device and 26665 is the port number used to receive messages. The control reaches up to call of receiveDataWithFlags: successfully but does nothing after that.
Now to send the data:
-(IBAction) sendMessageClicked:(id)sender {
NSString *pubEndPoint = [NSString stringWithFormat:@"tcp://%@:%@", @"192.168.49.1", @"26666"];
ZMQSocket *pubSocket = [objMeeps.context socketWithType:ZMQ_PUB];
[pubSocket connectToEndpoint:pubEndPoint];
NSData *msg = [@"hi" dataUsingEncoding:NSUTF8StringEncoding];
[pubSocket sendData:msg withFlags:ZMQ_NOBLOCK];
}
The method gives an error message ZMQLogError: *** <ZMQSocket: 0x17405fdd0 (ctx=0x174029480, type=ZMQ_PUB, endpoint=tcp://192.168.49.1:26666, closed=0)>: zmq_send: Resource temporarily unavailable
. When I searched regarding this error I got that the error is occurring due to absence of any receiver but my iPhone is connected to the WiFi network created by the android host using WiFi Direct.
I am new to ZeroMQ so might be missing something very small. Hope this clarifies the issue enough.Please guide me regarding this.
So you say tcp://192.168.49.1:26666 is the address of your Android host, but so far as I can tell you're trying to publish and subscribe from your ios device, at least for this example. So, let's be explicit: for this example, ignore your Android device and just try to get your ios application talking to itself.
Step 1: use the ip:port for your ios device
ZMQ has two sides to every connection, one side that "binds" (and could be considered the server) and one side that "connects" (and could be considered the client). Your choices are flexible, but for the sake of example it's probably best for your publisher to bind and your subscriber to connect. Right now, you have both sides connecting. This is likely the cause of the error you're seeing.
Step 2: change your pub socket to bind on its address rather than connect
When using PUB/SUB, you must subscribe to the "topics" you want. In your example code you're subscribing to "NASDAQ". For you to receive messages, the text of the message must begin with your subscriber topic, for instance "NASDAQ:hi" would get through your subscription filter.
Step 3: change your zmq_subscribe option to subscribe to an empty string "" to get every message that comes through
Making those three changes should put you on the right track. I highly suggest you read the guide, it covers these sorts of basic usage guidelines and has a fair body of ios example code to boot.