我们有一个商户端在线订购android应用程序,我已经成功集成了star sdk wifi、蓝牙、usb所有打印机工作正常,我们可以通过单独存储设置同时连接2台打印机,任何打印机都可以是wifi、蓝牙、usb,我们使用了本手册starprnt sdk用户手册,这是官方网站上的android sdk示例,现在问题出在我的一个活动中,我希望重新发现wifi打印机并更新本地数据库中的设置,这样我就可以确保每次创建活动时都会更新wifi打印机设置,因为打印机没有静态ip地址,star sdk示例和文档中没有任何内容自动发现。下面是我重新发现wifi打印机并存储其设置的代码。
创建方法的活动性。
//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类。我们检查设置是否已存储在本地数据库中,然后将已存储的打印机的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地址分配给打印机,但这不是一个好的方法与类型的客户,我们有它会造成管理员的问题,而且因为我们有海外客户,所以没有物理支持提供给客户。
我尽可能多地张贴代码。如有任何帮助,我们将不胜感激,谢谢并致以问候:)
暂无答案!
目前还没有任何答案,快来回答吧!