该程序可实现设置打印机参数,打印机使用socket与手机通讯
设置对方参数页面:
PC端依然用调试工具测试
接下来可以跟我一起操作
下载该jar包以后复制到libs中,并右键加入库中
http://download.csdn.net/download/bfz_50/10211740
常识性提醒
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.a123.printer">
<!--允许应用程序改变网络状态-->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<!--允许应用程序改变WIFI连接状态-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!--允许应用程序访问有关的网络信息-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--允许应用程序访问WIFI网卡的网络信息-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<!--允许应用程序完全使用网络-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingLabelParametersActivity"/>
</application>
</manifest>
activity_main.xml没什么特别的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="please enter server ip"
android:id="@+id/id_et_ip"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="please enter server port"
android:id="@+id/id_et_port"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="连接"
android:id="@+id/id_bt_openPort"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从手机下载pcx模板到打印机"
android:id="@+id/id_bt_downloadPCX"
android:visibility="gone"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置标签参数"
android:id="@+id/id_bt_setup"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用打印机内部条码格式打印"
android:id="@+id/id_bt_barcode"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用条码机内部文字格式打印"
android:id="@+id/id_bt_printerFont"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送GP-1634TC-XDBQ-1711278.txt内容到打印机"
android:id="@+id/id_bt_send_txt"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭端口"
android:id="@+id/id_bt_closePort"/>
</LinearLayout>
注意在MainActivity.java中,我使用handler跳转,因为涉及到网络操作,所以我将所有的点击事件都放在线程中,我在第二个按钮中设置了跳转,然后到handler中执行,可以使用Ctrl键+F查看handler的操作
MainActivity.java
package com.a123.printer;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.tscwifidll.TscWifiActivity;
import java.io.FileInputStream;
/*
* 该程序可实现向server端发送数据
* 【Android消息处理机制(Handler 与Message)---01】
* https://www.cnblogs.com/fuck1/p/5513412.html
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
EditText editText,editText2;
Button button_open_port,
button_download_pcx,
button_setup,
button_close_port,
button_barcode,
button_printer_font,
button_send_txt;
String ipString;
int port;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Toast.makeText(MainActivity.this,
"success",Toast.LENGTH_SHORT).show();
if (msg.what==1366){
Intent intent= (Intent) msg.obj;
startActivity(intent);
}
}
};
TscWifiActivity TscEthernetDll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
}
private void initView(){
editText= (EditText) findViewById(R.id.id_et_ip);
editText2= (EditText) findViewById(R.id.id_et_port);
button_open_port = (Button) findViewById(R.id.id_bt_openPort);
button_download_pcx= (Button) findViewById(R.id.id_bt_downloadPCX);
button_setup= (Button) findViewById(R.id.id_bt_setup);
button_close_port= (Button) findViewById(R.id.id_bt_closePort);
button_barcode= (Button) findViewById(R.id.id_bt_barcode);
button_printer_font= (Button) findViewById(R.id.id_bt_printerFont);
button_send_txt= (Button) findViewById(R.id.id_bt_send_txt);
}
private void initData(){
//数据,变量的初始化
ipString="10.3.0.87";
port=9100;
TscEthernetDll=new TscWifiActivity();//没有初始化不能使用
}
private void initEvent(){
button_open_port.setOnClickListener(this);
button_download_pcx.setOnClickListener(this);
button_setup.setOnClickListener(this);
button_close_port.setOnClickListener(this);
button_barcode.setOnClickListener(this);
button_printer_font.setOnClickListener(this);
button_send_txt.setOnClickListener(this);
}
private void initOpenPort(){
//为方便调试,直接在代码改IP了
if (!editText.getText().toString().equals("")){
ipString=editText.getText().toString();//切记getText,这里常出错
}
if (!editText2.getText().toString().equals("")){
port=Integer.parseInt(editText2.getText().toString());//切记不要使用Integer.getInteger
}
TscEthernetDll.openport(ipString,port);
}
private void initSetup(){
Intent intent=new Intent(MainActivity.this,SettingLabelParametersActivity.class);
intent.putExtra("ip",ipString);
intent.putExtra("port",port);
Message message=Message.obtain();
message.obj=intent;
message.what=1366;
handler.sendMessage(message);
}
@Override
public void onClick(final View view) {
//在线程中执行
new Thread(new Runnable() {
@Override
public void run() {
switch (view.getId()){
case R.id.id_bt_openPort:
initOpenPort();
break;
case R.id.id_bt_send_txt:
TscEthernetDll.sendfile("GP-1634TC-XDBQ-1711278.txt");
break;
case R.id.id_bt_downloadPCX:
TscEthernetDll.downloadpcx("UL.PCX");
break;
case R.id.id_bt_setup:
initSetup();
break;
case R.id.id_bt_barcode:
TscEthernetDll.barcode(
100, 100, "128", 100, 1,
0, 3, 3, "123456789");
break;
case R.id.id_bt_printerFont:
TscEthernetDll.printerfont(100, 250, "3", 0, 1,
1, "987654321");
break;
case R.id.id_bt_closePort:
TscEthernetDll.closeport();
break;
}
Message message=Message.obtain(handler);
message.sendToTarget();//发送给handler
}
}).start();
}
}
SettingLabelParametersActivity.java中用了无数个输入框,用于输入参数,如果为空就使用默认的
setting_label_parameters_layout.xml也没什么特别的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id_setting_label_height_edit_text"
android:hint="高度:单位mm,默认60"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/id_setting_label_width_edit_text"
android:hint="宽度:单位mm,默认100"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="打印速度:寸/s,默认4"
android:id="@+id/id_setting_label_speed_edit_text"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="打印浓度:0-15,数越大色越重,默认4"
android:id="@+id/id_setting_label_density_edit_text"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="使用传感器:0-垂直间距,1-黑标,默认0"
android:id="@+id/id_setting_label_sensor_edit_text"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="标签垂直间距高度:单位mm,默认0"
android:id="@+id/id_setting_label_sensor_distance_edit_text"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="偏移距离:单位mm,默认为0"
android:id="@+id/id_setting_label_sensor_offset_edit_text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_setting_label_size_button"
android:text="设置标签"
/>
</LinearLayout>
SettingLabelParametersActivity.java中要注意的是,获取从第一个activity中得到的IP和port,其实TscEthernetDll这个参数只不过是用来连接端口和发送指令的用,在第一个活动释放以后第二个活动连接也是可以的,而且打印机的指令接收也只需要一条一条过去就行,像命令行一样,过去了它就存在打印机ROM(或者flash)中,如果你什么都不做,直接发送打印指令,就会沿用以前的参数,至于获取输入框数据和检测空,基本操作就不多说了
package com.a123.printer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.tscwifidll.TscWifiActivity;
/**
* Created by Administrator on 2018/1/18/0018.
*/
public class SettingLabelParametersActivity extends Activity{
TscWifiActivity TscEthernetDll;
EditText
editTextSettingLabelWidth,
editTextSettingLabelHeight,
editTextSettingLabelSpeed,
editTextSettingLabelDensity,
editTextSettingLabelSensor,
editTextSettingLabelSensorDistance,
editTextSettingLabelSensorOffset;
Button buttonSettingLabelSize;
int width,height,speed,density,sensor,sensor_distance,sensor_offset,port;
String ipSting;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting_label_parameters_layout);
initView();
initData();
initEvent();
}
private void initView(){
buttonSettingLabelSize=findViewById(R.id.id_setting_label_size_button);
editTextSettingLabelWidth=findViewById(R.id.id_setting_label_width_edit_text);
editTextSettingLabelHeight=findViewById(R.id.id_setting_label_height_edit_text);
editTextSettingLabelSpeed=findViewById(R.id.id_setting_label_speed_edit_text);
editTextSettingLabelDensity=findViewById(R.id.id_setting_label_density_edit_text);
editTextSettingLabelSensor=findViewById(R.id.id_setting_label_sensor_edit_text);
editTextSettingLabelSensorDistance=findViewById(R.id.id_setting_label_sensor_distance_edit_text);
editTextSettingLabelSensorOffset=findViewById(R.id.id_setting_label_sensor_offset_edit_text);
buttonSettingLabelSize=findViewById(R.id.id_setting_label_size_button);
}
private void initData(){
Intent intent=getIntent();
ipSting=intent.getStringExtra("ip");
port=intent.getIntExtra("port",9100);
TscEthernetDll=new TscWifiActivity();
width=100;
height=60;
speed=4;
density=4;
sensor=0;
sensor_distance=0;
sensor_offset=0;//如果输入框为空则使用该值
}
private void initEvent(){
new Thread(new Runnable() {
@Override
public void run() {
TscEthernetDll.openport(ipSting,port);
}
}).start();
buttonSettingLabelSize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//此处需要先把各个输入框的值读出来,然后执行设置
//当输入框为空时使用默认值
if (!editTextSettingLabelWidth.getText().toString().equals("")){
width=Integer.parseInt(editTextSettingLabelWidth.getText().toString());
}
if (!editTextSettingLabelHeight.getText().toString().equals("")){
height=Integer.parseInt(editTextSettingLabelHeight.getText().toString());
}
if (!editTextSettingLabelSpeed.getText().toString().equals("")){
speed=Integer.parseInt(editTextSettingLabelSpeed.getText().toString());
}
if (!editTextSettingLabelDensity.getText().toString().equals("")){
density=Integer.parseInt(editTextSettingLabelDensity.getText().toString());
}
if (!editTextSettingLabelSensor.getText().toString().equals("")){
sensor=Integer.parseInt(editTextSettingLabelSensor.getText().toString());
}
if (!editTextSettingLabelSensorDistance.getText().toString().equals("")){
sensor_distance=Integer.parseInt(editTextSettingLabelSensorDistance.getText().toString());
}
if (!editTextSettingLabelSensorOffset.getText().toString().equals("")){
sensor_offset=Integer.parseInt(editTextSettingLabelSensorOffset.getText().toString());
}
new Thread(new Runnable() {
@Override
public void run() {
TscEthernetDll.setup(width,height,speed,density,sensor,sensor_distance,sensor_offset);
}
}).start();
Toast.makeText(SettingLabelParametersActivity.this,"设置成功",Toast.LENGTH_SHORT).show();
finish();
}
});
}
源码:
http://download.csdn.net/download/bfz_50/10211832
}
讲的还是比较简单,希望下次多补充一些对线程的描述。