Sending a single char from android phone to arduino over bluetooth
hi,
i'm working on project, want android phone send single char ("1") arduino on bluetooth connection. i've implemented sms listener , having trouble sending char arduino. module works, because i've tested ardudroid.
i followed this tutorial sending , receiving data on bluetooth no success. far connecting module.
this code tutorial:
could tell me how use or, maybe, did similar?
i thank in advance help.
i'm working on project, want android phone send single char ("1") arduino on bluetooth connection. i've implemented sms listener , having trouble sending char arduino. module works, because i've tested ardudroid.
i followed this tutorial sending , receiving data on bluetooth no success. far connecting module.
this code tutorial:
code: [select]
package com.example.bluetooth;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.set;
import java.util.uuid;
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.os.handler;
import android.os.message;
import android.util.log;
import android.view.menu;
import android.widget.toast;
public class mainactivity extends activity {
//you first have connect android phone arduino bt module.
bluetoothadapter mbluetoothadapter = null;
connectthread mconnectthread = null;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
bluetoothdevice mdevice = null;
//check if phone supports bluetooth
mbluetoothadapter = bluetoothadapter.getdefaultadapter();
if (mbluetoothadapter == null) {
// device not support bluetooth
}
toast.maketext(this, "bt supported", toast.length_short).show();
log.e("bluetooth", "bt supported");
// check if bluetooth enabled. if not, enable it.
if (!mbluetoothadapter.isenabled()) {
intent enablebtintent = new intent(bluetoothadapter.action_request_enable);
startactivityforresult(enablebtintent, 1);
}
toast.maketext(this, "bt enabled", toast.length_short).show();
log.e("bluetooth", "bt enabled");
// chech device connected with, @ beginning
set<bluetoothdevice> paireddevices = mbluetoothadapter.getbondeddevices();
if (paireddevices.size() > 0) {
toast.maketext(this, "paired bt device", toast.length_short).show();
log.e("bluetooth", "paired bt device");
(bluetoothdevice device : paireddevices) {
mdevice = device;
}
}
// source 7 - creating connection thread
mconnectthread = new connectthread(mdevice);
mconnectthread.start();
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate menu; adds items action bar if present.
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
//thread creates connection between arduino , phone
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;
}
//source 10 - create data transfer thread
connectedthread mconnectedthread = new connectedthread(mmsocket);
mconnectedthread.start();
}
public void cancel() {
try {
mmsocket.close();
} catch (ioexception e) { }
}
}
// end of connection thread
//thread sending , receiving messages
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() {
byte[] buffer = new byte[1024];
int begin = 0;
int bytes = 0;
while (true) {
try {
bytes += mminstream.read(buffer, bytes, buffer.length
- bytes);
(int = begin; < bytes; i++) {
if (buffer[i] == "#".getbytes()[0]) {
mhandler.obtainmessage(1, begin, i, buffer)
.sendtotarget();
begin = + 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) {
}
}
}
//end of thread sending , receiving
//source 9 - handler sending
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;
}
}
};
}
could tell me how use or, maybe, did similar?
i thank in advance help.
this college seminar report. there rather crude android code can at. if knew know now, code better.
https://www.sendspace.com/file/u6v6zk
https://www.sendspace.com/file/u6v6zk
Arduino Forum > Using Arduino > Programming Questions > Sending a single char from android phone to arduino over bluetooth
arduino
Comments
Post a Comment