Socket connection from WatchOS 2 and CFStream - apple-watch

I have to connect a remote server via tcp socket from the Watch.
I have already written a piece of code using CFStream which works perfectly from the Watch simulator.
When I run it on the Watch I get this error:
The operation couldn’t be completed. Can't assign requested address (Code = 49)
when I try to open the connection to the server using CFStreamCreatePairWithSocketToHost.
The Apple documentation declares that CFStreamCreatePairWithSocketToHost is
Available in watchOS 2.0 and later.
so I expected that it had should work but I have found this Apple's engineer answer which confirms that there is no way to open a tcp connection from the Watch:
Socket communication does not work real Apple Watch
Can anyone clarify and help me to understand?
Supposing that I cannot open a tcp connection from the Watch, what kind of alternatives have I, since I cannot use [NSURLSession]?
I could use one of the communication methods provided by [WCSession] but do they work when the iPhone app either is not running or is not in foreground?

You could use WCSession's sendMessage APIs to wake the iOS app up in the background and have it do the Stream work for you.

Related

WatchOS establish server connection

I have a client - server IOS application. In addition, I need to write an application for it on WatchOS. So I have 2 questions:
When I send a command from Apple Watch, do I need to connect to the server from the watch or transfer information to the IOS application and connect to the server from the phone?
If I have to connect to the server with an IOS application, then how can I connect in background mode?
As an example, you can take any messenger, for example Telegram
If I reply to a message with AppleWatch, how is the message being sent to the server (Via iPhone or directly via AppleWatch)
For connection to the server, I use the "Starscream" framework
Maybe I don't understand something, but Apple made it impossible to establish a connection in background mode
I would be grateful if you tell me or provide examples and articles, so that I could understand what to do.
Apple Watch apps can connect directly to servers using URLSession ... no need to go via the phone.
Here is an example on using URLSession in Combine, which is the "latest and greatest" way of doing things.
Otherwise this example shows using it in a more conventional way, with the bonus of SwiftUI.
Otherwise

iPhone sometimes fails connecting to cc2564 (Bluetooth Low Energy)

We are developing an application where an iPhone should connect to an cc2564 device (Specifically cc2564+msp430f5438), the application should make quick and short connections to send some data. We need reliability in the connection so we need to know if the connection was really successfully or not, and we need a way to connect to the device with assurance.
The iPhone connects correctly to de device most times, it discovers services and works as expected, but sometimes the iphone makes the connection but the callback of discovered services is never called.
We used a sniffer to look at the communication packages and we saw that in those cases the connection package was send but there wasn't any response from the device, the phone tries to retry some version request messages and then stops, as you can see in the image:
The problem seems to be on the device, we are using the SPPLEDemo sample provided by Texas, and the first function that is called when the connection is established is GAP_LE_Evenet_Callback, and when the problem occurs it's never called. We don't know if somewhere inside the GAP API the device receive the connection message.
Is there some way to debug it or to know if the connection message is received by the device when the error occurs?
Is it a problem on the iPhone (unlikely), or is a problem on the device?
You quoted that
..the first function that is called when the connection is established is GAP_LE_Evenet_Callback, and when the problem occurs it's never called..
I'd recommend tweaking (playing) a bit with connection timeouts and Link Supervision Timeouts before consulting the experts who have developed the BLE Device firmware if that is possible for you.
Be assured that whenever a connection is made, the BLE device gets an event (callback) from the BLE stack that the connection has been established.
If that event is not handled the way it should be, then you can conclude that BLE device has some implementation issue.

Live connection Voip in Background for iPhone

I'm updating one voip application and one of my issue is supporting multitasking.
I don't know how I should manage multitasking. Specially I need define connection as VoIP, to receiving incoming call when app is in background, but I don't know how do that.
I searched and found This tutorial and a lot of other pages, but I could not found how those work.
Is any one able to explain me it?
Apple only allows you to set up a TCP connection that you can handle with a CFNetworkStream. You need to bind the socket to a CFNetworkStream and then set the handler to the kCFNetworkStream value that enables VoIP. You'll need to support TCP to handle the socket as Apple doesn't support backgrounding over UDP. The documentation provided by Apple is quite clear on what you can do.

SIGPIPE error in iOS4 when app is running background and lock screen

I use BSD socket in my app to send and receive data on iphone4(iOS4.1),there are three situations in my app:
app is running in foreground and screen locks, it's fine.
app is running in background and screen does't lock, it's fine too(I use NSStream to send and receive data for keep app alive in background,it works fine.)
app is running in background and screen locks, app running fine but always get the SIGPIPE error when app try to call sendto() or send() through UDP or TCP socket.
I search a lot but get nothing useful,from here i know what SIGPIPE is , and i use setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(int)) to avoid app exit abnormally. but i still don't know why this error occurred only in situation 3 and how to send data normally through bsd socket in this situation.
I have try to renew and reconnect but it does't work,it still get SIGPIPE error.
I think it maybe that all BSD sockets cannot get internal access in situation 3(As far as we know that BSD socket cannot activate wifi or EDGE connection in iOS),Anyone can help me or give me some suggestions? and sorry for my Broken English^.^,Thank you very much .
I search on google and found my own question 10 months before, that's interesting.
I already had a solution for application keep running in background , and setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(int)) is useful which will ignore Sigpipe 13 error .
this error happens when the socket already lost physical connection and still try to write data,through Wifi, 3G or Accessory.

How do you communicate to a Modbus device within an iPhone application?

I would like to be able to develop an iPhone application that can communicate with Modbus devices, but I'm not sure how to proceed. Has anyone had experience with this or are there existing libraries out there for this purpose?
You will need first to know how to make a simple TCP client on your iPhone, and a simple TCP server on your computer that your iPhone client will talk to via WiFi. Make sure PING command works and firewall does not block your server port before any client/server experiments, and make sure that server port for experimenting is greater then 1024 and not already used. Then take a look at MODBUS TCP specification from official site, get familiar with function codes you are interested in, and study C code found here. Then make a simple MODBUS TCP client that can send a request to MODBUS TCP server and interprete an answer. The best way is to start reading single register value, then progress in time and add more functions.
I do exactly all above points, my iOS Modbus lib works fine, application is now up on AppStore, PLCLink read and set any modBus data into my Wago home automation.
Installation guide : http://pautex.fr/plclink