UDP パケットを wifi モジュールに送信して受信すると、シリアル ポート モニターで正常に監視できますが、iOS アプリでは受信できません。
接続と受信の両方にNetwork Frameworkを使用しています。接続部分は完全に機能しますが、リスナーをポートに設定し、rsponse を取得しようとするとreceiveMessage
、モジュールが送信するアウトパケットが表示されますが、呼び出されません。
class func connect()
{
connection = NWConnection(host: hostUDP, port: portUDP, using: .udp)
connection?.stateUpdateHandler =
{
(newState) in switch (newState)
{
case .ready:
//The connection is established and ready to send and recieve data.
print("ready")
self.sendPaket(self.sendingPacket)
self.receive()
case .setup:
//The connection has been initialized but not started
print("setup")
case .cancelled:
//The connection has been cancelled
print("cancelled")
case .preparing:
//The connection in the process of being established
print("Preparing")
default:
//The connection has disconnected or encountered an error
print("waiting or failed")
}
}
connection?.start(queue: Protocols.sendQueue)
}
class func sendPaket(_ packet:String)
{
let packet = dataWithHexString(hex: sendingPacket)
print("converted version to byte is :\(packet)")
connection?.send(content: packet, completion: NWConnection.SendCompletion.contentProcessed((
{
(NWError) in
print("error in sending packet : \(String(describing:NWError))")
})))
}
class func receive()
{
connection?.receiveMessage(completion:
{
(data, context, isComplete, error) in
print("Got it")
if data != nil
{
print("context:\n")
print((context?.protocolMetadata)!)
Protocols.receivedPacket = data?.dataToHexEncodedString()
print("received packet:\n")
print(Protocols.receivedPacket!)
let rec = Test()
rec.label.text = Protocols.receivedPacket
}
print("receive error: \(String(describing: error))")
})
}