- Platform: iOS 7
- Architecture: ARMv7, ARMv7s
Getting the files:
- Create a new project in XCode.
- In your project folder, clone the repository recursively:
- git clone --recursive https://github.com/ase-lab/SoD_iOS_Library.git SOD
- This will clone SOD files and socket.io obj-c (as well as SocketRocket support files)
- In XCode, right click on the project in the file view and choose "Add Files to ..."
- Select the newly cloned SOD folder, and click Add.
- In your project target, add the following libraries if they don't already exist:
- At this point, your project should build although SoD is not being imported yet.
Using SoD in your project:
- Add reference to SoD in your ViewController.m:
- #import "SOD.h"
- #import "SocketIOPacket.h" (optional, if you want to implement didReceiveEvent() and parse the raw data packete yourself, rather than use the provided event notifications)
- Create an instance of SOD:
- @property (nonatomic, strong) SOD *SOD;
- To connect to the server, add (perhaps under viewDidLoad):
- self.SOD = [[SOD alloc] initWithAddress:@"192.168.20.60" andPort:3000];
- IMPORTANT: Change the IP address to the IP address of your server.
- If your server is running, your device should connect and push orientation data automatically.
Note: If the device disconnects, it can reconnect by calling:
- [self.SOD reconnectToServer];
Sending a request to server
There are four main types of requests sent to the server:
- Send information to server: These requests tell the server about the device.
- sendDeviceInfoToServer (device height, width, type, etc.)
- sendOrientation (angle the device is facing - use calibrateDeviceAngle to set current angle to 0)
- setPairingState (device is switching to pairing mode)
- tryPairWithID (device wants to pair with a person)
- unpairDevice (device wants to unpair)
- Get information from server: These requests get information stored on the server.
- getDevicesWithSelection (get a list of all devices in the selection, eg. "inView")
- getAllTrackedPeoplewithCallBack (get a list of all people tracked by server)
- Send information to a device: These requests provide a selection for the server to determine which devices will receive the information ("all", "inView", etc.)
- sendString (send a string to specified device or devices)
- sendDictionary (send a dictionary to specified device or devices)
- Request information from a device
- requestDataWithRequestName (ask for some data with "requestName", from all devices in selection, eg. "all", inView", etc.)
When your device receives data, it raises an event. These events are sent by NSNotificationCenter, via defaultCenter. To handle these events, add a listener:
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stringReceivedHandler:) name:@"string" object:nil];
In the above example, we add a listener for the "string" event. Whenever another device or the server sends an event called "string" (eg. socket.emit("string", "some sample string") ), stringReceivedHandler is called.
Here is an example of a handler:
- (void)stringReceivedHandler: (NSNotification*) event
NSDictionary *theData = [[event userInfo] objectForKey:@"data"];
NSLog(@"String received: %@", [theData objectForKey:@"data"]);
When raising events with NSNotificationCenter, arguments are stored in userInfo, which is passed with the event. In the above example, theData is the dictionary received by the device. A typical dictionary received will contain three objects, ie. "name", "data", and "PID".
- name: event name
- data: the actual data sent
- PID: message ID, used for sending back replies (acknowledgements)