Я пытаюсь отправить координаты со своего телефона на мой компьютер через usb. Я использую Socket.io для подключения сервера Node.js к клиенту android, а для подключения портов я использую adb reverse tcp:9002 tcp:9002
.
Вот как выглядит сервер Node.js:
const express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.get('/', (req, res) => {
res.send('Server is running on port 9002')
});
io.on('connection',(socket) =>{
console.log('Android Connected')
socket.on('join', function() {
console.log('Android Ready')
//res.send('Android just connected')
socket.broadcast.emit('userjoinedthechat', " : Android has joined ")
});
socket.on('message',(messageContent) => {
console.log(messageContent)
var numbers = messageContent.match(/\d+/g).map(Number);
var x = numbers[0];
var y = numbers[1];
console.log("Thats X: " +x+ "and Y: "+y);
});
socket.on('disconnect', function(){
console.log('Android has left');
})
})
server.listen(9002,()=>{
console.log('Node app is running on port 9002')
});
Это MainActivity Android клиента:
package com.example.calibrator;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int screenWidth;
private int screenHeight;
private IOSender io;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get Screenheight and Screenwidth of the Display
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
// set the Layout to fit the Screen
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
LayoutParams layoutParams = new LayoutParams(screenWidth,screenHeight);
layout.setLayoutParams(layoutParams);
if(getSupportActionBar() != null){
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
}
io = new IOSender();
io.connect();
}
@Override
public boolean onTouchEvent(MotionEvent e){
RelativeLayout layout = findViewById(R.id.layout);
//MessageSender messageSender= new MessageSender();
//JSSender jsSender = new JSSender();
//Points where the Display is touched
int points = e.getPointerCount();
//useless?
layout.removeAllViews();
//clean the Layout if the last Finger is up
if(e.getAction() == MotionEvent.ACTION_UP){
layout.removeAllViews();
}else{
//Show Coordinates of the Touch
for(int i = 0; i < points;i++){
TextView textView = new TextView(this);
int x = (int)e.getX(i);
int y = (int)e.getY(i);
textView.setText("("+x+", "+y+")");
textView.setTextSize(30);
//to get the dynamic Height and Width of the TextView
textView.measure(0,0);
TextView tmp = new TextView(this);
tmp.setText(textView.getMeasuredHeight()+", "+ textView.getMeasuredWidth()+", "+ screenHeight +", "+screenWidth);
tmp.setTextSize(20);
//Too close in right corner
if ( x >= screenWidth - textView.getMeasuredWidth()&& y >= screenHeight - 2*textView.getMeasuredHeight() ) {
textView.setX(screenWidth - textView.getMeasuredWidth());
textView.setY(screenHeight - 2 * textView.getMeasuredHeight());
// Too close to right
}else if ( x >= screenWidth - textView.getMeasuredWidth()){
textView.setX(screenWidth - textView.getMeasuredWidth());
textView.setY(y);
// Too close to Bottom
} else if ( y >= screenHeight - 2*textView.getMeasuredHeight()){
textView.setX(x);
textView.setY(screenHeight - 2*textView.getMeasuredHeight());
} else {
textView.setX(x);
textView.setY(y);
}
layout.addView(tmp);
layout.addView(textView);
if(i==0) {
//messageSender.execute(textView.getText().toString());
//jsSender.execute(textView.getText().toString());
if(io.socket.connected()){
Toast.makeText(MainActivity.this, "Socket Connected!!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "No connection found",Toast.LENGTH_SHORT).show();
}
io.send(textView.getText().toString());
}
}
}
return true;
}
}
А это мой IoSender:
package com.example.calibrator;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import java.net.URISyntaxException;
public class IOSender {
public Socket socket;
public void connect(){
try{
socket = IO.socket("http://127.0.0.1:9002");
socket.connect();
socket.emit("join", "Android connected" );
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
public void send(String msg){
socket.emit("message",msg);
}
}
Это работало кратко несколько дней go, но теперь они не могут найти друг друга снова. В коде, о котором я знаю, не было никаких изменений, и я не знаю, что не так с моим кодом.