我不太确定最好的方法是什么,但基本上我希望能够将Android手机中的角色发送到插入Arduino的蓝牙模块。我对Arduino结束没有任何问题,但是我遇到了Android位问题。
到目前为止,我所拥有的只是图像上的点击事件,当我点击该图像时,我想发送一个字符,例如,1。
这是我目前的代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class ToggleLightsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void imageClick(View view)
{
}
}
我应该指出我是Android的初学者,你可能已经猜到了!
提前致谢。
答案 0 :(得分:1)
Android蓝牙代码的起点是"Bluetooth Chat example"
答案 1 :(得分:1)
import android.app.Activity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class Bluetooth extends Activity{
private static final String TAG = "THINBTCLIENT";
private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
private static String address = "00:06:66:45:0E:93";
private static final UUID MY_UUID =UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
public InputStream ins=null;
public static String status;
static int count_speech=0;
static String ipadd="";
static int count_ip=0;
static String inst_data="";
static char ret;
static String Data="";
static int count=0;
Reading rd;
static int ch;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.bluetooth);
Bundle extras = getIntent().getExtras();
status=extras.getString("status");
// Data=String.valueOf(a);
if(extras !=null)
{
show("Inside Bt Activity");
Log.e(TAG, Data);
Toast.makeText(getApplicationContext(), "the value is " + Data, Toast.LENGTH_SHORT).show();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null)
{
finish();
return;
}
if (!mBluetoothAdapter.isEnabled())
{
Toast.makeText(this, "Please enable your BT and re-runprogram.", Toast.LENGTH_LONG).show();
finish();
return;
}
BluetoothDevice device =mBluetoothAdapter.getRemoteDevice(address);
try
{
btSocket =device.createRfcommSocketToServiceRecord(MY_UUID);
}
catch (Exception e)
{
Log.e(TAG, "ON RESUME: Socket creation failed.", e);
}
try
{
btSocket.connect();
Log.e(TAG, "ON RESUME: BT connection established, datatransfer link open.");
Toast.makeText(this, "Blutooth_socket is created", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
try
{
btSocket.close();
}
catch (IOException e2)
{
Log.e(TAG, "ON RESUME: Unable to close socket duringconnection failure", e2);
}
}
if(status.equals("something"))
{
try
{
char c='whatever character u want';
writeSucess(c);
/* outStream=btSocket.getOutputStream();
ins=btSocket.getInputStream();
// Thread.sleep(5000);
outStream.write(1);
Thread.sleep(5000);
outStream.write('2');
//rd=new Reading();
//new Thread(rd). start( );
//new Thread(Writing). start( );
*/
}
catch (Exception e)
{
Log.e(TAG, "ON RESUME: Output stream creation failed.",e);
}
thr = e在上面的try块中注释的东西。创建一个方法并使用那些行forwriteSuccess