Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.arduinoprzesyl;
- import java.io.IOException;
- import com.hoho.android.usbserial.driver.UsbSerialDriver;
- import com.hoho.android.usbserial.driver.UsbSerialProber;
- import android.app.Activity;
- import android.content.Context;
- import android.hardware.usb.UsbManager;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- /*
- import java.io.IOException;
- import com.hoho.android.usbserial.driver.UsbSerialDriver;
- import com.hoho.android.usbserial.driver.UsbSerialProber;
- import android.app.Activity;
- import android.content.Context;
- import android.hardware.usb.UsbManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;*/
- public class MainActivity extends Activity {
- private TextView t1;
- private TextView t2;
- private Button openButton;
- private Button closeButton;
- private Button downloadButton;
- public UsbManager manager;
- public UsbSerialDriver driver;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- t1 = (TextView) findViewById(R.id.readView);
- t2 = (TextView) findViewById(R.id.controlView);
- openButton = (Button) findViewById(R.id.openButton);
- openButton.setOnClickListener(new OnClickListener{
- @Override
- public void onClick(){
- try{
- open();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- });
- closeButton.setOnClickListener(new OnClickListener{
- @Override
- public void onClick(){
- try{
- close();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- });
- closeButton = (Button) findViewById(R.id.closeButton);
- downloadButton = (Button) findViewById(R.id.downloadButton);
- }
- public void open() throws IOException{
- manager = (UsbManager) getSystemService(Context.USB_SERVICE);
- driver = UsbSerialProber.acquire(manager);
- if (driver != null){
- t2.setText("Otwieram");
- driver.open();
- driver.setParameters(11520, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_NONE);
- }
- }
- public void close() throws IOException{
- if (driver != null){
- driver.close();
- t2.setText("Zamykam");
- }
- }
- public void download(View v) throws IOException{
- if (driver != null){
- t2.setText("Pobieram");
- byte buffer[] = new byte[16];
- int numBytesRead = driver.read(buffer, 1000);
- t1.setText(Integer.toString(numBytesRead));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment