Peer-to-peer communication between iOS devices - iphone

I am trying to prototype a solution to a problem and am currently exploring multiple routes I could try. Is it possible for one iOS device, running a certain app, to communicate directly with another iOS device, running the same application - without the need to be on the same LAN?
Solutions I am currently investigating are using Bluetooth and ad-hoc wireless connections.
Ideally, the application when installed would ask the user for the required permissions, and then would accept and/or send data to/from another client after a handshake had happened.
My concern with Bluetooth is that 'pairing' would need to happen with every device, rather than happen in the background once the user has installed the app. I have a feeling what I am talking about isn't possible from what I've been reading elsewhere on Stackoverflow.

Take a look at Bluetooth Low Energy.
https://developer.apple.com/library/ios/#samplecode/BTLE_Transfer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012927
Here is another example,
https://github.com/KhaosT/CBPeripheralManager-Demo
You might also want to look into GameKit and peer-to-peer connectivity there.

I can't tell you anything about it, but you might try looking at iOS 7. If that's an option, I'd take a look. Can't talk about what it is because of NDA though.

Depending on what you need to communicate, you could try checking out this project, which lets you share arrays of strings between iOS devices over Bluetooth LE.
You don't need to "pair" the devices and it can still communicate while the app is in the background. SimpleShare
Hope it helps!

From the documentation of MultipeerGroupChat:
MultipeerGroupChat sample application utilizes the Multipeer Connectivity framework to enable nearby users to discover, connected, and send data between each other. This sample simulates a simple chat interface where up to 8 devices can connect with each other and send text messages or images to each other. Here you will learn how to bring up framework UI for discovery and connections and also how to monitor session state, listen for incoming data and resources, and send data and resources.
This is an excellent example at developer.apple.com here is the link
https://developer.apple.com/library/ios/samplecode/MultipeerGroupChat/Introduction/Intro.html
Also this tutorial from Ralf Ebert demonstrates how to use Multipeer Connectivity framework for peer communication should help you.
https://www.ralfebert.de/tutorials/ios-swift-multipeer-connectivity/

Related

Communication between two iOS devices

I am looking for a way to have one iPhone app send a message to another app on a different phone (sort of like a Sender-Receiver set up). I am looking for the best possible way to do this. Does anyone have any ideas and/or tutorials?
Thanks for the help.
You should use GameKit. It is super easy to send messages between two iOS devices using it. Here's a great tutorial: Game Kit. You can also get more information about it here from the docs: About Game Kit.
You communicate by creating an ad-hoc bluetooth or local wireless network.
lmirak provided insightful info about device communication(especially about GameKit). I would like to add one more solution. You can use WiFi network to do your device communication.
See the link or download the sample application from developer.apple
The sample application named as WiTap. It demonstrates how to achieve network communication between applications. Using Bonjour, the application both advertises itself on the local network and displays a list of other instances of this application on the network.
If your app is only going to run on iOS, then you should use the fantastic MultipeerConnectivity library. https://developer.apple.com/documentation/multipeerconnectivity
If you need a solution that will work cross-platform, then one way to accomplish this is using sockets and connecting over a local network. On iOS you can use CocoaAsyncPods for sockets and NetService for discovery.
Here is a basic example app that does this: https://github.com/brendaninnis/LocalNetworkingApp
, which I explain in great detail here: http://brendaninnis.ca/connect-nearby-devices-part-1.html

iOS adhoc wifi sensor data

My iPhone connects over adhoc wifi to a wifi sensor module.
The challenge is to code an app that uses this sensor module. But I'm not sure what specific API's to use to best architect this implementation.
I've started looking into the CocoaAsyncSocket class as it seems to be an appropriate tool for such use.
Does the user always have to manually connect to the adhoc wifi device? (through the Settings app) or can my own app handle the searching, making, and breaking of the wifi connection?
I doubt iOS lets me programmatically toggle whether Wifi is on/off.
Once the sensor data is being received, what container would best handle the stream?
For example, on other platforms, I coded a rotating queue buffer.
Thanks for your input.
Edit: The protocol in question is straight CSV formatted ASCII. Not HTTP, FTP, etc. Just raw data. The app is to simply open a port on the connected IP, and read/write.
Your application cannot turn wifi on/off, or select a wifi network.
Without more information on what protocols this wifi sensor module speaks, it will be impossible for anyone to give more than vague recommendations. If the module can serve data over an http connection, that would probably be ideal. If it requires your software to open a connection on a specific port and communicate over something other than http or ftp, your job will be a bit more complicated. CFNetwork and projects derived from it's usage, such as CocoaAsyncSocket which you mentioned. You can see another implementation of an HTTP connection over CFNetwork in ASIHTTPRequest, that may help as a reference for handling download streams, queuing operations, etc.
As for storing the data, again it's hard to give any concrete recommendations without more information. If you want to store the data to the filesystem of the iOS device, NSData will probably meet your needs. If you need/prefer to use a queue for buffering data, there is a simple category on NSArray which provides queue semantics. The link to CHCircularBuffer in that SO article is dead, but this github project appears to have it.
edit: Here is the official version of the CHDataStructures. I don't know if it's an improvement over the previous link, but it appears to be updated for the more recent iOS SDK.
Add the SSID of your wifi ad hoc networks in settings. When you see the SSID of the network, click the arrow and choose connect automatically. After this, the phone will automatically connect whenever it comes in range. After this, the two devices can communicate using regular socket APIs.

App communication in nearby devices without user intervention

Is it possible to communicate two instances of an app:
In nearby devices (Android or iPhone)
Without user intervention (aside from starting the app once)
Without internet (but not network) connection?
How?
Android to Android?
iPhone to iPhone?
Android to iPhone and vice-versa?
Let us ignore the issues associated with implementing the network communication at the application level and focus on the system design problem your question raises.
You want an application on one device to connect to another associated application on another device without any user intervention/interaction beyond launching the device. In order for this to work your system is going to need a 3rd party separate from the two devices/applications that provides a lookup service.
What will need to happen is each application will need to communicate with this service, ask for the data regarding other device it should connect to, if it exists, and then attempt that connection. This is basically how a bittorrent tracker works.
Keep in mind that there's quite a bit more involved in the implementation than that simple scenario suggests. For example, the proper registering/unregistering of devices, registration timeouts, security, etc.
Unless I misunderstand your question, you should be able to just use standard sockets (TCP or UDP depending on your needs) for communication between any/all devices. For iPhone, you could probably use the NSStream classes and for Android you'd use the Socket classes.
Can the new NFC feature be an answer to this?

How to do peer-to-peer communication in an iPhone app?

I'm trying to write a simple chat application for the iPhone (as an experiment). Is there a simple way for two devices to discover each others' IP addresses, and given the addresses is there a simple API or protocol that would let me send text messages back and forth?
I've investigated SIP (specifically Sofia and eXosip), but these tools exist as C libraries and are beyond my current ability to port them to the iPhone.
Update: I'm trying to connect two devices over the Internet (i.e. not over Bluetooth or a local wireless network, which is what GameKit does).
You're going to need a server that provides the match making service. Game Center makes this pretty easy, but your users will have to have Game Center accounts.
Alternatively, you can set up an XMPP (formerly Jabber, it's what powers Google Chat) server (I've never done this, but there are several available) and use the XMPP Framework for Cocoa. There are instructions for using it in iPhone apps here.
I'm sure there are other chat servers and client source also available. IRC and Mobile Colloquy come to mind.
Finally, you could write your own server using your favorite server language / framework. This isn't too hard (I've done it myself), but it's far from what I'd call simple, and I wouldn't use it for a production system.
There is support for exactly this kind of ad-hoc peer-to-peer networking in GameKit. Have a look at the second half of the GameKit documentation for details:
http://developer.apple.com/library/ios/#documentation/...
NSNetService is a good option.
Take a look at WebRTC Datachannels. WebRTC is a newer option with native iOS support a standard that is still being finalized, but it is more flexible should the iOS app need to communicate with browser or even android peers

How can I send simple data from an iPhone to another iPhone, or an Android?

I'm just getting started with mobile development, and after considerable searching I'm still confused about sending a message from my iPhone to another or an Android. For instance, in building a simple Tic-Tac-Toe game, if I want to notify the other phone of a move, what is the best way to do so? Wifi, bluetooth, 3G? And how?
Many methods seem to point to communicating through a web server, but I'd rather send them directly from phone to phone if possible. (this is for native apps of course)
If you don't want to have a server (or even if you do! It depends!) it sounds like you will want to use TCP sockets. Apple doesn't have a generic bluetooth API to use with iPhone-to-android communication and you'll have TCP access anyway with 3G. A 3G network connection is very similar to wifi and you should be able to check for the difference as needed with little change to your overall networking code. I'm not sure if bonjour is supported on android, but I bet someone has ported it for similar reasons already, and it is supported in iOS. Apple has a networking section for iOS that you may find informative.
Additionally if you want to stay within the iOS device only realm you could use GameKit to communicate via bluetooth.
Hope that helps some.
The easiest way is to create a web server with an API and the clients poll the server for new moves (or the server pushes the client an update). This is the only way you're going to be able to do it over 3G unless you can figure out the IP addresses of both phones (do phones even keep the same IP address for a long period of time?) and open up sockets between them and let one phone be the server and the other be the client (could cause potential cheating).
WiFi is not always there - by limiting your users' options to areas with access points, you'll lose sales. Not sure about Bluetooth - how prevalent is it on handsets, how much of the API is exposed. Your best bet, IMHO, is 3G. With some kind of a central presence server (Web or otherwise), with optional proxying. Read up on peer-to-peer networking.
As far as i know does the iphone only support incoming messages over push notifications.
This means, that you have to update the information, by requesting it from web services etc.
The bluetooth API of the iphone doesn't support this either.