I'm attempting to connect to a device which is broadcasting it's own SSID and has a tcp server at a known ip address port. This device does not have internet access - the goal is to connect to this device and use tcp to send it some wifi credentials so that it can join a network which has internet.
Here is my attempt to connect to the server (once the phone is on the SSID it is broadcasting):
public func startTCPConnection(ipAddress: Network.NWEndpoint.Host, port:Network.NWEndpoint.Port) {
let tls = NWProtocolTLS.Options()
sec_protocol_options_set_verify_block(tls.securityProtocolOptions, { (_, _, completionHandler) in
completionHandler(true)
}, .main)
let tcp = NWProtocolTCP.Options()
let params = NWParameters(tls: tls, tcp: tcp)
let endpoint = NWEndpoint.hostPort(host: ipAddress, port: port)
params.requiredInterfaceType = .wifi
tcpConnection = NWConnection(to: endpoint, using: params)
tcpConnection?.stateUpdateHandler = { newState in
print("tcpConnectionStateUpdate: \(newState)")
switch newState {
case .ready:
print("Successfully connected to tcp server")
default:
break
}
}
tcpConnection?.pathUpdateHandler = { path in
print("path: \(path.debugDescription)")
}
tcpConnection?.start(queue: .main)
}
I am finding that if the phone's cellular network is enabled, the connection will never work. Console looks like this:
path: satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns
2022-07-27 14:57:14.599846-0600 TCPClient[74168:6075102] [] nw_path_evaluator_create_flow_inner NECP_CLIENT_ACTION_ADD_FLOW 39C2C653-87D4-482E-9CD0-3329955086AD [65: No route to host]
2022-07-27 14:57:14.599939-0600 TCPClient[74168:6075102] [connection] nw_endpoint_flow_setup_channel [C1 192.0.0.1:9002 in_progress channel-flow (satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns)] failed to request add nexus flow
tcpConnectionStateUpdate: preparing
tcpConnectionStateUpdate: waiting(POSIXErrorCode(rawValue: 65): No route to host)
However, as soon as I toggle off the phone's cellular network, connect succeeds:
path: satisfied (Path is satisfied), interface: en0, scoped, ipv4, dns
tcpConnectionStateUpdate: preparing
tcpConnectionStateUpdate: ready
Successfully connected to tcp server
I've tried many different combinations of NWParameters, to no avail. Any hints as to what I'm doing wrong? Is it not possible to accomplish what I want to do with NWConnection?
Selecting any option will automatically load the page