I was searching for answer like whole day I when I thought that I have one - it did not work. I read a lot of topics around here but I am still having problem. I can non send anything trough bluetooth.
package sk.example.arduinobtcontrol;
import java.util.Set;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;
import org.apache.http.conn.ConnectTimeoutException;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass.Device;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class ArduinoBTControl extends Activity {
private String adress = "00:12:12:04:41:66";
Button Vyhladaj,Pripoj,Switch;
TextView tex;
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice mDevice = mAdapter.getRemoteDevice(adress);
byte sendByte;
public OutputStream mOutputStream=null;
// static BluetoothSerialService blue;
private static final int REQUEST_ENABLE_BT = 3;
public static final UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arduino_btcontrol);
if(!mAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Vyhladaj = (Button)findViewById(R.id.button1);
Vyhladaj.setOnClickListener(new Button_Clicker());
Switch = (Button)findViewById(R.id.button2);
Switch.setOnClickListener(new Button_Clicker());
Pripoj = (Button)findViewById(R.id.button3);
Pripoj.setOnClickListener(new Button_Clicker());
tex = (TextView)findViewById(R.id.textView1);
}
class Button_Clicker implements Button.OnClickListener
{
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
{
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.startDiscovery();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for(BluetoothDevice device : devices) {
tex.setText(tex.getText()+"\nFound device:"+device);
}
}
Toast.makeText(v.getContext(), "Hello!! button Clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
//System.exit(0);break;
write(sendByte);break;
case R.id.button3: Conect(mDevice); break;
}
}
}
private BluetoothSocket mSocket;
public void Conect(BluetoothDevice mDevice) {
BluetoothSocket tmp=null;
mAdapter.cancelDiscovery();
try {
Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mDevice, 1);
} catch(Exception e) {
Toast.makeText(getApplicationContext(), "FAIL", Toast.LENGTH_LONG).show();
}
mSocket = tmp;
try{
mSocket.connect();
} catch (IOException connectException) {
try{mSocket.close();}
catch(IOException closeException) {}
return;
}
}
public void write(byte sendByte) {
try {
mOutputStream = mSocket.getOutputStream();
mOutputStream.write(sendByte);
Toast.makeText(getApplicationContext(),"Odoslane", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_arduino_btcontrol, menu);
return true;
}
}
there is the whole code, the think is that when I press button2 whitch call write(sendByte) the application crash althought eclipse show no error and compile the app. So I assume the problem is in the
public void write(byte sendByte){
try {mOutputStream = mSocket.getOutputStream();
mOutputStream.write(sendByte);
Toast.makeText(getApplicationContext(),"Odoslane", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {}
}