我们有一个商家端在线订购 android 应用程序,我在其中成功集成了 star sdk Wifi、蓝牙、USB 所有打印机都可以正常工作,我们可以通过单独存储设置来同时连接 2 台打印机,任何打印机都可以是 Wifi,蓝牙,USB,我们使用了本手册StarPRNT SDK 用户手册,以及本示例 Android SDK从那里的官方网站,现在问题出在我的一个活动中,我希望重新发现 Wifi 打印机并更新我的本地数据库中的设置,这样我就可以确保每次创建活动时都会更新 wifi 打印机设置,因为打印机没有静态 IP 地址,star sdk 示例和文档没有任何用于自动发现的内容。这是我下面的代码,用于重新发现 wifi 打印机并存储它的设置。
活动 onCreate 方法。
//Printer Lists
private List<PortInfo> wifiList = new ArrayList<>();
private List<PortInfo> blueToothList = new ArrayList<>();
private List<PortInfo> usbList = new ArrayList<>();
private PrintSettingLocalDB printSettingLocalDB;
private PrintSettingTwoLocDb print2SettingLocalDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
session = new SessionManager(getApplicationContext(), className);
merchantSession = session.getUserInfo();
onCreateDrawer(savedInstanceState , R.layout.orders);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
printSettingLocalDB = new PrintSettingLocalDB(getApplicationContext());
print2SettingLocalDB = new PrintSettingTwoLocDb(getApplicationContext());
//Printers to be searched
String[] selectedInterfaces = new String[] {
PrinterSettingConstant.IF_TYPE_ETHERNET,
PrinterSettingConstant.IF_TYPE_USB,
PrinterSettingConstant.IF_TYPE_BLUETOOTH
};
//Search started for each type of printer
for (String selectedInterface : selectedInterfaces) {
Order.SearchStartTask searchTask = new Order.SearchStartTask();
searchTask.execute(selectedInterface);
}
}
SearchStartTask 类在同一个 Activity 中。我们检查设置是否已存储在本地数据库中,然后我们将已存储的打印机的 MAC 地址与任何最近发现的 wifi 打印机匹配,如果匹配,则我们更新设置,然后检查打印机的状态/纸张/裁纸器等使用最近更新的两台打印机设置,然后相应地在小吃店中显示响应。下面的代码适用于打印机 1 和打印机 2 上的 USB 打印机,Wifi 也可以工作,直到 IP 地址没有更改。
private int count = 0;
private class SearchStartTask extends AsyncTask<String, Void, String> {
private List<PortInfo> mPortList;
SearchStartTask() {
super();
}
@Override
protected String doInBackground(String... interfaceType) {
Log.wtf("SearchStartTask -> doInBackground -> ",interfaceType[0]+" count -> "+count);
try {
mPortList = StarIOPort.searchPrinter(interfaceType[0], getApplicationContext());
}
catch (StarIOPortException e) {
mPortList = new ArrayList<>();
}
return interfaceType[0];
}
@Override
protected void onPostExecute(String printerType) {
count++;
Log.wtf("SearchStartTask -> onPostExecute -> mPortList.size -> ",mPortList.size()+" count -> "+count);
Log.wtf("SearchStartTask -> onPostExecute -> portName -> ",printerType);
if (mPortList.size() > 0) {
switch (printerType){
case "TCP:":
wifiList = mPortList;
break;
case "USB:":
usbList = mPortList;
break;
case "BT:":
blueToothList = mPortList;
break;
}
}
if(count%3==0){
//PRINTER 1
PrinterSettingModel printerSettingModel = printSettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId());
if (printerSettingModel != null)
{
try
{
PrinterSettings settings = printerSettingModel.getPrinterSettings();
Toast.makeText(Order.this, "PrinterSettings found", Toast.LENGTH_SHORT).show();
//Wifi printer auto discovery logic for first printer starts from here.
if(settings.getPortName().contains("TCP")){
if(!wifiList.isEmpty()){
for (int i = 0; i < wifiList.size() ; i++){
if(settings.getMacAddress().trim().equals(wifiList.get(i).getMacAddress().trim())){
try {
printerNumber = 1;
registerPrinter(wifiList.get(i));
if(printSettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId()) != null){
settings = printSettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId()).getPrinterSettings();
}
else {
showSnackBar("Unknown Error. Please Reattach Printer.", R.color.redColor);
}
StarPrinterStatus status = StarIOPort.getPort(settings.getPortName(), settings.getPortSettings(), 3000).retreiveStatus();
if (!status.offline) {
if (!status.receiptPaperEmpty) {
if (!status.coverOpen) {
showSnackBar(settings.getModelName() + " Connected.", R.color.green);
}
else {
showSnackBar("Printer cover is open.", R.color.redColor);
}
}
else {
showSnackBar("Printer receipt is empty.", R.color.redColor);
}
}
else {
showSnackBar(settings.getModelName() + " is offline.", R.color.redColor);
}
}
catch (StarIOPortException e){
e.printStackTrace();
showSnackBar(e.getMessage(), R.color.redColor);
}
}
else {
showSnackBar(settings.getModelName() + " is offline.", R.color.redColor);
}
}
}
else {
showSnackBar(settings.getModelName() + " is offline.", R.color.redColor);
}
}
//USB printer auto discovery logic for first printer starts from here.
if(settings.getPortName().contains("USB")){
String message = Helper.checkPrinterStatus(settings, Order.this);
int color = Helper.checkPrinterStatus(settings, Order.this).contains("Connected") ? R.color.green : R.color.redColor;
showSnackBar(message, color);
}
}
catch (Exception e){
e.printStackTrace();
}
}
//PRINTER 2
PrinterSettingModel printer2SettingModel = print2SettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId());
if (printer2SettingModel != null)
{
try
{
PrinterSettings settings = printer2SettingModel.getPrinterSettings();
if(settings.getPortName().contains("TCP")){
//Wifi printer auto discovery logic for scond printer starts from here.
if(!wifiList.isEmpty()){
for (int i = 0; i < wifiList.size() ; i++){
if(settings.getMacAddress().trim().equals(wifiList.get(i).getMacAddress().trim())){
try {
printerNumber = 1;
registerPrinter(wifiList.get(i));
if(print2SettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId()) != null){
settings = print2SettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId()).getPrinterSettings();
}
else {
showSnackBar("Unknown Error. Please Reattach Printer.", R.color.redColor);
}
StarPrinterStatus status = StarIOPort.getPort(settings.getPortName(), settings.getPortSettings(), 3000).retreiveStatus();
if (!status.offline) {
if (!status.receiptPaperEmpty) {
if (!status.coverOpen) {
showSnackBar(settings.getModelName() + " Connected.", R.color.green);
}
else {
showSnackBar("Printer cover is open.", R.color.redColor);
}
}
else {
showSnackBar("Printer receipt is empty.", R.color.redColor);
}
}
else {
showSnackBar(settings.getModelName() + " is offline.", R.color.redColor);
}
}
catch (StarIOPortException e){
e.printStackTrace();
showSnackBar(e.getMessage(), R.color.redColor);
}
}
else {
showSnackBar(settings.getModelName() + " is offline.", R.color.redColor);
}
}
}
else {
showSnackBar(settings.getModelName() + " is offline.", R.color.redColor);
}
}
//USB printer auto discovery logic for second printer starts from here.
if(settings.getPortName().contains("USB")){
String message = Helper.checkPrinterStatus(settings, Order.this);
int color = Helper.checkPrinterStatus(settings, Order.this).contains("Connected") ? R.color.green : R.color.redColor;
showSnackBar(message, color);
}
}
catch (Exception e){
e.printStackTrace();
}
}
count = 0;
}
}
}
显示状态的 Snackbar 方法。
private void showSnackBar(String msg, int backgroundColor){
Helper.showSnackBar(findViewById(R.id.idOrderMainLayout),
msg,
"DISSMISS",
getResources().getColor(R.color.white),
getResources().getColor(backgroundColor));
}
使用发现的端口信息(打印机设置)在下面注册打印机方法,如果设置不存在,我们会以某种方式存储它们,如果设置已经存在,那么我们只更新它们。
private void registerPrinter(PortInfo info) {
PrinterSettingModel printerSettingModel = new PrinterSettingModel(session.getBrandId(),
"pref_key_printer_settings_json",
"pref_key_allreceipts_settings",
0,
new PrinterSettings(TSP100,
info.getPortName().contains("USB") ? "USB:" : info.getPortName(),
ModelCapability.getPortSettings(TSP100),
info.getMacAddress(),
info.getModelName(),
true,
PrinterSettingConstant.PAPER_SIZE_THREE_INCH));
switch (printerNumber){
case 1:
boolean isPrinter1Added = printSettingLocalDB.checkExistingPrinterSettings(session.getBrandId());
if (!isPrinter1Added)
{
printSettingLocalDB.addPrinterSettings(printerSettingModel);
}
else {
printSettingLocalDB.updatePrinterSettings(session.getBrandId() , printerSettingModel);
}
break;
case 2:
boolean isPrinter2Added = print2SettingLocalDB.checkExistingPrinterSettings(session.getBrandId());
if (!isPrinter2Added)
{
print2SettingLocalDB.addPrinterSettings(printerSettingModel);
}
else {
print2SettingLocalDB.updatePrinterSettings(session.getBrandId() , printerSettingModel);
}
break;
}
Toast.makeText(getApplicationContext(), info.getModelName() + " Attached.", Toast.LENGTH_LONG).show();
addPrinterInfo();
}
addPrinterInfo 方法来获取和设置 UI 上的打印机信息。
private void addPrinterInfo() {
//PRINTER 1
PrinterSettingModel printerSettingModel = printSettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId());
if (printerSettingModel == null) {
printerDeviceName.setText(R.string.connect_printer_text);
printerDeleteBtn.setText(R.string.printer_connect_btn_text);
printerDeleteBtn.setBackgroundColor(getResources().getColor(R.color.green));
}
else {
// Get a port name, MAC address and model name of the destination printer.
String portName = printerSettingModel.getPrinterSettings().getPortName();
String macAddress = printerSettingModel.getPrinterSettings().getMacAddress();
String modelName = printerSettingModel.getPrinterSettings().getModelName();
printerDeviceName.setText(modelName + " - " + portName);
printerDeleteBtn.setText(R.string.printer_reset_btn_text);
printerDeleteBtn.setBackgroundColor(getResources().getColor(R.color.redColor));
}
//PRINTER 2
PrinterSettingModel printer2SettingModel = print2SettingLocalDB.getPrinterSettingsFromLocalDB(session.getBrandId());
if (printer2SettingModel == null) {
printer2DeviceName.setText(R.string.connect_printer_text);
printer2DeleteBtn.setText(R.string.printer_connect_btn_text);
printer2DeleteBtn.setBackgroundColor(getResources().getColor(R.color.green));
}
else {
String modelName = printer2SettingModel.getPrinterSettings().getModelName();
String portName = printer2SettingModel.getPrinterSettings().getPortName();
printer2DeviceName.setText(modelName + " - " + portName);
printer2DeleteBtn.setText(R.string.printer_reset_btn_text);
printer2DeleteBtn.setBackgroundColor(getResources().getColor(R.color.redColor));
}
}
用于检查/添加/更新/删除代码的打印机设置的本地数据库也很糟糕。PrintSettingLocalDB.Java
public class PrintSettingLocalDB extends SQLiteOpenHelper {
private static final String PRINTER_SETTING_TABLE = "PRINTER_SETTING_TABLE";
private static final String COL_ID = "ID";
private static final String COL_BRAND_ID = "BRAND_ID";
private static final String COL_PREF_KEY_PRINTER_SETTINGS_JSON = "pref_key_printer_settings_json";
private static final String COL_PREF_KEY_ALLRECEIPTS_SETTINGS = "pref_key_allreceipts_settings";
private static final String COL_mAllReceiptSettings = "mAllReceiptSettings";
private static final String COL_mModelIndex = "mModelIndex";
private static final String COL_mPortName = "mPortName";
private static final String COL_mPortSettings = "mPortSettings";
private static final String COL_mMacAddress = "mMacAddress";
private static final String COL_mModelName = "mModelName";
private static final String COL_mCashDrawerOpenActiveHigh = "mCashDrawerOpenActiveHigh";
private static final String COL_mPaperSize = "mPaperSize";
private Cursor cursor = null;
public PrintSettingLocalDB(Context context) {
super(context, PRINTER_SETTING_TABLE, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String createTable = "CREATE TABLE " + PRINTER_SETTING_TABLE + " ( " +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1, " +
COL_BRAND_ID + " INTEGER NOT NULL, " +
COL_PREF_KEY_PRINTER_SETTINGS_JSON + " TEXT , " +
COL_PREF_KEY_ALLRECEIPTS_SETTINGS + " TEXT NOT NULL, " +
COL_mAllReceiptSettings + " INTEGER NOT NULL, " +
COL_mModelIndex + " INTEGER NOT NULL, " +
COL_mPortName + " TEXT NOT NULL, " +
COL_mPortSettings + " TEXT NOT NULL, " +
COL_mMacAddress + " TEXT NOT NULL, " +
COL_mModelName + " TEXT NOT NULL, " +
COL_mCashDrawerOpenActiveHigh + " TEXT NOT NULL, " +
COL_mPaperSize + " INTEGER NOT NULL ); ";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + PRINTER_SETTING_TABLE);
onCreate(db);
}
public boolean checkExistingPrinterSettings(int brandId)
{
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + PRINTER_SETTING_TABLE + " WHERE " + COL_BRAND_ID + " =? ";
String[] verifyDetail = {String.valueOf(brandId)};
Cursor cursor = db.rawQuery(query, verifyDetail);
if (cursor != null && cursor.getCount() > 0)
{
return true;
}
else
{
return false;
}
}
public boolean updatePrinterSettings(int brandId , PrinterSettingModel printerSettingModel)
{
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_BRAND_ID, printerSettingModel.getBrandID());
contentValues.put(COL_PREF_KEY_PRINTER_SETTINGS_JSON, printerSettingModel.getPREF_KEY_PRINTER_SETTINGS_JSON());
contentValues.put(COL_PREF_KEY_ALLRECEIPTS_SETTINGS, printerSettingModel.getPREF_KEY_ALLRECEIPTS_SETTINGS());
contentValues.put(COL_mAllReceiptSettings, printerSettingModel.getmAllReceiptSettings());
contentValues.put(COL_mModelIndex, printerSettingModel.getPrinterSettings().getModelIndex());
contentValues.put(COL_mPortName, printerSettingModel.getPrinterSettings().getPortName());
contentValues.put(COL_mPortSettings, printerSettingModel.getPrinterSettings().getPortSettings());
contentValues.put(COL_mMacAddress, printerSettingModel.getPrinterSettings().getMacAddress());
contentValues.put(COL_mModelName, printerSettingModel.getPrinterSettings().getModelName());
contentValues.put(COL_mCashDrawerOpenActiveHigh, printerSettingModel.getPrinterSettings().getCashDrawerOpenActiveHigh());
contentValues.put(COL_mPaperSize, printerSettingModel.getPrinterSettings().getPaperSize());
String[] detail = {String.valueOf(brandId)};
db.update(PRINTER_SETTING_TABLE, contentValues, COL_BRAND_ID + " =?", detail);
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
public boolean deletePrinterSettings(int brandId)
{
try {
SQLiteDatabase db = this.getWritableDatabase();
String[] detail = {String.valueOf(brandId)};
// db.delete(PRINTER_SETTING_TABLE, COL_BRAND_ID + " =?", detail);
return db.delete(PRINTER_SETTING_TABLE, COL_BRAND_ID + " =?", detail) > 0;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
public boolean addPrinterSettings(PrinterSettingModel printerSettingModel)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_BRAND_ID, printerSettingModel.getBrandID());
contentValues.put(COL_PREF_KEY_PRINTER_SETTINGS_JSON, printerSettingModel.getPREF_KEY_PRINTER_SETTINGS_JSON());
contentValues.put(COL_PREF_KEY_ALLRECEIPTS_SETTINGS, printerSettingModel.getPREF_KEY_ALLRECEIPTS_SETTINGS());
contentValues.put(COL_mAllReceiptSettings, printerSettingModel.getmAllReceiptSettings());
contentValues.put(COL_mModelIndex, printerSettingModel.getPrinterSettings().getModelIndex());
contentValues.put(COL_mPortName, printerSettingModel.getPrinterSettings().getPortName());
contentValues.put(COL_mPortSettings, printerSettingModel.getPrinterSettings().getPortSettings());
contentValues.put(COL_mMacAddress, printerSettingModel.getPrinterSettings().getMacAddress());
contentValues.put(COL_mModelName, printerSettingModel.getPrinterSettings().getModelName());
contentValues.put(COL_mCashDrawerOpenActiveHigh, printerSettingModel.getPrinterSettings().getCashDrawerOpenActiveHigh());
contentValues.put(COL_mPaperSize, printerSettingModel.getPrinterSettings().getPaperSize());
long result = db.insert(PRINTER_SETTING_TABLE, null, contentValues);
if (result == -1)
{
return false;
}
else
{
return true;
}
}
public PrinterSettingModel getPrinterSettingsFromLocalDB(int brandId) {
SQLiteDatabase db = this.getReadableDatabase();
PrinterSettingModel printerSettingModel = null;
try {
cursor = null;
String query = "SELECT * FROM " + PRINTER_SETTING_TABLE + " WHERE " + COL_BRAND_ID + " =? ";
String[] verifyDetail = {String.valueOf(brandId)};
Cursor cursor = db.rawQuery(query, verifyDetail);
cursor.moveToFirst();
if (cursor != null && cursor.getCount()>0) {
if (cursor.moveToFirst()) {
printerSettingModel = new PrinterSettingModel();
printerSettingModel.setBrandID(cursor.getInt(cursor.getColumnIndex(COL_BRAND_ID)));
printerSettingModel.setPREF_KEY_PRINTER_SETTINGS_JSON(cursor.getString(cursor.getColumnIndex(COL_PREF_KEY_PRINTER_SETTINGS_JSON)));
printerSettingModel.setPREF_KEY_ALLRECEIPTS_SETTINGS(cursor.getString(cursor.getColumnIndex(COL_PREF_KEY_ALLRECEIPTS_SETTINGS)));
printerSettingModel.setmAllReceiptSettings(cursor.getInt(cursor.getColumnIndex(COL_mAllReceiptSettings)));
printerSettingModel.setPrinterSettings(new PrinterSettings(cursor.getInt(cursor.getColumnIndex(COL_mModelIndex)),
cursor.getString(cursor.getColumnIndex(COL_mPortName)),
cursor.getString(cursor.getColumnIndex(COL_mPortSettings)),
cursor.getString(cursor.getColumnIndex(COL_mMacAddress)),
cursor.getString(cursor.getColumnIndex(COL_mModelName)),
Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(COL_mCashDrawerOpenActiveHigh))),
cursor.getInt(cursor.getColumnIndex(COL_mPaperSize))));
}
cursor.close();
}
return printerSettingModel;
} catch (Exception e) {
e.printStackTrace();
}
return printerSettingModel;
}
}
我一直在许多论坛上在线搜索,但没有关于星打印机集成的帮助或示例。我们能够与多台打印机完全集成并完成打印操作,但我们编写的自动发现逻辑无法正常工作,我还研究了如何从 Star Micronincs官方支持为打印机分配静态 IP 地址网站,但是对于我们拥有的客户类型来说,这不是一个好方法,它会产生管理员问题,而且因为我们有海外客户,所以没有为客户提供物理支持。
我试图尽可能多地发布代码。任何帮助将不胜感激,谢谢您和问候:)