I am a newbie to RXKotlin/RXJava.
I am developing the background service in Android.
In my service, I have
Bluetooth socket
TCP Socket
Whenever the data is available on the Bluetooth socket, read and write to the TCP socket. And whenever data is received in the TCP socket, write to the Bluetooth socket.
Can someone help me:
how to achieve this using Observables?
how to exchange the socket id information?
how to exchange the data?
Thanks
Please try using RxSubjects (https://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f)
Let me take PublishSubject as an example here.
//a publish subject which publishes int values
public PublishSubject<Integer> source = PublishSubject.create();
source.onNext(1);
source.onNext(2);
So above lines of code goes in Bluetooth socket class.
Now in TCP socket class, using the source, you can observe here.
source
.subscribe(
{
//result
},
{
//error
}
)
Thats it.
Please make sure, the subscription happens before Bluetooth socket starts publishing data.
Related
I'm still new to Android studio and I'm having a lot of trouble with Bluetooth, I'm currently making an app where I need to send data between devices. I'm letting the phone's default Bluetooth setup do the pairing but now I need to know how to send the data, I know I need to use input and output streams but I don't know exactly how.
And yes I have searched all over Google, I've followed a lot of Bluetooth tutorials but none of them seem to really explain how to send data from one device to another.
Thanks in advance.
After you establish a secure/insecure connection via bluetooth the rest is just socket programming simply. That is lets think about sending a text. We convert the text to byte and send that by Java OutputStream. In the same manner for the data received we can get it by InputStream.
But remember you need to maintain bunch of code and thread/handler to maintain state and others. Though the basic thing is simply socket programming over Bluetooth socket using the Bluetooth adapter. Have a look at the below repository in github. This creates a chatroom over bluetooth. i.e it sends and receives string data
https://github.com/zahansafallwa/Android-bluetooth-chat-with-emoji/tree/master/app/src/main/java/com/zahan/safallwa/donttalk
Specially have a look at the BluetoothChatService class. That contains codes related to sending data. BluetoothChatService
Edit:
As per your comment lets think that your devices are paired and also connected. Now you only need to send the text. Declare a outputstream
private final OutputStream mmOutStream;
Suppose you have a string. We convert it to byte. Then get our socket outputstream and send data by write() method
String message="this is test data";
byte[] send = message.getBytes();
mmOutStream = socket.getOutputStream(); // here socket is the bluetooth socket you establish
mmOutStream.write(send);//this is what sends the message
Remember:
Edited code is for your understanding only. It is prescribed to use separate thread for sending data.
I'm using autobahn android web socket library to build my web socket client application.
In order to keep the web socket connection alive for a long time, I need to periodically send ping messages from the client.
How can I do that with auotbahn android web socket library?
AutobahnAndroid's WebSocketWriter supports client pong messages, and AutobahnPython claims the following:
explicit processing of Pings/Pongs is unnecessary normally - AutobahnPython will do the right thing under the hood.
However, during execution of a simple chat client-server setup, I observe no such ping/pong heartbeat messages.
From what I have read, it is unclear from both the documentation and the source, what the full requirements are for enabling implicit Autobahn* heartbeats.
My personal solution involves issuing a ping from the server during onConnection and the appropriate onMesssage, and something similar to the following client code onMessage:
if (payload.equals("ping from the server"))) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
myWebSocket.sendTextMessage("pong from the client");
}
}, 1000L);
}
... which schedules a pong response to be sent by the client, 1 second after receiving a ping from the server.
You can use the pull request that I just submitted
https://github.com/tavendo/AutobahnAndroid/pull/67
which adds sendPingMessage(byte[] payload) and onPongMessage(byte[] payl).
I've read the discussion, and now I am working on my own simple Android firewall. Here is the algorithm they used.
The discussion
I was able to forward packets. If I remember correctly I just needed to read the TCP header and open another socket to that destination and send it headerless.
So now I need to write data to my Wi-Fi or 3G network interface if I understand them correctly. How to do that?
I solved it. I only needed to protect the socket which I'd like to use to send data directly to the server without using the TUN device.
Socket socket = socket = SocketChannel.open().socket();
if (!protect(socket)) {
Log.e(Constants.TAG, "Failed to protect the socket");
}
The documentation
I am using Android Autobahn client for communication with a web socket server. I need to know how the client detects tcp connection loss. Does it send TCP heart probes to detect this?
PS: error code : de.tavendo.autobahn.WebSocket.ConnectionHandler.CLOSE_CONNECTION_LOST
According to the source code, the CLOSE_CONNECTION_LOST error code is emitted when the endpoint calls close with an abnormal close code:
final int tavendoCloseCode = (close.mCode == 1000) ? ConnectionHandler.CLOSE_NORMAL : ConnectionHandler.CLOSE_CONNECTION_LOST;
I don't know exactly how your router is implemented, but in the source code, you have also a Ping / Pong implementation.
So you can expect that the connection loss is detected by sending some Ping / Pong WebSocket.
In my application i want to share file between a server and client, for that purpose i want to create two socket connection in one thread.
For example at server we have thread "T" and it has opened two socket like "1234" and socket no "3456" now on the client thread "T2" i will try to connect with these sockets.
So how i should implement it.
server:
thread
{
wait at socket "1234"
wait at socket "3456"
}
CLIENT SIDE:
Thread
{
connect with socket no "1234"
connect with socket no "3456"
}
if you have well implemented protocol(ie. each socket will send the data in a definite order), yeah one thread should work. but you can do this using only one socket connection too. what is the main point of creating two sockets? and what is the relation with android?
in addition; you definitely have to elaborate your question, otherwise it seems the question will be closed soon.