kopia lustrzana https://github.com/felHR85/UsbSerial
added MainActivity to synchronous example
rodzic
d15adc3252
commit
5321490a3d
|
@ -1,9 +1,212 @@
|
|||
package com.felhr.serialportexamplesync;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* Created by Felipe Herranz(felhr85@gmail.com) on 17/9/16.
|
||||
*/
|
||||
public class MainActivity extends Activity{
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Set;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final int SYNC_READ = 3;
|
||||
|
||||
/*
|
||||
* Notifications from UsbService will be received here.
|
||||
*/
|
||||
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
switch (intent.getAction()) {
|
||||
case UsbService.ACTION_USB_PERMISSION_GRANTED: // USB PERMISSION GRANTED
|
||||
Toast.makeText(context, "USB Ready", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_USB_PERMISSION_NOT_GRANTED: // USB PERMISSION NOT GRANTED
|
||||
Toast.makeText(context, "USB Permission not granted", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_NO_USB: // NO USB CONNECTED
|
||||
Toast.makeText(context, "No USB connected", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_USB_DISCONNECTED: // USB DISCONNECTED
|
||||
Toast.makeText(context, "USB disconnected", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case UsbService.ACTION_USB_NOT_SUPPORTED: // USB NOT SUPPORTED
|
||||
Toast.makeText(context, "USB device not supported", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
private UsbService usbService;
|
||||
private TextView display;
|
||||
private EditText editText;
|
||||
private CheckBox box9600, box38400;
|
||||
private MyHandler mHandler;
|
||||
private final ServiceConnection usbConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
|
||||
usbService = ((UsbService.UsbBinder) arg1).getService();
|
||||
usbService.setHandler(mHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName arg0) {
|
||||
usbService = null;
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
mHandler = new MyHandler(this);
|
||||
|
||||
display = (TextView) findViewById(R.id.textView1);
|
||||
editText = (EditText) findViewById(R.id.editText1);
|
||||
Button sendButton = (Button) findViewById(R.id.buttonSend);
|
||||
sendButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (!editText.getText().toString().equals("")) {
|
||||
String data = editText.getText().toString();
|
||||
if (usbService != null) { // if UsbService was correctly binded, Send data
|
||||
usbService.write(data.getBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Button readButton = (Button) findViewById(R.id.buttonRead);
|
||||
readButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
byte[] buffer = usbService.read(10);
|
||||
mHandler.obtainMessage(SYNC_READ, buffer).sendToTarget();
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
});
|
||||
|
||||
box9600 = (CheckBox) findViewById(R.id.checkBox);
|
||||
box9600.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
if(box9600.isChecked())
|
||||
box38400.setChecked(false);
|
||||
else
|
||||
box38400.setChecked(true);
|
||||
}
|
||||
});
|
||||
|
||||
box38400 = (CheckBox) findViewById(R.id.checkBox2);
|
||||
box38400.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
if(box38400.isChecked())
|
||||
box9600.setChecked(false);
|
||||
else
|
||||
box9600.setChecked(true);
|
||||
}
|
||||
});
|
||||
|
||||
Button baudrateButton = (Button) findViewById(R.id.buttonBaudrate);
|
||||
baudrateButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(box9600.isChecked())
|
||||
usbService.changeBaudRate(9600);
|
||||
else
|
||||
usbService.changeBaudRate(38400);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
setFilters(); // Start listening notifications from UsbService
|
||||
startService(UsbService.class, usbConnection, null); // Start UsbService(if it was not started before) and Bind it
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
unregisterReceiver(mUsbReceiver);
|
||||
unbindService(usbConnection);
|
||||
}
|
||||
|
||||
private void startService(Class<?> service, ServiceConnection serviceConnection, Bundle extras) {
|
||||
if (!UsbService.SERVICE_CONNECTED) {
|
||||
Intent startService = new Intent(this, service);
|
||||
if (extras != null && !extras.isEmpty()) {
|
||||
Set<String> keys = extras.keySet();
|
||||
for (String key : keys) {
|
||||
String extra = extras.getString(key);
|
||||
startService.putExtra(key, extra);
|
||||
}
|
||||
}
|
||||
startService(startService);
|
||||
}
|
||||
Intent bindingIntent = new Intent(this, service);
|
||||
bindService(bindingIntent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||
}
|
||||
|
||||
private void setFilters() {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(UsbService.ACTION_USB_PERMISSION_GRANTED);
|
||||
filter.addAction(UsbService.ACTION_NO_USB);
|
||||
filter.addAction(UsbService.ACTION_USB_DISCONNECTED);
|
||||
filter.addAction(UsbService.ACTION_USB_NOT_SUPPORTED);
|
||||
filter.addAction(UsbService.ACTION_USB_PERMISSION_NOT_GRANTED);
|
||||
registerReceiver(mUsbReceiver, filter);
|
||||
}
|
||||
|
||||
/*
|
||||
* This handler will be passed to UsbService. Data received from serial port is displayed through this handler
|
||||
*/
|
||||
private static class MyHandler extends Handler {
|
||||
private final WeakReference<MainActivity> mActivity;
|
||||
|
||||
public MyHandler(MainActivity activity) {
|
||||
mActivity = new WeakReference<>(activity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
switch (msg.what) {
|
||||
case UsbService.MESSAGE_FROM_SERIAL_PORT:
|
||||
String data = (String) msg.obj;
|
||||
mActivity.get().display.append(data);
|
||||
break;
|
||||
case UsbService.CTS_CHANGE:
|
||||
Toast.makeText(mActivity.get(), "CTS_CHANGE",Toast.LENGTH_LONG).show();
|
||||
break;
|
||||
case UsbService.DSR_CHANGE:
|
||||
Toast.makeText(mActivity.get(), "DSR_CHANGE",Toast.LENGTH_LONG).show();
|
||||
break;
|
||||
case MainActivity.SYNC_READ:
|
||||
byte[] buffer = (byte[]) msg.obj;
|
||||
mActivity.get().display.append(new String(buffer));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@
|
|||
android:id="@+id/buttonRead"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Read"
|
||||
android:text="Read 10 bytes"
|
||||
android:layout_below="@+id/buttonSend"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_alignParentEnd="true"
|
||||
|
|
Ładowanie…
Reference in New Issue