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?
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:
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?