I get a android.os.NetworkOnMainThreadException when I run this code the jar is provide by the printer manufacture. The error come right at the StarIOPort.searchPrinter. I don't get the error running the demo code provide from the manufacture.
I'm trying to see if there is any network printers available on the local network.
private void PortDiscovery(String interfaceName)
{
final EditText editPortName;
ArrayList<String> arrayPortName;
List<PortInfo> BTPortList;
List<PortInfo> TCPPortList;
final ArrayList<PortInfo> arrayDiscovery;
try {
if (true == interfaceName.equals("BT")) {
BTPortList = StarIOPort.searchPrinter("BT:");
for (PortInfo portInfo : BTPortList) {
arrayDiscovery.add(portInfo);
}
}
if (true == interfaceName.equals("LAN")) {
TCPPortList = StarIOPort.searchPrinter("TCP:");
for (PortInfo portInfo : TCPPortList) {
arrayDiscovery.add(portInfo);
}
}
arrayPortName = new ArrayList<String>();
for(PortInfo discovery : arrayDiscovery) {
String portName;
portName = discovery.getPortName();
if(discovery.getMacAddress().equals("") == false) {
portName += "\n - " + discovery.getMacAddress();
if(discovery.getModelName().equals("") == false) {
portName += "\n - " + discovery.getModelName();
}
}
arrayPortName.add(portName);
}
} catch (StarIOPortException e) {
e.printStackTrace();
}
editPortName = new EditText(this);
new AlertDialog.Builder(this).setTitle("Please Input Port Name").setView(editPortName).setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int button){
EditText portNameField = (EditText)findViewById(R.id.printerName);
portNameField.setText(editPortName.getText());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int button) {
}
})
.setItems(arrayPortName.toArray(new String[0]), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int select) {
EditText portNameField = (EditText)findViewById(R.id.printerName);portNameField.setText(arrayDiscovery.get(select).getPortName());
}
})
.show();
}