我一整天都在寻找答案,当我以为我有一个 - 它没有用。我在这里阅读了很多主题,但我仍然遇到问题。我不能通过蓝牙发送任何东西。
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;
}
}
有整个代码,想的是当我按下button2 whitch调用write(sendByte)应用程序崩溃时,eclipse显示没有错误并编译应用程序。所以我认为问题出在
中 public void write(byte sendByte){
try {mOutputStream = mSocket.getOutputStream();
mOutputStream.write(sendByte);
Toast.makeText(getApplicationContext(),"Odoslane", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {}
}
答案 0 :(得分:0)
也许你的mSocket.connect()
失败了,但是因为你不会在异常中打印任何东西,你永远不会知道!
try{
mSocket.connect();
} catch (IOException connectException) {
connectException.printStachTrace(); // SHOW THE ERROR
try{mSocket.close();}
catch(IOException closeException) {}
return;
}
另外,为什么要使用反射来创建createRfcommSocket
?
请参阅以下代码段作为指南:
static final UUID SERIAL_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
String BTAddress = "00:12:12:04:41:66";
BluetoothDevice btDevice = btAdapter.getRemoteDevice(BTAddress);
BluetoothSocket btSocket = btDevice.createRfcommSocketToServiceRecord(SERIAL_UUID);
btSocket.connect();
BluetoothSocket iStream = btSocket.getInputStream();
OutputStream oStream = btSocket.getOutputStream();
另外,为了测试和调试目的,您可以编写一些硬编码值,例如: write(5);