安卓通過藍牙打印條碼業務,根據最新的幫助打印條碼,可以打印二維碼內容。
https://download.csdn.net/download/weijia3624/11975543
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button Button1,Button2;
private TextView textView1;
private ListView BlueDevView;
private static final int REQUEST_ENABLE_BT= 1;
private String btDevMacAddr = "";
private BluetoothSocket Socket = null;
private BluetoothDevice btdev = null;
private Context context = this;
public static final byte[] DOUBLE_HEIGHT_WIDTH = {0x1d, 0x21, 0x11};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button1 = (Button) findViewById(R.id.button1);// 取得組件
Button1.setOnClickListener(this);
Button2 = (Button) findViewById(R.id.button2);// 取得組件
Button2.setOnClickListener(this);
textView1 = (TextView) findViewById(R.id.textView1);
BlueDevView = (ListView) findViewById(R.id.listView1);
setStatusInfo("未連接設備 ");
BlueDevView.setAdapter(null);
BlueDevView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
try
{
@SuppressWarnings("unchecked")
HashMap<String,String> map=(HashMap<String,String>)BlueDevView.getItemAtPosition(position);
btDevMacAddr=map.get("devMacAddr");
Log.d("ligao","btDevMacAddr "+btDevMacAddr);
if(btDevMacAddr!=""){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btdev=mBluetoothAdapter.getRemoteDevice(btDevMacAddr);
setStatusInfo("已經連接 "+btDevMacAddr);
}
}
catch(Exception e)
{
Log.d("ligao","err "+e.getMessage());
setStatusInfo("err "+e.getMessage());
return;
}
}
});
setBluetooth();
}
private void setStatusInfo(String info)
{
textView1.setText(info);
}
private void setBluetooth()
{
//獲取 BluetoothAdapter,該設備不支持藍牙,getDefaultAdapter()返回 null,
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
//判斷藍牙是否啓用,若未啓用則請求用戶允許啓用藍牙
if (!mBluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
}
BlueDevView.setAdapter(null);
//查詢配對的設備
try{
ArrayAdapter<HashMap<String,String>> mArrayAdapter=new ArrayAdapter<HashMap<String,String>>(this,android.R.layout.simple_list_item_1);
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
String devName=device.getName();
if(devName.indexOf("SATO")>-1)
{
HashMap<String, String> devMap = new HashMap<String, String>();
devMap.put("devName", device.getName());
devMap.put("devMacAddr", device.getAddress());
mArrayAdapter.add(devMap);
}
}
BlueDevView.setAdapter(mArrayAdapter);
}
}
catch(Exception e)
{
Log.d("ligao","setBlueDevView Err "+ e.getMessage());
setStatusInfo("setBlueDevView Err "+ e.getMessage());
return ;
}
}
private void printDate()
{
try {
String text="AAAAAA@BBBBB@CCCCC@DDDDD@EEEEE@FFFFF@GGGGGG";
String[] inarr = text.split("@");
String drugCode=inarr[0];
String drugDesc=inarr[1];
String drugSpec=inarr[2];
String drugBatNo=inarr[3];
String drugExpDate=inarr[4];
String drugBarcode=inarr[5];
String drugManf=inarr[6];
drugCode=string2HexString("A數");
drugDesc=string2HexString("B量:100");
drugSpec=string2HexString("日期:_ _ _ _ _ _");
drugBatNo=string2HexString("編號:_ _ _ _ _ _");
drugExpDate=string2HexString(drugExpDate);
drugBarcode=string2HexString(drugBarcode);
drugManf=string2HexString(drugManf);
int vstar=50; //豎直位置
int hstar=1; //水平位置
int h1=hstar+40,h2=hstar+165;
int vstep=40; //行寬
byte[] byteText = text.getBytes("GB18030");
int byteTextLen= byteText.length;
String dnmmmm= String.format("%04d", byteTextLen);
String CommandCode="<A>"
+"<V>"+(vstar+vstep*1)+"<H>"+h2+"<P>3<L>0101<C9>H"+drugCode
+"<V>"+(vstar+vstep*3)+"<H>"+h1+"<P>3<L>0101<C9>H"+drugDesc
+"<V>"+(vstar+vstep*5)+"<H>"+h1+"<P>3<L>0101<C9>H"+drugSpec
+"<V>"+(vstar+vstep*7)+"<H>"+h1+"<P>3<L>0101<C9>H"+drugBatNo
+"<V>"+(vstar+vstep*9)+"<H>"+h2+"<2D30>,L,03,1,0<DN>"+dnmmmm+","+text
+"<Q>1"
+"<Z>";
//CommandCode= "<A><H>100<V>100<2D30>,L,09,1,0<DN>0096,"+text+"<Q1<Z";
Log.d("ligao","CodePrint CommandCode:"+ CommandCode );
CodePrint(CommandCode);
return;
} catch (Exception e) {
}
}
private void printClear()
{
CodePrint("<A><*><Z>");
return;
}
private void CodePrint(final String CommandCode)
{
Thread thread = new Thread() {
public void run() {
try {
String PrintCode=CommandCode;
Log.d("ligao","CodePrint CommandCode:"+ PrintCode );
setSocket();
Socket.connect();
OutputStream outputStream = Socket.getOutputStream();
PrintCode=PrintCode.replace('<','\u001b'); //< 替換成 ESC
PrintCode=PrintCode.replace(">", "");
Log.d("ligao","CodePrint CommandCode2:"+ PrintCode );
byte[] b = PrintCode.getBytes("GB18030"); //GB18030
outputStream.write(b);
outputStream.flush();
Socket.close();
} catch (Exception e) {
Log.d("ligao","CodePrint Err1"+ e.getMessage());
setStatusInfo("CodePrint Err1"+ e.getMessage());
try {
Socket.close();
} catch (Exception closeException) {
Log.d("ligao","CodePrint Err2"+ closeException.getMessage());
setStatusInfo("CodePrint Err2"+ closeException.getMessage());
}
return;
}
}
};
thread.start();
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button1:
{
try
{
Log.d("ligao","b1 click 0");
printDate();
//selectCommand();
}catch(Exception e)
{
Log.d("ligao","b1 click eeee "+ e.getMessage());
return ;
}
break;
}
case R.id.button2:
{
printClear();
Log.d("ligao","b2 click 0");
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setSocket()
{
try{
if(btdev != null)
{
Socket = btdev.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
else
{
Log.d("ligao","setSocket btdev is null");
}
}
catch(Exception e)
{
Log.d("ligao","Socket Err "+ e.getMessage());
return ;
}
}
private static String string2HexString(String strPart) {
byte[] b;
StringBuffer result = new StringBuffer();
String hex;
try {
b = strPart.getBytes("GB18030"); //GB18030
for (int i = 0; i < b.length; i++) {
hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result.append(hex.toUpperCase());
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return result.toString();
}
return result.toString();
}
}