说明
因公司需求,需要实现连接WiFi打印机进行打印,很多年还没有WiFi的时候做过蓝牙打印机,真实难为我了,废话不多说,开始吧!
效果图
实现步骤
1:平台创建账号并认证:https://admin.feieyun.com/index.php 审核通过后就会得到相应的USER和UKEY,后面会用到 2:对接相应API:API官方文档自家看
公共参数
public static final String URL = "http://api.feieyun.cn/Api/Open/";
public static final String USER = "*********";
public static final String UKEY = "*********";
public static final String SN = "*********";
String snlist="*******# *******# 拼必达打印机 # *******";
添加打印机
public void rightDatelist(String snlist){
String STIME = String.valueOf(System.currentTimeMillis()/1000);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
FormBody formBody = new FormBody.Builder()
.add("user", USER)
.add("stime", STIME)
.add("sig", getSHA(USER+UKEY+STIME))
.add("apiname", "Open_printerAddlist")
.add("debug", "0")
.add("printerContent", snlist)
.build();
final Request request = new Request.Builder()
.url(URL)
.post(formBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("添加失败"+e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code()==200) {
System.out.println("添加成功"+response.body().string());
}
}
});
}
删除打印机
public void rightDatelistDc(){
String STIME = String.valueOf(System.currentTimeMillis()/1000);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
FormBody formBody = new FormBody.Builder()
.add("user", USER)
.add("stime", STIME)
.add("sig", getSHA(USER+UKEY+STIME))
.add("apiname", "Open_printerDelList")
.add("debug", "0")
.add("snlist", SN)
.build();
final Request request = new Request.Builder()
.url(URL)
.post(formBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("删除失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code()==200) {
System.out.println("删除成功"+response.body().string());
}
}
});
}
打印小票
public void rightDatelistDy(){
String content;
content = "<CB>安卓测试打印</CB><BR>";
content += "名称 单价 数量 金额<BR>";
content += "--------------------------------<BR>";
content += "饭 1.0 1 1.0<BR>";
content += "炒饭 10.0 10 10.0<BR>";
content += "蛋炒饭 10.0 10 100.0<BR>";
content += "鸡蛋炒饭 100.0 1 100.0<BR>";
content += "番茄蛋炒饭 1000.0 1 100.0<BR>";
content += "西红柿蛋炒饭 1000.0 1 100.0<BR>";
content += "西红柿鸡蛋炒饭 100.0 10 100.0<BR>";
content += "备注:加辣<BR>";
content += "--------------------------------<BR>";
content += "合计:10000.0元<BR>";
content += "送货地点:贵州易荣德尚<BR>";
content += "联系电话:17385159420<BR>";
content += "订餐时间:2021-08-04 08:08:08<BR>";
String STIME = String.valueOf(System.currentTimeMillis()/1000);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
FormBody formBody = new FormBody.Builder()
.add("user", USER)
.add("stime", STIME)
.add("sig", getSHA(USER+UKEY+STIME))
.add("apiname", "Open_printMsg")
.add("sn", SN)
.add("content", content)
.add("times","1")
.build();
final Request request = new Request.Builder()
.url(URL)
.post(formBody)
.build();
System.out.println("打印机编号:"+SN);
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("打印失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code()==200) {
System.out.println("打印成功"+response.body().string());
}
}
});
}
SHA-1加密字符串
public static String getSHA(String info) {
byte[] bytesSHA = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
messageDigest.update(info.getBytes());
bytesSHA = messageDigest.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String strSHA = byteToHex(bytesSHA);
return strSHA;
}
Bytes数组转换为字符串
private static String byteToHex(byte[] bytes) {
String hs = "";
String temp;
for (byte b : bytes) {
temp = (Integer.toHexString(b & 0XFF));
if (temp.length() == 1) {
hs = hs + "0" + temp;
} else {
hs = hs + temp;
}
}
return hs;
}
|