arduino java 通信,安卓USB串口通信 Arduino

【实例简介】

OkUSB

一个简洁的Android串口通信框架。

## 功能简介

- 支持设置波特率

- 支持设置数据位

- 支持设置停止位

- 支持设置校验位

- 支持DTS和RTS

- 支持串口连接状态监听

【实例截图】

【核心代码】

package me.zhouzhuo810.okusbdemo;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import me.zhouzhuo810.okusbdemo.TaskerIntent;

import com.hoho.android.usbserial.driver.UsbSerialPort;

import java.nio.charset.Charset;

import me.zhouzhuo810.okusb.USB;

public class MainActivity extends AppCompatActivity {

private USB usb;

private EditText etData;

private Button btnSend;

private TextView tvResult;

private Button btnClear;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

etData = (EditText) findViewById(R.id.et_data);

btnSend = (Button) findViewById(R.id.btn_send);

btnClear = (Button) findViewById(R.id.btn_clear);

tvResult = (TextView) findViewById(R.id.tv_result);

usb = new USB.USBBuilder(this)

.setBaudRate(9600)

.setDataBits(8)

.setStopBits(UsbSerialPort.STOPBITS_1)

.setParity(UsbSerialPort.PARITY_NONE)

.setMaxReadBytes(20)

.setReadDuration(500)

.setDTR(false)

.setRTS(false)

.build();

usb.setOnUsbChangeListener(new USB.OnUsbChangeListener() {

@Override

public void onUsbConnect() {

Toast.makeText(MainActivity.this, "conencted", Toast.LENGTH_SHORT).show();

}

@Override

public void onUsbDisconnect() {

Toast.makeText(MainActivity.this, "disconencted", Toast.LENGTH_SHORT).show();

}

@Override

public void onDataReceive(byte[] data) {

// final String rData = DataUtil.byteArrayToString(data);

// tvResult.append(rData);

String ss=new String(data);

TaskerIntent i=new TaskerIntent("ty0");

//String x=filep "下载成功";

i.addParameter( ss);

sendBroadcast(i);

//int a=Integer.parseInt(ss);

Toast.makeText(MainActivity.this,ss, Toast.LENGTH_SHORT).show();

// tvResult.setText(new String(data, Charset.forName("GB2312")));

}

@Override

public void onUsbConnectFailed() {

Toast.makeText(MainActivity.this, "connect fail", Toast.LENGTH_SHORT).show();

}

@Override

public void onPermissionGranted() {

Toast.makeText(MainActivity.this, "permission ok", Toast.LENGTH_SHORT).show();

}

@Override

public void onPermissionRefused() {

Toast.makeText(MainActivity.this, "permission fail", Toast.LENGTH_SHORT).show();

}

@Override

public void onDriverNotSupport() {

Toast.makeText(MainActivity.this, "no driver", Toast.LENGTH_SHORT).show();

}

@Override

public void onWriteDataFailed(String error) {

Toast.makeText(MainActivity.this, "write fail" error, Toast.LENGTH_SHORT).show();

}

@Override

public void onWriteSuccess(int num) {

Toast.makeText(MainActivity.this, "write ok " num, Toast.LENGTH_SHORT).show();

}

});

btnSend.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String str = etData.getText().toString();

usb.writeData(str.getBytes(Charset.forName("GB2312")), 500);

}

});

btnClear.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

tvResult.setText("");

}

});

}

@Override

protected void onDestroy() {

super.onDestroy();

if (usb != null) {

usb.destroy();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值