c++ Console application connection to android app

2019-08-07 05:30发布

I have to make a connection between android app and c++ console application: The console application is suppose to be the client side which has 4 commands to get data from the server which is the android device. In other words I need to display the device info (BT/WIFI on/off, BATTERY LVL) in the console application.

I created the client side and I also have the code for the things I have to check(if BT is on for example). The only thing I'm missing is the connection between these, I have no experience in server code, and I need your help with this, how is this done or links for tutorials cause I didn't find some.

the android code:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //IMEI init
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    IMEI = telephonyManager.getDeviceId();

    //software version init
    PackageInfo pInfo = null;
    try {
        pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    int SoftwareVersion = pInfo.versionCode;


    //battery lvl init
    battery = Library.getBatteryLevel(this);

    //android id init
    android_id = Secure.getString(this.getContentResolver(),
            Secure.ANDROID_ID);

    //android availability init
    internetaval = Library.isNetworkAvailable(this);

    //finish init  <<~~




    TextView tv = (TextView) findViewById(R.id.tv1);
    tv.setText(" " + internetaval);

    Library.setWifi(this,false);


}

2条回答
【Aperson】
2楼-- · 2019-08-07 05:54

Although your question is too broad and subject to being closed, I will give you an overlook clue to be implemented.

Using Java network APIs (socket programming) you should pick a port number and start listening on. The key thing here is that your Android device and your computer running that C++ app must be connected to the same network (for example a WLAN or LAN).

In this case both your devices will be assigned two unique IP address, you can also set them manually.

In your client side (C++), you should create a TCP socket and connect to IP address of your Android device given its port number. So, using this way you can connect your C++ to your Java server running in your Android app. Also in other side, you should accept incoming connects in your Android app using Java socket APIs.

Suggestion: Never create your apps from scratch, always use well-known libraries. This makes development process more quicker and also more enjoyable. :)

查看更多
Melony?
3楼-- · 2019-08-07 06:11

If you mean you have created an C++ for your Android device using cross-compiler. I can launch and execute your own binary app has been builded for your Android app. The most common usage is to extract to a folder inside /data/data/yourapp/somewhere start your own c++ app. Then, launch this app in a separate thread. To interact between Java Android and your C++ app I can offer your to major variants for linux OS: 1. TCP-IP server-client. 2. Use Pipe interaction: FIFO (file in - file out). 3. Or just simple run your c++ app with some input arguments with a result. Of course, you read sdtout and write to stdin as in usual OS.

If you mean a usual Windows/MacOS/Linux app, you have to interact with the Phone using WiFi/BlueTooth/NFS/Lan/adb(Log)/ connection, is your device rooted. That is, first of all you have to define which hardware you have to use. But the major interact in this case: TCP-IP.

查看更多
登录 后发表回答