本周末,我决定与arduino合作。目前我在将手机连接到Arduino时遇到了困难。我已成功将模块的地址传递给ledControl.java
。我正在使用AsyncTask来执行连接到arduino的操作。任何人都可以指导我。
注意:m无法连接任何具有蓝牙功能的设备。
import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.IOException;
import java.util.UUID;
/**
* Created by milind on 09/01/16.
*/
public class ledControl extends Activity {
Button btnOn, btnOff, btnDis;
SeekBar brightness;
String address = null;
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.led_control);
//receive the address of the bluetooth device
Intent newint = getIntent();
address = newint.getStringExtra("address");
Log.wtf("ledControl Address:", address); // Log value received.
//view of the ledControl layout
//call the widgtes
btnOn = (Button) findViewById(R.id.on);
btnOff = (Button) findViewById(R.id.off);
//btnDis = (Button)findViewById(R.id.button4);
//brightness = (SeekBar)findViewById(R.id.seekBar);
new ConnectBT().execute(address);
btnOn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
turnOnLed(); //method to turn on
}
});
btnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
turnOffLed(); //method to turn off
}
});
}
private void turnOffLed()
{
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write("1".toString().getBytes());
}
catch (IOException e)
{
msg("Error");
}
}
}
private void turnOnLed()
{
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write("2".toString().getBytes());
}
catch (IOException e)
{
msg("Error");
}
}
}
private class ConnectBT extends AsyncTask<String, Void, Void> // UI thread
{
private boolean ConnectSuccess = true; //if it's here, it's almost connected
@Override
protected void onPreExecute()
{
Log.i("ledControl","Device Connecting");
//progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please wait!!!"); //show a progress dialog
}
@Override
protected Void doInBackground(String... devices) //while the progress dialog is shown, the connection is done in background
{
try
{
if (btSocket == null || !isBtConnected)
{
Log.wtf("ledControl","AsyncTask Called");
myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
Log.wtf("AsyncTask:",address);
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(devices[0]);//connects to the device's address and checks if it's available
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();//start connection
Log.i("ledControl:","Connected to Device.");
}
}
catch (IOException e)
{
ConnectSuccess = false;//if the try failed, you can check the exception here
}
return null;
}
@Override
protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
{
super.onPostExecute(result);
if (!ConnectSuccess)
{
msg("Connection Failed. Is it a SPP Bluetooth? Try again."); // This toast is produced everytime.
finish();
}
else
{
msg("Connected.");
isBtConnected = true;
}
//progress.dismiss();
}
}
private void msg(final String s)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
});
}
}
最后,我得到了这个Toast消息。 “连接失败。它是SPP蓝牙吗?再试一次。”