google / coapblaster

A Java Library for implementing CoAP clients and servers.
Apache License 2.0
35 stars 13 forks source link

Client-Server app doesn't work on Android. #6

Open micrcx opened 3 years ago

micrcx commented 3 years ago

I tried to test CoapBlaster on my phone. I wrote a simple application with two buttons: the first creates and starts the server, the second creates a client and tries to get a response:

public class MainActivity extends AppCompatActivity {
    final String TAG = "coap";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button server = findViewById(R.id.server);
        server.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startServer();
            }
        });

        final Button client = findViewById(R.id.client);
        client.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClient();
            }
        });
    }

    private void startClient() {
        try {
            LocalEndpointManager manager = new LocalEndpointManager();
            Client client = new Client(manager, "coap://127.0.0.1/test/hello");
            Transaction transaction = client.newRequestBuilder().send();
            Message response = transaction.getResponse(5000);
            Log.e(TAG, "Got response: " + response.getPayloadAsString());
        }
        catch (Exception exception) {
            Log.e(TAG, "Client.Exception->" + exception.toString());
        }
    }

    private void startServer() {
        try {
            LocalEndpointManager manager = new LocalEndpointManager();
            Server server = new Server(manager);
            LocalEndpoint udpEndpoint = manager.getLocalEndpointForScheme(Coap.SCHEME_UDP);
            server.addLocalEndpoint(udpEndpoint);
            Resource<InboundRequestHandler> rootResource = new Resource<>();
            Resource<InboundRequestHandler> testFolder = new Resource<>();
            InboundRequestHandler helloHandler = new InboundRequestHandler() {
                public void onInboundRequest(InboundRequest inboundRequest) {
                    inboundRequest.sendSimpleResponse(Code.RESPONSE_CONTENT, "Hello, World!");
                }
            };
            testFolder.addChild("hello", helloHandler);
            rootResource.addChild("test", testFolder);
            server.setRequestHandler(rootResource);
            server.start();
            Log.e(TAG, "Server started");
        }
        catch (Exception exception) {
            Log.e(TAG, "Server.Exception->" + exception.toString());
        }
    }
}

Result: 2020-10-21 13:24:04.466 17315-17315/com.mk.testcoap2 E/coap: Server started 2020-10-21 13:24:12.968 17315-17315/com.mk.testcoap2 E/coap: Client.Exception->java.util.concurrent.TimeoutException What's wrong?

a1573595 commented 3 years ago

Same issue on Android and JVM device, but Client is OK.