4

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

Dan
  • 872
  • 10
  • 24
  • Maybe have a look at this library, supports `UDP` and might help you rule out if it is a beta bug. https://github.com/robbiehanson/CocoaAsyncSocket Also refer to this, might only be valid for `WatchKit` and not `watchOS` http://stackoverflow.com/questions/28905131/is-there-an-activityindicator-in-watchkit-for-apple-watch/28931587#28931587 – sbarow Aug 04 '15 at 12:24
  • You could use WatchConnectivity to stream the AppleWatch CoreMotion data to the phone and let the iPhone send the data to the server. Check this out how to use WatchConnectivity http://stackoverflow.com/questions/31457811/send-messages-between-ios-and-watchos-with-watchconnectivity-in-watchos2/31457812#31457812 This is ofc a workaround until the bug(?) is fixed. Maybe it suffice for a hackathon project – Philip Aug 04 '15 at 14:09
  • @sbarow, as I've said, it's a hackathon project, so I'm not afraid at the "brief" window, since it does not apply here (and also, I don't care much if some measurements will fail sending), but I will definitely check that library. Thanks! – Dan Aug 04 '15 at 14:16
  • @Philip - This is my Plan B, but I'm afraid of big latency with that solution. – Dan Aug 04 '15 at 14:17
  • I can, unfortunately, confirm that neither AsyncUdpSocket nor GCDAsyncUdpSocket (from CocoaAsyncSocket) send UDP data from the watch. Works on the simulator, but not on the watch itself. – Dan Aug 04 '15 at 17:36
  • 5 years later, WatchOS 6, with claims of running standalone apps, is yet unable to send a simple UDP, while it works in the simulator. – Unknown Apr 27 '20 at 01:23

1 Answers1

0

Well, as of WatchOS 2 Beta 5, there still isn't any way of sending rapid information from the watch.

I did found a workaround (with limitations that make it not suitable for production) - though non of the methods mentioned in the comments to the original post were able to pass information with high speed, I've noticed the logs (posted through NSLog()) do get very fast to the Xcode console window. I guess Apple uses a low level bluetooth communication system to pass these messages.

I've written a short script to read these logs from Xcode, thus getting data from the watch in near real-time. From the mac I could send it wherever I wanted (and even process it on the way if I wanted to). The solution and it's limitations are described here - Reading Apple Watch syslog (NSLog()) in real-time.

Community
  • 1
  • 1
Dan
  • 872
  • 10
  • 24