Java Question SimpleLibraryCompiler for brither print

buras3

Active Member
Licensed User
Longtime User
Hey

I made a library from sdk file from brother site
with SimpleLibraryCompiler and this java code

every time i try to print i get ERROR_CONNECT_SOCKET_FAILED
but if i print with brother sdk demo app with the same settings , I can print

what can be the problem ?
tanks

package michael.vegs;
import com.brother.ptouch.sdk.*;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;

@Version(3.0f)
@ShortName("BrotherPrintSDK")
@DependsOn(values={"BrotherPrintLibrary"})
public class BrotherPrintSDK {

public String print(String filePath,String printerIP){

Printer myPrinter = new Printer();
PrinterInfo myPrinterInfo = new PrinterInfo();
PrinterStatus printResult = new PrinterStatus();
LabelInfo mLabelInfo=new LabelInfo();


myPrinterInfo = myPrinter.getPrinterInfo();
myPrinterInfo.printerModel=PrinterInfo.Model.QL_720NW;

myPrinterInfo.printMode=PrinterInfo.PrintMode.FIT_TO_PAGE;
myPrinterInfo.port=PrinterInfo.Port.NET;
myPrinterInfo.paperSize = PrinterInfo.PaperSize.CUSTOM;
myPrinterInfo.orientation = PrinterInfrientation.PORTRAIT;
myPrinterInfo.numberOfCopies = 1;
myPrinterInfo.ipAddress=printerIP;
//myPrinter.skipStatusCheck=true;
mLabelInfo.labelNameIndex=LabelInfo.QL700.valueOf("W29H90").ordinal();
//mLabelInfo.labelNameIndex=5;
mLabelInfo.isAutoCut = true;
mLabelInfo.isEndCut =true;

myPrinter.setPrinterInfo(myPrinterInfo);
myPrinter.setLabelInfo(mLabelInfo);
printResult=myPrinter.printFile(filePath);
return printResult.errorCode.toString();
}}
 

buras3

Active Member
Licensed User
Longtime User
now i changed targetSdkVersion from 8 to 14
and i get java error - android.os.NetworkOnMainThreadException

Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:michael.vegs
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (cust) Create, isFirst = true **
** Activity (cust) Resume **
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:566)
at java.net.Socket.<init>(Socket.java:225)
at com.brother.ptouch.sdk.connection.WifiConnection.startConnection(WifiConnection.java:170)
at com.brother.ptouch.sdk.connection.WifiConnection.open(WifiConnection.java:47)
at com.brother.ptouch.sdk.Printer.init(Printer.java:1681)
at com.brother.ptouch.sdk.Printer.printImage(Printer.java:362)
at com.brother.ptouch.sdk.Printer.sendPrintData(Printer.java:328)
at com.brother.ptouch.sdk.Printer.printFile(Printer.java:311)
at michael.vegs.BrotherPrintSDK.print(BrotherPrintSDK.java:37)
at michael.vegs.cust._cmdsend2_click(cust.java:494)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
at android.view.View.performClick(View.java:3623)
at android.view.View$PerformClick.run(View.java:14325)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4512)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
at dalvik.system.NativeStart.main(Native Method)
android.os.NetworkOnMainThreadException
 

buras3

Active Member
Licensed User
Longtime User
i add
Dim p AsPhoneIf p.SdkVersion >= 9ThenDim r AsReflector
r.Target = r.CreateObject("android.os.StrictMode$ThreadPolicy$Builder")
r.Target = r.RunMethod("permitAll")
r.Target = r.RunMethod("build")
r.RunStaticMethod("android.os.StrictMode", "setThreadPolicy", _ArrayAs Object(r.Target), ArrayAsString("android.os.StrictMode$ThreadPolicy"))EndIf

and it's fix the problem for the java error but the same socket error
please help i am tring to fix this for 5 months now
 

DonManfred

Expert
Licensed User
Longtime User
please help i am tring to fix this for 5 months now
You started this thread TODAY

You can start with:
1. Use code tags when posting code!
2. Use quote-tags when posting quotes/logs!
3. Upload the source you want to wrap or at least post a link to it
4. Upload your java-project (ZIPped)
5. Seems that your code does not use a different thread for the communication.
Start with this tutorial: https://www.b4x.com/android/forum/threads/java-libraries-and-multithreading.12697/
 

buras3

Active Member
Licensed User
Longtime User
if i will add to b4aignore field , it would help ?
now it's empty
 

DonManfred

Expert
Licensed User
Longtime User
1 how can i do that?




2 how can i do that?
See the hint for the code tag. The quote tag is in the same dropdown.

5 i didn't understand that tutorial.
You should start understanding it as it is required to know to write libs which do the work in a different thread. Using the code from #5 is probably not the right solution.

Here an example: one of the methods of my Cloudrail-Library
B4X:
    public void getMetadata(final String filePath) {
        // TODO Auto-generated method stub
        new Thread(new Runnable() {
      @Override
      public void run() {
          CloudMetaData metadata = cloud.getMetadata(filePath);
                ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_metadata", true, new Object[] {filePath,metadata});
      }
        }).start();
    }

Basically it is THIS code which i run in a different thread.

B4X:
 CloudMetaData metadata = cloud.getMetadata(filePath);
and then i raise an event in b4a from the thread instead of using a return value

B4X:
 ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_metadata", true, new Object[] {filePath,metadata});
This code does not raise an networkonmainthread-exception as it does not run on the mainthread. It is a async call.
 
Last edited:

buras3

Active Member
Licensed User
Longtime User
I tank you ,but i really don't understand Java .
I made an app that print with another app intend but you need to press back 5 times to print again .
This library is for 1 app only.

Basically it is THIS code which i run in a different thread.
B4X:
CloudMetaData metadata = cloud.getMetadata(filePath);
Can i add this code to my code and it will help or it's much more complicated ?
B4X:
package michael.vegs;
import com.brother.ptouch.sdk.*;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;

@Version(3.0f)
@ShortName("BrotherPrintSDK")
@DependsOn(values={"BrotherPrintLibrary"})
public class BrotherPrintSDK {
  
   public String print(String filePath,String printerIP){
    
     Printer myPrinter = new Printer();
     PrinterInfo myPrinterInfo = new PrinterInfo();
     PrinterStatus printResult = new PrinterStatus();
     LabelInfo mLabelInfo=new LabelInfo();
    
    
       myPrinterInfo = myPrinter.getPrinterInfo();
       myPrinterInfo.printerModel=PrinterInfo.Model.QL_720NW;
      
       myPrinterInfo.printMode=PrinterInfo.PrintMode.FIT_TO_PAGE;
       myPrinterInfo.port=PrinterInfo.Port.NET;
       myPrinterInfo.paperSize = PrinterInfo.PaperSize.CUSTOM;
       myPrinterInfo.orientation = PrinterInfo.Orientation.PORTRAIT;
       myPrinterInfo.numberOfCopies = 1;
       myPrinterInfo.ipAddress=printerIP;
        //myPrinter.skipStatusCheck=true;
     mLabelInfo.labelNameIndex=LabelInfo.QL700.valueOf("W29H90").ordinal();
       //mLabelInfo.labelNameIndex=5;
       mLabelInfo.isAutoCut = true;
       mLabelInfo.isEndCut =true;
      
       myPrinter.setPrinterInfo(myPrinterInfo);
       myPrinter.setLabelInfo(mLabelInfo);
       printResult=myPrinter.printFile(filePath);
       return printResult.errorCode.toString();
     }}
 

buras3

Active Member
Licensed User
Longtime User
I added the code
and still no printing
B4X:
package michael.vegs;
import com.brother.ptouch.sdk.*;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;

@Version(3.0f)
@ShortName("BrotherPrintSDK")
@DependsOn(values={"BrotherPrintLibrary"})
public class BrotherPrintSDK {
  
   public void print( final String filePath,final String printerIP){
                    new Thread(new Runnable() {
     @Override
       public void run() {
     Printer myPrinter = new Printer();
     PrinterInfo myPrinterInfo = new PrinterInfo();
     PrinterStatus printResult = new PrinterStatus();
     LabelInfo mLabelInfo=new LabelInfo();
    
    
       myPrinterInfo = myPrinter.getPrinterInfo();
       myPrinterInfo.printerModel=PrinterInfo.Model.QL_720NW;
      
       myPrinterInfo.printMode=PrinterInfo.PrintMode.FIT_TO_PAGE;
       myPrinterInfo.port=PrinterInfo.Port.NET;
       myPrinterInfo.paperSize = PrinterInfo.PaperSize.CUSTOM;
       myPrinterInfo.orientation = PrinterInfo.Orientation.PORTRAIT;
       myPrinterInfo.numberOfCopies = 1;
       myPrinterInfo.ipAddress=printerIP;
        //myPrinter.skipStatusCheck=true;
     mLabelInfo.labelNameIndex=LabelInfo.QL700.valueOf("W29H90").ordinal();
       //mLabelInfo.labelNameIndex=5;
       mLabelInfo.isAutoCut = true;
       mLabelInfo.isEndCut =true;
      
       myPrinter.setPrinterInfo(myPrinterInfo);
       myPrinter.setLabelInfo(mLabelInfo);

    
       printResult=myPrinter.printFile(filePath);
      // return printResult.errorCode.toString();
      //return "";
       //ba.raiseEventFromDifferentThread(this, null, 0, eventName + "_metadata", true, new Object[] {printResult.errorCode.toString()});
           }
        }).start();
    }
     }
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…