Created
September 24, 2019 22:47
-
-
Save Mxrck/297290b47c6a70392148b59f3ae3bcf2 to your computer and use it in GitHub Desktop.
SunmiPrinter Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.marcomaldonado.sunmiv2.contracts; | |
import android.os.RemoteException; | |
public interface SunmiCallback { | |
void onRunResult(boolean isSuccess) throws RemoteException; | |
void onReturnString(String result) throws RemoteException; | |
void onRaiseException(int code, String msg) throws RemoteException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.marcomaldonado.sunmiv2.services; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.ServiceConnection; | |
import android.graphics.Bitmap; | |
import android.os.IBinder; | |
import android.os.RemoteException; | |
import com.marcomaldonado.sunmiv2.contracts.SunmiCallback; | |
import woyou.aidlservice.jiuiv5.ICallback; | |
import woyou.aidlservice.jiuiv5.IWoyouService; | |
public class SunmiPrinter { | |
private static final SunmiPrinter printer = new SunmiPrinter(); | |
private IWoyouService woyouService = null; | |
private final String UNREACHABLE = "unreachable"; | |
private ServiceConnection connService = new ServiceConnection() { | |
@Override | |
public void onServiceDisconnected(ComponentName name) { | |
woyouService = null; | |
} | |
@Override | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
woyouService = IWoyouService.Stub.asInterface(service); | |
} | |
}; | |
private SunmiPrinter(){} | |
public static SunmiPrinter getInstance() { | |
return printer; | |
} | |
public void initPrinter(Context context){ | |
Intent intent=new Intent(); | |
intent.setPackage("woyou.aidlservice.jiuiv5"); | |
intent.setAction("woyou.aidlservice.jiuiv5.IWoyouService"); | |
context.startService(intent); | |
context.bindService(intent, connService, Context.BIND_AUTO_CREATE); | |
} | |
// -------------------------------- | |
public void updateFirmware() { | |
if (woyouService != null) { | |
try { | |
woyouService.updateFirmware(); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public int getFirmwareStatus() { | |
if (woyouService != null) { | |
try { | |
return woyouService.getFirmwareStatus(); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
return -1; | |
} | |
public String getServiceVersion() { | |
if (woyouService != null) { | |
try { | |
return woyouService.getServiceVersion(); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
return UNREACHABLE; | |
} | |
public void printerInit(SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printerInit(makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printerSelfChecking(SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printerSelfChecking(makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public String getPrinterSerialNo() { | |
if (woyouService != null) { | |
try { | |
return woyouService.getPrinterSerialNo(); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
return UNREACHABLE; | |
} | |
public String getPrinterVersion() { | |
if (woyouService != null) { | |
try { | |
return woyouService.getPrinterVersion(); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
return UNREACHABLE; | |
} | |
public String getPrinterModal() { | |
if (woyouService != null) { | |
try { | |
return woyouService.getPrinterModal(); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
return UNREACHABLE; | |
} | |
public void getPrintedLength(SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.getPrintedLength(makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void lineWrap(int n, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.lineWrap(n, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void sendRAWData(byte[] data, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.sendRAWData(data, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void setAlignment( int alignment, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.setAlignment(alignment, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void setFontName(String typeface, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.setFontName(typeface, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void setFontSize(float fontSize, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.setFontSize(fontSize, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printText(String text, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printText(text, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printTextWithFont(String text, String typeface, float fontSize, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printTextWithFont(text, typeface, fontSize, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printColumnsText(String[] colsTextArr, int[] colsWidthArr, int[] colsAlign, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printColumnsText(colsTextArr, colsWidthArr, colsAlign, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printBitmap(Bitmap bitmap, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printBitmap(bitmap, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printBarCode(String data, int symbology, int height, int width, int textposition, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printBarCode(data, symbology, height, width, textposition, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printQRCode(String data, int moduleSize, int errorLevel, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printQRCode(data, moduleSize, errorLevel, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void printOriginalText(String text, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.printOriginalText(text, makeCallback(callback)); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void commitPrinterBuffer(SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.commitPrinterBuffer(); | |
if (callback != null) { | |
callback.onRunResult(true); | |
} | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void enterPrinterBuffer(boolean clean, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.enterPrinterBuffer(clean); | |
if (callback != null) { | |
callback.onRunResult(true); | |
} | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
public void exitPrinterBuffer(boolean commit, SunmiCallback callback) { | |
if (woyouService != null) { | |
try { | |
woyouService.exitPrinterBuffer(commit); | |
if (callback != null) { | |
callback.onRunResult(true); | |
} | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
ThrowErrorCallback(callback); | |
} | |
} | |
else { | |
ThrowErrorCallback(callback); | |
} | |
} | |
private void ThrowErrorCallback(SunmiCallback callback) { | |
if (callback != null) { | |
try { | |
callback.onRaiseException(500, UNREACHABLE); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private ICallback makeCallback(final SunmiCallback callback) { | |
if (callback != null) { | |
return new ICallback.Stub() { | |
@Override | |
public void onRunResult(boolean isSuccess) throws RemoteException { | |
callback.onRunResult(isSuccess); | |
} | |
@Override | |
public void onReturnString(String result) throws RemoteException { | |
callback.onReturnString(result); | |
} | |
@Override | |
public void onRaiseException(int code, String msg) throws RemoteException { | |
callback.onRaiseException(code, msg); | |
} | |
}; | |
} | |
return null; | |
} | |
} |
Es necesario también que en tu MainActivity del proyecto android añadas esta línea
SunmiPrinter.Instance.InitPrinter(ApplicationContext);Esto iría antes de
LoadApplication(new App());
.
Perfecto, ya me está funcionando el método PrintText(). Ahora trataré de probar y trabajar con las demás funcionalidades.
Una vez más, muchas gracias por tus aportes.
Hola, al generar el modulo, estoy teniendo este error:
public void commitPrint(com.sunmi.trans.TransBean[] transbean, woyou.aidlservice.jiuiv5.ICallback callback) throws android.os.RemoteException;
Hi guys,
Any sample of complete interface to SUNMI V2 smartcard reader with printer on xamarin form ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Desafortunadamente es la parte que me falta completar del post, con el dll ya tienes la instancia de la clase creada en android disponible para usar en el proyecto android de tu solución, tu clase Printer debe usarlas y cuando ejecutes el botón se llaman esas funciones que en este momento tienes vacías, tu implementación tendría que ser algo similar a lo siguiente:
el objeto
sunmi
tiene todos los métodos que se declararon en el dll, por lo que puedes ampliar tu interface con todos los métodos que quieras usar, personalmente implementéTaskCompletionSource
para poder devolver unTask
y usar async/await pero este es el ejemplo más corto para que ya le añadas lo que necesites.Es necesario también que en tu MainActivity del proyecto android añadas esta línea
Esto iría antes de
LoadApplication(new App());
.