2013-02-17 2 views
0

나는 하루 종일 내가 내가 가지고 있다고 생각했을 때 나는 대답을 찾고 있었다. 나는이 주제를 많이 읽었지만 여전히 문제가있다. 나는 아무것도 물마루 블루투스를 보낼 수 없습니다.블루투스 안드로이드는 아무 것도 보낼 수 없다.

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) {} 
    } 
+0

button3을 눌러 블루투스 장치에 연결할 수 있지만 연결되어 있거나 연결되어 있지 않은 상태입니다. button2를 누른 후 앱이 여전히 충돌합니다. – Lukas161

+1

예외가 발생했습니다. – Ziem

+0

https://docs.google.com/file/d/0B0_cwD39yvpuR3phZHp2dWk0MUk/edit?usp=sharing 다음은 기대했던 로그 파일입니다. – Lukas161

답변

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);

+0

BluetoothSocket mSocket = mDevice.createInsecureRfcommSocketToServiceRecord (SERIAL_UUID) ; - 기본 생성자가 암시 적 슈퍼 생성자가 throw하는 예외 IOException을 처리 할 수 ​​없습니다. 명시 적 생성자 을 정의해야합니다. – Lukas161

+0

예외로 처리하려고 시도했지만 로그에 아무 것도 변경되지 않았습니다. 또한 위의 내용을 볼 때 오류가 발생하면 프로그램을 변경하려고 시도했습니다. 오류없이 연결되기 때문에 연결에 실수가 있었는지 모르지만 가능하면 도움을 받으십시오. – Lukas161

+0

https://docs.google.com/file/d/0B0_cwD39yvpuTHBSeHdhZmp4dnM/edit?usp=sharing 살펴볼 수 있습니까? ? – Lukas161

관련 문제