【问题标题】:Probelm with Server-Client Application using Usb with Adb (Android & Node.js)使用 Usb 和 Adb(Android 和 Node.js)的服务器-客户端应用程序问题
【发布时间】:2020-05-21 07:36:16
【问题描述】:

我正在尝试使用 USB 将坐标从我的手机发送到我的计算机。 我正在使用 Socket.io 将 Node.js 服务器与 android 客户端连接,我正在使用 adb reverse tcp:9002 tcp:9002 连接端口。

这是 Node.js-Server 的外观:

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')

});

这是 Android 客户端的 MainActivity:

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);
}
}

几天前它工作过,但现在他们再也找不到对方了。 据我所知,没有对代码进行任何更改,我现在也不知道我的代码有什么问题。

【问题讨论】:

  • 请检查设备节点js服务器和android客户端是否在同一个网络上。另外,在您的connect() 方法中,不要使用127.0.0.1,而是使用运行节点服务器的设备的真实IP 地址,然后重试。
  • @Atish 他们都使用PC的localhost,因为Phone的127.0.0.1:9002是通过adb reverse tcp:9002 tcp:9002连接到PC的同一个地址的。它们通过 USB 连接
  • 请检查防火墙或其他杀毒软件是否阻塞了此端口。另外,检查您是否可以直接使用其他代码(在 PC 上)手动连接到您的套接字服务器。这是为了验证您的节点服务器是否正常工作。然后尝试调试手机的端口。
  • 我在本地测试它并且服务器运行没有问题,但是如果我什至尝试使用 10.0.2.2 和我的端口(10.0.2.2 是一个环回地址到运行模拟器的 PC 的本地主机),它不会连接。所以这似乎是应用程序本身的问题。
  • 这很奇怪。我仍然想知道应用程序是否存在问题。但是,如果您愿意以某种方式分享源代码,我们可以看看。

标签: android node.js socket.io adb


【解决方案1】:

我终于找到了修复它的东西。 对于 Android 9 及更高版本,您必须将 android:usesCleartextTraffic="true" 添加到 Manifest,否则应用程序会直接阻止流量,而不会以任何方式通知您。 现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多