Exemplo n.º 1
0
        protected override ObexPacket?OnClientRequest(ObexPacket clientRequestPacket)
        {
            Console.WriteLine("Opcode: " + clientRequestPacket.Opcode);

            if (clientRequestPacket.Opcode == Opcode.Put || clientRequestPacket.Opcode == Opcode.PutAlter)
            {
                string bodyString;
                if (clientRequestPacket.Headers.ContainsKey(HeaderId.EndOfBody))
                {
                    bodyString = ((BodyHeader)clientRequestPacket.Headers[HeaderId.EndOfBody]).Value !;
                }
                else
                {
                    Console.WriteLine("Recieved header dose not contains EndOfBody, abort! ");
                    return(null);
                }

                clientRequestPacket.PrintHeaders();
                Console.WriteLine("Body: " + bodyString);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(bodyString);
                string handle = doc.SelectSingleNode("/MAP-event-report/event/@handle").Value;
                MessageReceived?.Invoke(this, new MessageReceivedEventArgs(handle));

                return(new ObexPacket(Opcode.Success));
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send OBEX Connect packet to the server.
        /// </summary>
        /// <param name="targetUuid">A 16-length byte array indicates the UUID of the target service.</param>
        /// <exception cref="InvalidOperationException">The Connect method can call only once and it is already called before.</exception>
        /// <exception cref="ObexRequestException">The request failed due to an underlying issue such as connection issue, or the server reply with a invalid response</exception>
        public async Task Connect(ObexServiceUuid targetService)
        {
            if (Conntected)
            {
                throw new InvalidOperationException("ObexClient is already connected to a ObexServer");
            }

            ObexConnectPacket packet = new ObexConnectPacket(targetService);
            var buf = packet.ToBuffer();

            Console.WriteLine("Sending OBEX Connection request to server:");
            Console.WriteLine(BitConverter.ToString(buf.ToArray()));
            Console.WriteLine("Opcode: " + packet.Opcode);

            _writer.WriteBuffer(buf);
            await _writer.StoreAsync();

            Console.WriteLine("Waiting reply packet...");
            ObexPacket response = await ObexPacket.ReadFromStream(_reader, packet);

            var bytes = response.ToBuffer().ToArray();

            Console.WriteLine("Reply packet:");
            Console.WriteLine(BitConverter.ToString(bytes));
            Console.WriteLine($"ResponseCode: {response.Opcode}");
            response.PrintHeaders();

            if (response.Opcode != Opcode.Success && response.Opcode != Opcode.SuccessAlt)
            {
                throw new ObexRequestException($"Unable to connect to the target OBEX service.", response.Opcode);
            }

            if (response.Headers.ContainsKey(HeaderId.ConnectionId))
            {
                _connectionIdHeader = (Int32ValueHeader)response.Headers[HeaderId.ConnectionId];
            }
            Conntected = true;
        }