Java Android-Arduino蓝牙通信
嗨,我是android studio和java的新手,希望能将数据从arduino读取到android 计划是让我的界面上的按钮部件向arduino发送信号,让我的flex传感器通过蓝牙将信息发送回android 我的蓝牙模块是HC-06,该代码的设计目的是让手机向arduino发送一个“*”,并向android发送一个随机值,但我希望该随机值显示在我界面的文本框中。当然,这种改变是要在java上发生的,但我不知道如何做到。请帮忙 这是我的arduino代码Java Android-Arduino蓝牙通信,java,android,bluetooth,arduino,Java,Android,Bluetooth,Arduino,嗨,我是android studio和java的新手,希望能将数据从arduino读取到android 计划是让我的界面上的按钮部件向arduino发送信号,让我的flex传感器通过蓝牙将信息发送回android 我的蓝牙模块是HC-06,该代码的设计目的是让手机向arduino发送一个“*”,并向android发送一个随机值,但我希望该随机值显示在我界面的文本框中。当然,这种改变是要在java上发生的,但我不知道如何做到。请帮忙 这是我的arduino代码 #include <Softw
#include <SoftwareSerial.h>
const int RX_PIN = 2;
const int TX_PIN = 3;
SoftwareSerial serial(RX_PIN, TX_PIN);
char commandChar;
void setup ()
{
serial.begin (9600);
andomSeed(analogRead(0))
}
void loop ()
{
if(serial.available())
{
commandChar = serial.read();
switch(commandChar)
{
case '*':
serial.print(random(1000) + "#");
break;
}
}
}
#包括
const int RX_PIN=2;
const int TX_PIN=3;
软件串行(RX_引脚、TX_引脚);
char命令char;
无效设置()
{
serial.begin(9600);
andomSeed(模拟读取(0))
}
空循环()
{
if(serial.available())
{
commandChar=serial.read();
开关(命令字符)
{
案例“*”:
串行打印(随机(1000)+“#”);
打破
}
}
}
以下是我在android studio上完成的代码:
package com.example.ft.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
BluetoothDevice mDevice = null;
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mDevice = device;
}
}
ConnectThread mConnectThread = new ConnectThread(mDevice);
mConnectThread.start();
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
switch(msg.what) {
case 1:
String writeMessage = new String(writeBuf);
writeMessage = writeMessage.substring(begin, end);
break;
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
ConnectedThread mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
String s = "*";
write(s.getBytes());
byte[] buffer = new byte[1024];
int begin = 0;
int bytes = 0;
while (true) {
try {
bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);
for(int i = begin; i < bytes; i++) {
if(buffer[i] == "#".getBytes()[0]) {
mHandler.obtainMessage(1, begin, i, buffer).sendToTarget();
begin = i + 1;
if(i == bytes - 1) {
bytes = 0;
begin = 0;
}
}
}
} catch (IOException e) {
break;
}
}
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
package com.example.ft.myapplication;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.Intent;
导入android.os.Handler;
导入android.os.Message;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.Toast;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Set;
导入java.util.UUID;
公共类MainActivity扩展了AppCompatActivity{
蓝牙适配器mBluetoothAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null){
//设备不支持蓝牙
}
如果(!mBluetoothAdapter.isEnabled()){
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent,1);
}
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
Bluetooth设备mDevice=null;
如果(pairedDevices.size()>0){
用于(蓝牙设备:pairedDevices){
mDevice=设备;
}
}
ConnectThread mConnectThread=新的ConnectThread(mDevice);
mConnectThread.start();
}
Handler mHandler=新处理程序(){
@凌驾
公共无效handleMessage(消息消息消息){
字节[]writeBuf=(字节[])msg.obj;
int begin=(int)msg.arg1;
int end=(int)msg.arg2;
开关(msg.what){
案例1:
String writeMessage=新字符串(writeBuf);
writeMessage=writeMessage.substring(开始、结束);
打破
}
}
};
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
私有类ConnectThread扩展线程{
私人最终蓝牙插座mmSocket;
专用最终蓝牙设备;
私有最终UUID MY_UUID=UUID.fromString(“00001101-0000-1000-8000-00805f9b34fb”);
公共连接线程(蓝牙设备){
BluetoothSocket tmp=null;
mmDevice=设备;
试一试{
tmp=device.createrFComSocketToServiceRecord(我的UUID);
}捕获(IOE){}
mmSocket=tmp;
}
公开募捐{
mBluetoothAdapter.cancelDiscovery();
试一试{
mmSocket.connect();
}捕获(IOException-connectException){
试一试{
mmSocket.close();
}捕获(IOException closeException){}
返回;
}
ConnectedThread mConnectedThread=新的ConnectedThread(mmSocket);
mConnectedThread.start();
}
公开作废取消(){
试一试{
mmSocket.close();
}捕获(IOE){}
}
}
私有类ConnectedThread扩展线程{
私人最终蓝牙插座mmSocket;
私有最终输入流mmInStream;
私有最终输出流mmOutStream;
公共连接线程(BluetoothSocket){
mmSocket=插座;
InputStream tmpIn=null;
OutputStream tmpOut=null;
试一试{
tmpIn=socket.getInputStream();
tmpOut=socket.getOutputStream();
}捕获(IOE){}
mmInStream=tmpIn;
mmOutStream=tmpOut;
}
公开募捐{
字符串s=“*”;
写入(s.getBytes());
字节[]缓冲区=新字节[1024];
int begin=0;
int字节=0;
while(true){
试一试{
bytes+=mmInStream.read(buffer,bytes,buffer.length-bytes);
for(int i=begin;i