如何让蓝牙RFCOMM始终如一地工作?

时间:2011-04-04 19:58:07

标签: android sockets bluetooth rfcomm

我正在尝试构建一个Android应用程序,它将通过蓝牙串行端口配置文件(SPP)与外部GPS接收器连接。我正在使用运行2.3.3的Nexus One。我设法让我的应用程序从GPS接收数据,但我有两个问题:1)当我连接到设备时,它只在某些时候工作。有时连接只是超时,有时则表示设备正忙或正在使用中。 2)我无法弄清楚如何将数据发送回设备,这可能是我如何使用流的一个问题,因为传入流是阻塞呼叫。

我将相关代码移动到新的Android应用程序进行测试,具体如下:

/res/layout/main.xml(两个按钮和一个textview)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Connect"></Button>
<Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Message"></Button>
<TextView android:id="@+id/textStatus" android:textSize="24sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Status Goes Here" />
</LinearLayout>

/src/com.example.bluetoothspp/MainActivity.java

package com.example.bluetoothspp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.SocketTimeoutException;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private static final String BTAG = "BTThread";
    static final int MSG_BT_GOT_DATA = 1;
    static final int MSG_BT_STATUS_MSG = 2;
    static final int MSG_BT_FINISHED = 99;

    Button btnStart, btnSend;
    TextView textStatus;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothDevice btdevice = null;
    Thread bThread;
    BluetoothSocket bsocket;
    InputStream bis = null; //Bluetooth input stream
    OutputStream bos = null; //Bluetooth output stream
    private String MACAddress = "00:01:95:06:1F:32";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnStart = (Button)findViewById(R.id.btnStart);
        btnSend = (Button)findViewById(R.id.btnSend);
        textStatus = (TextView)findViewById(R.id.textStatus);
        btnStart.setOnClickListener(btnStartListener);
        btnSend.setOnClickListener(btnSendListener);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    private OnClickListener btnStartListener = new OnClickListener() {
        public void onClick(View v){
            if(btnStart.getText().equals("Connect")){
                Log.i(TAG, "Connect button pressed");
                if (mBluetoothAdapter == null) { //No adapter. Fail
                    Log.e(TAG, "getDefaultAdapter returned null");
                    textStatus.setText("getDefaultAdapter returned null");

                } else {
                    if (!mBluetoothAdapter.isEnabled()) { //Bluetooth disabled
                        Log.e(TAG, "Bluetooth is Disabled");
                        textStatus.setText("Bluetooth is Disabled");

                    } else {
                        Log.i(TAG, "Connecting to Device: " + MACAddress);
                        btdevice = mBluetoothAdapter.getRemoteDevice(MACAddress);
                        Log.i(TAG, "Device: " + btdevice.getName());
                        Log.i(TAG, "Trying to Connect...");
                        textStatus.setText("Trying to Connect...");
                        Log.i(TAG, "Starting Thread");
                        try {
                            bThread = new Thread(new BluetoothClient(btdevice, true));
                            bThread.start();
                        } catch (IOException e) {
                            Log.e(TAG, "Could not create thread for bluetooth: " + e);
                            textStatus.setText("Could not create thread for bluetooth...");
                        }
                        btnStart.setText("Disconnect");
                    }
                }

            } else {
                Log.i(TAG, "Disconnect button pressed");

                btnStart.setText("Connect");
            }
        }
    };
    private OnClickListener btnSendListener = new OnClickListener() {
        public void onClick(View v){
            textStatus.setText("Sending Message to Thread.");
            SendDataToBluetooth("something\r\n");
        }
    };


    public class BluetoothClient implements Runnable {
        public BluetoothClient(BluetoothDevice device, boolean IsAnHTCDevice) throws IOException {
            if (IsAnHTCDevice) {
                //This is a workaround for HTC devices, but it likes to throw an IOException "Connection timed out"
                try {
                    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                    bsocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
                } catch (Exception e) {
                    Log.e(BTAG, "Error at HTC/createRfcommSocket: " + e);
                    e.printStackTrace();
                    handler.sendMessage(handler.obtainMessage(MSG_BT_STATUS_MSG, "MethodException: " + e));
                }
            } else {
                //This is the normal method, but on a Nexus One it almost always throws an IOException "Service discovery failed" message
                try {
                    UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
                    bsocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                } catch (Exception e) {
                    Log.e(BTAG, "Error at createRfcommSocketToServiceRecord: " + e);
                    e.printStackTrace();
                    handler.sendMessage(handler.obtainMessage(MSG_BT_STATUS_MSG, "MethodException: " + e));
                }
            }
        }

        public void run() {
            try {
                Log.i(BTAG, "Cancelling Discovery");
                mBluetoothAdapter.cancelDiscovery();
                Log.i(BTAG, "Connecting to Socket");
                bsocket.connect();
                bis = bsocket.getInputStream();
                bos = bsocket.getOutputStream();
                Log.i(BTAG, "Socket created, streams assigned");
                handler.sendMessage(handler.obtainMessage(MSG_BT_STATUS_MSG, "Device Connected"));
                Log.i(BTAG, "Waiting for data...");
                byte[] buffer = new byte[4096];
                int read = bis.read(buffer, 0, 4096); // This is blocking
                Log.i(BTAG, "Getting data...");
                while (read != -1) {
                    byte[] tempdata = new byte[read];
                    System.arraycopy(buffer, 0, tempdata, 0, read);
                    handler.sendMessage(handler.obtainMessage(MSG_BT_GOT_DATA, tempdata));
                    read = bis.read(buffer, 0, 4096); // This is blocking
                }
            } catch (SocketTimeoutException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Log.i(BTAG, "Finished");
                handler.sendMessage(handler.obtainMessage(MSG_BT_FINISHED));
            }
        }
    }
    public void SendDataToBluetooth(String cmd) { // You run this from the main thread.
        try {
            if (bsocket != null) {
                bos.write(cmd.getBytes());
            }
        } catch (Exception e) {
            Log.e("SendDataToBluetooth", "Message send failed. Caught an exception: " + e);
        }
    }

    public Handler handler = new Handler() { // Handler for data coming from the network and bluetooth sockets
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_BT_GOT_DATA:
                Log.i("handleMessage", "MSG_BT_GOT_DATA: " + (String) msg.obj);
                textStatus.setText((String) msg.obj);
                break;
            case MSG_BT_STATUS_MSG:
                Log.i("handleMessage", "MSG_BT_STATUS_MSG: " + (String) msg.obj);
                textStatus.setText((String) msg.obj);
                break;
            case MSG_BT_FINISHED:
                Log.i("handleMessage", "MSG_BT_FINISHED");
                btnStart.setText("Connect");
                break;
            default:
                super.handleMessage(msg);
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bThread != null) { // If the thread is currently running, close the socket and interrupt it.
            Log.i(BTAG, "Killing BT Thread");
            try {
                bis.close();
                bos.close();
                bsocket.close();
                bsocket = null;
            } catch (IOException e) {
                Log.e(BTAG, "IOException");
                e.printStackTrace();
            } catch (Exception e) {
                Log.e(BTAG, "Exception");
                e.printStackTrace();
            }
            try {
                Thread moribund = bThread;
                bThread = null;
                moribund.interrupt();
            } catch (Exception e) {}
            Log.i(BTAG, "BT Thread Killed");
        }
    }
}

我发现使用普通的“bsocket = device.createRfcommSocketToServiceRecord(MY_UUID);”方法通常会导致我的“服务发现失败”消息,所以我也尝试了“bsocket =(BluetoothSocket)m.invoke(device,Integer.valueOf(1));”方法。这种情况更常见,但在我尝试连接时喜欢超时。

我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

尝试侦听传入的数据并在单独的线程中写入设备。通过这种方式,您可以分离阻止呼叫  你看过蓝牙聊天样本了吗?该示例使用类似的线程技术。

答案 1 :(得分:0)

如果你的目标是2.3及以上(当前安装了on over 50%的Android设备)你可以使用createInsecureRfcommSocketToServiceRecord方法与设备进行通信,这肯定会让它更好,更可连接