从Arduino向Android发送数据
我一直试图通过蓝牙模块HC-05将数据从arduino发送到Android应用程序,但我无法从arduino获取任何显示在应用程序中的读数 arduino代码是:从Arduino向Android发送数据,android,arduino,Android,Arduino,我一直试图通过蓝牙模块HC-05将数据从arduino发送到Android应用程序,但我无法从arduino获取任何显示在应用程序中的读数 arduino代码是: void setup() { Serial.begin(9600); } void loop() { char c; if(Serial.available()) { c = Serial.read(); Serial.print(c); } } Android代码是: import android.
void setup() {
Serial.begin(9600);
}
void loop()
{
char c;
if(Serial.available())
{
c = Serial.read();
Serial.print(c);
}
}
Android代码是:
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends Activity {
// private final String DEVICE_NAME="MyBTBee";
private final String DEVICE_ADDRESS="30:14:12:18:01:38";
private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");//Serial Port Service ID
private BluetoothDevice device;
private BluetoothSocket socket;
private OutputStream outputStream;
private InputStream inputStream;
Button startButton, sendButton,clearButton,stopButton;
TextView textView;
EditText editText;
boolean deviceConnected=false;
Thread thread;
byte buffer[];
int bufferPosition;
boolean stopThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.buttonStart);
sendButton = (Button) findViewById(R.id.buttonSend);
clearButton = (Button) findViewById(R.id.buttonClear);
stopButton = (Button) findViewById(R.id.buttonStop);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
setUiEnabled(false);
}
public void setUiEnabled(boolean bool)
{
startButton.setEnabled(!bool);
sendButton.setEnabled(bool);
stopButton.setEnabled(bool);
textView.setEnabled(bool);
}
public boolean BTinit()
{
boolean found=false;
BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
}
if(!bluetoothAdapter.isEnabled())
{
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
if(bondedDevices.isEmpty())
{
Toast.makeText(getApplicationContext(),"Please Pair the Device first",Toast.LENGTH_SHORT).show();
}
else
{
for (BluetoothDevice iterator : bondedDevices)
{
if(iterator.getAddress().equals(DEVICE_ADDRESS))
{
device=iterator;
found=true;
break;
}
}
}
return found;
}
public boolean BTconnect()
{
boolean connected=true;
try {
socket = device.createRfcommSocketToServiceRecord(PORT_UUID);
socket.connect();
} catch (IOException e) {
e.printStackTrace();
connected=false;
}
if(connected)
{
try {
outputStream=socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream=socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
return connected;
}
public void onClickStart(View view) {
if(BTinit())
{
if(BTconnect())
{
setUiEnabled(true);
deviceConnected=true;
beginListenForData();
textView.append("\nConnection Opened!\n");
}
}
}
void beginListenForData()
{
final Handler handler = new Handler();
stopThread = false;
buffer = new byte[1024];
Thread thread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopThread)
{
try
{
int byteCount = inputStream.available();
if(byteCount > 0)
{
byte[] rawBytes = new byte[byteCount];
inputStream.read(rawBytes);
final String string=new String(rawBytes,"UTF-8");
handler.post(new Runnable() {
public void run()
{
textView.append(string);
}
});
}
}
catch (IOException ex)
{
stopThread = true;
}
}
}
});
thread.start();
}
public void onClickSend(View view) {
String string = editText.getText().toString();
string.concat("\n");
try {
outputStream.write(string.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
textView.append("\nSent Data:"+string+"\n");
}
public void onClickStop(View view) throws IOException {
stopThread = true;
outputStream.close();
inputStream.close();
socket.close();
setUiEnabled(false);
deviceConnected=false;
textView.append("\nConnection Closed!\n");
}
public void onClickClear(View view) {
textView.setText("");
}
}
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.os.Handler;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Set;
导入java.util.UUID;
公共类MainActivity扩展了活动{
//专用最终字符串设备\u NAME=“MyBTBee”;
专用最终字符串设备\u ADDRESS=“30:14:12:18:01:38”;
专用最终UUID端口_UUID=UUID.fromString(“00001101-0000-1000-8000-00805f9b34fb”);//串行端口服务ID
私人蓝牙设备;
私人蓝牙插座;
私有输出流输出流;
私有输入流输入流;
按钮开始按钮、发送按钮、清除按钮、停止按钮;
文本视图文本视图;
编辑文本编辑文本;
布尔设备连接=假;
螺纹;
字节缓冲区[];
int缓冲位置;
布尔停止线程;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
开始按钮=(按钮)findViewById(R.id.buttonStart);
sendButton=(Button)findviewbyd(R.id.buttonSend);
clearButton=(按钮)findViewById(R.id.buttonClear);
stopButton=(按钮)findViewById(R.id.buttonStop);
editText=(editText)findViewById(R.id.editText);
textView=(textView)findViewById(R.id.textView);
setUiEnabled(false);
}
公共void setUiEnabled(布尔布尔布尔值)
{
startButton.setEnabled(!bool);
sendButton.setEnabled(bool);
停止按钮。设置启用(布尔);
textView.setEnabled(bool);
}
公共布尔值BTinit()
{
布尔值=false;
BluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
Toast.makeText(getApplicationContext(),“设备不支持蓝牙”,Toast.LENGTH\u SHORT.show();
}
如果(!bluetoothAdapter.isEnabled())
{
Intent enableAdapter=新的Intent(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(enableAdapter,0);
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
Set-bondedDevices=bluetoothAdapter.getBondedDevices();
if(bondedDevices.isEmpty())
{
Toast.makeText(getApplicationContext(),“请先配对设备”,Toast.LENGTH\u SHORT.show();
}
其他的
{
for(蓝牙设备迭代器:bondedDevices)
{
if(iterator.getAddress().equals(设备地址))
{
设备=迭代器;
发现=真;
打破
}
}
}
发现退货;
}
公共布尔值BTconnect()
{
布尔连接=真;
试一试{
socket=device.createrFComSocketToServiceRecord(端口号);
socket.connect();
}捕获(IOE异常){
e、 printStackTrace();
连接=错误;
}
如果(已连接)
{
试一试{
outputStream=socket.getOutputStream();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
inputStream=socket.getInputStream();
}捕获(IOE异常){
e、 printStackTrace();
}
}
回流连接;
}
公共void onClickStart(视图){
if(BTinit())
{
if(BTconnect())
{
setUiEnabled(true);
deviceConnected=true;
beginListenForData();
textView.append(“\n连接已打开!\n”);
}
}
}
void beginListenForData()
{
最终处理程序=新处理程序();
stopThread=false;
缓冲区=新字节[1024];
Thread Thread=新线程(new Runnable()
{
公开募捐
{
而(!Thread.currentThread().isInterrupted()&&!stopThread)
{
尝试
{
int byteCount=inputStream.available();
如果(字节数>0)
{
字节[]rawBytes=新字节[字节计数];
inputStream.read(rawBytes);
最终字符串=新字符串(原始字节,“UTF-8”);
handler.post(新的Runnable(){
公开募捐
{
textView.append(字符串);
}
});
}
}
捕获(IOEX异常)
{
stopThread=true;
}
}
}
});
thread.start();
}
公共void onClickSend(查看){
String String=editText.getText().toString();
string.concat(“\n”);
试一试{
write(string.getBytes());
}捕获(IOE异常){
e、 printStackTrace();
}
textView.ap