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

有整个代码,认为当我按下 button2 调用 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) {}
    }
4

1 回答 1

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

于 2013-02-17T21:27:38.507 回答