因此,我们正在为视障人士开发智能鞋,我们在从arduino到Android应用程序的数据传输方面遇到了问题。我们正在使用振动电机,它是arduino代码中的motorPin。 蓝牙模块连接正确,其上的LED闪烁,所以我认为Android应用程序存在问题。 下面是arduino nano代码和android代码!提前致谢
#include<SoftwareSerial.h>
// defines pins numbers
const int trigPin = 6;
const int echoPin = 7;
const int motorPin = 8;
const int tx = 2;
const int rx = 3;
// defines variables
long duration;
int distance;
SoftwareSerial bluetooth(rx,tx);
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(motorPin, OUTPUT); //Sets the motorPin as an Output
Serial.begin(9600); // Starts the serial communication
bluetooth.begin(9600);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in
microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if (distance < 30)
{
digitalWrite(motorPin, HIGH);
delay(1000);
bluetooth.print("1");
delay(1000);
}
else
{
digitalWrite(motorPin, LOW);
delay(1000);
}
}
这是android代码:MainActivity.java
package com.example.shreyagaddam.asfb;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.shreyagaddam.asfb.R;
import java.io.InputStream;
//import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,TextToSpeech.OnInitListener {
Button open, close;
TextView out;
EditText input;
BluetoothAdapter ba;
BluetoothDevice bd;
BluetoothSocket bs;
InputStream is;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
volatile boolean stopWorker;
String speechdata = "";
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
out = (TextView) findViewById(R.id.data);
open = (Button) findViewById(R.id.openBT);
close = (Button) findViewById(R.id.closeBT);
open.setOnClickListener(this);
close.setOnClickListener(this);
tts = new TextToSpeech(this, this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.openBT) {
try {
findBT();
openBT();
} catch (Exception e) {
}
} else if (v.getId() == R.id.closeBT) {
try {
closeBT();
} catch (Exception e) {
}
} else {
try {
String data = input.getText().toString();
} catch (Exception e) {
}
}
}
void findBT() throws Exception {
ba = BluetoothAdapter.getDefaultAdapter();
if (!ba.isEnabled()) {
Intent i1 = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(i1);
}
if (ba.isEnabled()) {
Set<BluetoothDevice> paired = ba.getBondedDevices();
if (paired.size() > 0) {
for (BluetoothDevice dev : paired) {
if (dev.getName().equals("HC-05")) {
bd = dev;
break;
}
}
}
}
}
void openBT() throws Exception {
UUID uuid = UUID.fromString("1101-0000-1000-8000-00805f9b34fb");
bs = bd.createRfcommSocketToServiceRecord(uuid);
bs.connect();
is = bs.getInputStream();
recieve();
Toast.makeText(this, "Socket Opened", Toast.LENGTH_LONG).show();
}
void recieve() throws Exception {
final Handler handler = new Handler();
final byte delimiter = 32;
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
int bytesAvailable = is.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
is.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable() {
public void run() {
out.setText(data);
if (data.equals("1")) {
speechdata = "Obstacle";
speak();
}
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (Exception ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
}
void callme() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, 1);
speechdata = "";
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
speechdata = text.get(0);
Toast.makeText(this, speechdata, Toast.LENGTH_SHORT).show();
if (speechdata.contains("restaurant")) {
Uri gmmIntentUri = Uri.parse("geo:0,0?q=restaurants");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else if (speechdata.contains("college")) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=Sreenidhi+Institute+of+Science+&+Technology,+Hyderabad+India");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else if (speechdata.contains("cafeteria")) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=SNIST+Cafeteria,+Hyderabad");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else if (speechdata.contains("canteen")) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=SNIST+College+Canteen,+Hyderbad");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else if (speechdata.contains("Block 2")) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=Departments+of+Computer+science+&+Mechanical,+Hyderabad");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else if (speechdata.contains("arts club")) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=SNIST+Arts+Club,+Hyderbad");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
}
}
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
callme();
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
try {
findBT();
openBT();
} catch (Exception e) {
}
}
return super.onKeyDown(keyCode, event);
}
void closeBT() throws Exception {
stopWorker = true;
is.close();
bs.close();
Toast.makeText(this, "Socket closed", Toast.LENGTH_LONG).show();
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
speak();
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
void speak() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(speechdata, TextToSpeech.QUEUE_FLUSH, null, null);
}
}
}