c ++控制台应用程序与android应用程序的连接 [英] c++ Console application connection to android app

查看:74
本文介绍了c ++控制台应用程序与android应用程序的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在android应用程序和c ++控制台应用程序之间建立连接:
假设控制台应用程序是客户端,它具有4条命令来从服务器(即android设备)获取数据。换句话说,我需要在控制台应用程序中显示设备信息(BT / WIFI开/关,BATTERY LVL)。



我创建了客户端,我也有我必须检查的代码(例如BT是否已打开)。
我唯一缺少的是它们之间的连接,我没有服务器代码方面的经验,我需要您的帮助,这是如何完成的或教程的链接导致我找不到它们。 / p>

android代码:

  @Override 
受保护的无效onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// IMEI初始化
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
IMEI = telephonyManager.getDeviceId();

//软件版本初始化
PackageInfo pInfo = null;
try {
pInfo = getPackageManager()。getPackageInfo(getPackageName(),0);
} catch(PackageManager.NameNotFoundException e){
e.printStackTrace();
}
int SoftwareVersion = pInfo.versionCode;


//电池lvl初始化
battery = Library.getBatteryLevel(this);

// android id初始化
android_id = Secure.getString(this.getContentResolver(),
Secure.ANDROID_ID);

// Android可用性初始化
internetaval = Library.isNetworkAvailable(this);

//完成初始化< ~~




TextView tv =(TextView)findViewById(R.id。电视1);
tv.setText( + internetaval);

Library.setWifi(this,false);


}


解决方案

尽管您的问题过于笼统,有待解决,但我将为您提供一个可以忽略的线索。



使用Java网络API(套接字编程),您应该选择一个端口号并开始监听。这里的关键是,您的Android设备和运行该C ++应用程序的计算机必须连接到同一网络(例如WLAN或LAN)。



在这种情况下两个设备都将分配两个唯一的IP地址,也可以手动设置它们。



在客户端(C ++)中,您应该创建一个TCP套接字并连接到指定端口号的Android设备的IP地址。因此,通过这种方式,您可以将C ++连接到在Android应用中运行的Java服务器。同样在另一面,您应该使用Java套接字API在Android应用中接受传入连接。





建议:请勿从头开始创建应用,请始终使用众所周知的图书馆。这使开发过程更快,更有趣。 :)


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


}

解决方案

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. :)

这篇关于c ++控制台应用程序与android应用程序的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆