For a project I'm doing on the Apple Watch, I'm looking for a way to send streams of data to a server on the local network. This is rapid, online data, so I preferred it would be sent over UDP (but it wasn't a strict requirement for me). The data is the current accelerometer reading from the watch, which is read every fraction of a second.
I'm using WatchOS 2 (Beta 4), iOS 9 (Beta 4) (and the latest beta of Xcode 7).
I've used the following code:
- (void) sendMsg: (NSString *)msg{
int socketSD = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (socketSD <= 0) {
NSLog(@"Error: Could not open socket.");
return;
}
// set socket options enable broadcast
int broadcastEnable = 1;
int ret = setsockopt(socketSD, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
if (ret) {
NSLog(@"Error: Could not open set socket to broadcast mode");
close(socketSD);
return;
}
// Configure the port and ip we want to send to
struct sockaddr_in broadcastAddr;
memset(&broadcastAddr, 0, sizeof(broadcastAddr));
broadcastAddr.sin_family = AF_INET;
inet_pton(AF_INET, SERVER_IP, &broadcastAddr.sin_addr);
broadcastAddr.sin_port = htons(SERVER_PORT);
char *request = "Message from Watch";
ret = sendto(socketSD, request, strlen(request), 0, (struct sockaddr*)&broadcastAddr, sizeof(broadcastAddr));
if (ret < 0) {
NSLog(@"Error: Could not open send broadcast.");
close(socketSD);
return;
}
}
- (IBAction)watchGoButtonTouched {
[self sendMsg:@"Hi"];
}
Which succeeds in getting the message to the server in the Watch simulator, but when ran on the actual watch, I get the error Error: Could not open send broadcast.
.
I should mention that the same code works well on the iPhone that the watch is connected to.
Due to these reasons, I'm afraid the above code fails because of beta issues, out of the realm of my code. I would love to know if you think otherwise.
If it is a beta issue, I cannot wait for it to be fixed, so I wanted to ask here for any idea as to how to send data that conforms to the following points:
- Basically, a way to send traffic directly from the watch to a UDP server on the local network.
- The data has to be sent at the moment it is received (so concatenating multiple data points is not an option)
- UDP is preferable, but any other way of sending streams of data with low latency is good.
- This is a hackathon project, that is a proof-of-concept for a really cool idea. Meaning, AppStore compliance or battery issues do not apply here.
Any help will be much appreciated!
Thanks a lot :) Dan