나는 하루 종일 내가 내가 가지고 있다고 생각했을 때 나는 대답을 찾고 있었다. 나는이 주제를 많이 읽었지만 여전히 문제가있다. 나는 아무것도 물마루 블루투스를 보낼 수 없습니다.블루투스 안드로이드는 아무 것도 보낼 수 없다.
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;
}
}
전체 코드는 생각이 I를 누르면 단추 2 때 호출 쓰기 (sendByte) 응용 프로그램 충돌 althought 일식은 오류를 표시하지 않고 whitch 응용 프로그램을 컴파일이다,있다. 그래서 문제가
public void write(byte sendByte){
try {mOutputStream = mSocket.getOutputStream();
mOutputStream.write(sendByte);
Toast.makeText(getApplicationContext(),"Odoslane", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {}
}
button3을 눌러 블루투스 장치에 연결할 수 있지만 연결되어 있거나 연결되어 있지 않은 상태입니다. button2를 누른 후 앱이 여전히 충돌합니다. – Lukas161
예외가 발생했습니다. – Ziem
https://docs.google.com/file/d/0B0_cwD39yvpuR3phZHp2dWk0MUk/edit?usp=sharing 다음은 기대했던 로그 파일입니다. – Lukas161