Guest User

Untitled

a guest
Oct 29th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1.         package com.example.arduinoprzesyl;
  2.  
  3.  
  4. import java.io.IOException;
  5.  
  6. import com.hoho.android.usbserial.driver.UsbSerialDriver;
  7. import com.hoho.android.usbserial.driver.UsbSerialProber;
  8.  
  9.  
  10. import android.app.Activity;
  11. import android.content.Context;
  12. import android.hardware.usb.UsbManager;
  13. import android.os.Bundle;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.TextView;
  17.  
  18. /*
  19. import java.io.IOException;
  20.  
  21. import com.hoho.android.usbserial.driver.UsbSerialDriver;
  22. import com.hoho.android.usbserial.driver.UsbSerialProber;
  23. import android.app.Activity;
  24. import android.content.Context;
  25. import android.hardware.usb.UsbManager;
  26. import android.os.Bundle;
  27. import android.util.Log;
  28. import android.view.Menu;
  29. import android.view.MenuItem;
  30. import android.widget.TextView;*/
  31.  
  32.  
  33. public class MainActivity extends Activity {
  34.  
  35.     private TextView t1;
  36.     private TextView t2;
  37.     private Button openButton;
  38.     private Button closeButton;
  39.     private Button downloadButton;
  40.    
  41.     public UsbManager manager;
  42.     public UsbSerialDriver driver;
  43.    
  44.    
  45.     @Override
  46.     protected void onCreate(Bundle savedInstanceState) {
  47.         super.onCreate(savedInstanceState);
  48.         setContentView(R.layout.activity_main);
  49.        
  50.         t1 = (TextView) findViewById(R.id.readView);
  51.         t2 = (TextView) findViewById(R.id.controlView);
  52.         openButton = (Button) findViewById(R.id.openButton);
  53.     openButton.setOnClickListener(new OnClickListener{
  54.         @Override
  55.         public void onClick(){
  56.             try{
  57.             open();
  58.             }catch(IOException e){
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.     });
  63.     closeButton.setOnClickListener(new OnClickListener{
  64.         @Override
  65.         public void onClick(){
  66.             try{
  67.             close();
  68.             }catch(IOException e){
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.     });
  73.         closeButton = (Button) findViewById(R.id.closeButton);
  74.         downloadButton = (Button) findViewById(R.id.downloadButton);
  75.        
  76.     }
  77.    
  78.    
  79.     public void open() throws IOException{
  80.        
  81.         manager = (UsbManager) getSystemService(Context.USB_SERVICE);
  82.         driver = UsbSerialProber.acquire(manager);
  83.         if  (driver != null){
  84.             t2.setText("Otwieram");
  85.             driver.open();
  86.             driver.setParameters(11520, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
  87.         }
  88.     }
  89.    
  90.     public void close() throws IOException{
  91.         if  (driver != null){
  92.             driver.close();
  93.             t2.setText("Zamykam");
  94.         }
  95.     }
  96.    
  97.     public void download(View v) throws IOException{
  98.        
  99.         if  (driver != null){  
  100.             t2.setText("Pobieram");
  101.             byte buffer[] = new byte[16];
  102.             int numBytesRead = driver.read(buffer, 1000);
  103.             t1.setText(Integer.toString(numBytesRead));
  104.         }
  105.     }
  106.    
  107.    
  108.  
  109.  
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment