通过蓝牙发送字符
问题描述:
我不确定这是什么最好的方式,但基本上我想能够从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的初学者,正如您可能已经猜到的一样!
在此先感谢。为Android蓝牙代码
答
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的事情。创建一个方法并使用这些行forwriteSuccess
+0
请提供一些解释以及您发布的代码。 –
那么你发现这个? –