使用PrinterShare

时间:2017-10-11 10:52:46

标签: android printing android-wifi

我正在尝试在Android中打印PDF文件。

系统:Android 6.0。 要打印的应用程序:PrinterShare。 打印机:HP Deskjet 3720。 设备:摩托罗拉MotoG 2。

我创建PDF文件并通过WIFI与打印机建立连接,但是当我启动PrinterShare的应用程序时,它从未选择打印机,我必须始终搜索它。 这是我的代码:

public class MainActivityCopiaBrotherSDK extends Activity {
private static final int PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION = 1001;
WifiManager mainWifi;

StringBuilder sb = new StringBuilder();
List<ScanResult> wifiList;

ArrayList<String> connections=new ArrayList<String>();
String networkSSID = new String();

Context cntx;

File f =new File( "/storage/emulated/0/Download/Informes/99901512_1507556054090.pdf");
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity_copia_brother_sdk);
    cntx = getApplicationContext();

    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
    }else{
        //do something, permission was previously granted; or legacy device
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
        int[] grantResults) {
    if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        conectarAImpresoraaaa(cntx);
    } else {
        Log.v("ERROR", "PERMISOOOO");
    }
}

public void conectarAImpresoraaaa(Context c){
    try {
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    } catch (Exception e) {
        e.printStackTrace();
    }
    boolean wifiEnabled = mainWifi.isWifiEnabled();
    if(wifiEnabled){
        mainWifi.setWifiEnabled(false);
        mainWifi.setWifiEnabled(true);
    }

    if(!wifiEnabled){
        mainWifi.setWifiEnabled(true);
    }

    if(!mainWifi.getConnectionInfo().getSSID().contains("DeskJet")){


        boolean resutltadoStartScanBoolean = mainWifi.startScan();

        if(resutltadoStartScanBoolean){
            Handler handler = new Handler(); 
            handler.postDelayed(new Runnable() {
                @Override 
                public void run() { 
                    despuesDeScan(cntx); 
                } 
            }, 2000);

        } else{
            Log.v("ERROR", "FALLO ESCANEANDO WIFI");
        }
    }
}

public void despuesDeScan(Context c){
    wifiList = mainWifi.getScanResults();
    for(int i = 0; i < wifiList.size(); i++)
    {
        if(wifiList.get(i).SSID.contains("DeskJet")){
            connections.add(wifiList.get(i).SSID);
            networkSSID= wifiList.get(i).SSID;
            break;
        }
    }
    WifiConfiguration conf = new WifiConfiguration();
    if((null!=networkSSID)&&(""!=networkSSID)){
        conf.SSID = "\"" + networkSSID + "\"";
        conf.preSharedKey = "\""+ "12345678" +"\"";

        WifiManager wifiManager = (WifiManager)c.getSystemService(Context.WIFI_SERVICE); 
        wifiManager.addNetwork(conf);
        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for( WifiConfiguration i : list ) {
            if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();               
                break;
            }           
        }
    }


    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setPackage("com.dynamixsoftware.printershare");
    i.setDataAndType(Uri.fromFile(f),"application/pdf");
    startActivity(i);
}

}

如何对Priner分享我想要打印的打印机?

0 个答案:

没有答案