Android蓝牙通讯错误

时间:2013-06-23 15:51:25

标签: android bluetooth serial-port

我正在尝试通过蓝牙与Android手机通信惯性测量单元(加速度计,陀螺仪和磁力计)。具体来说,我使用的是MPU9150作为我的IMU。我有一个Python的工作代码,它与IMU完美地沟通。我继续将此代码移植到Java的Android部分。我拿了蓝牙聊天示例并根据我的需要进行了修改。现在它“有效”。

我可以毫无问题地将数据发送到IMU,我可以以某种方式正确地接收数据。我得到了正确的数据包,但每个人说,第4或第5个数据包,可能更频繁,也许更少,我得到一个完全无理的数据包。例如,正确的加速包看起来像     0.002 0.001 0.003 虽然我得到的不稳定的价值看起来像     0.002 0.001 -16,371.002

我知道我的移植工作正常,因为我已经测试并调试了处理传入数据的函数。在我看来,问题在于连接部分。当我检查Python和Java中的数据时,Java数据包从它们到达的那一刻起就显示出这种完全不合理的数据(同样,并非所有数据)。但是Python数据包没有,它们都是正确的!想知道可能会发生什么?

我可以正确发送数据并且可以正确接收一些数据包,这真的很奇怪。非常感谢帮助。感谢。

代码添加: Python串行初始化

def __init__(self, port, quat_delegate=None, debug_delegate=None, data_delegate=None):

    self.s = serial.Serial(port, 115200)

    self.s.setTimeout(0.1)

    self.s.setWriteTimeout(0.2)

Android部分有点长...对不起。

//constructor
public BluetoothChatService(Context context, Handler handler){
    mAdapter = BluetoothAdapter.getDefaultAdapter();
    mState = STATE_NONE;
    mHandler = handler;
}

/**
 * Start the ConnectThread to initiate a connection to a remote device.
 * @param device  The BluetoothDevice to connect
 * @param secure Socket Security type - Secure (true) , Insecure (false)
 */
public synchronized void connect(BluetoothDevice device, boolean secure){
    if (D) Log.d(TAG, "connect to: " + device);

    // Cancel any thread attempting to make a connection
    if (mState == STATE_CONNECTING){
        if (mConnectThread != null){
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null){
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Start the thread to connect with the given device
    mConnectThread = new ConnectThread(device, secure);
    mConnectThread.start();
    setState(STATE_CONNECTING);
}

/**
 * Start the ConnectedThread to begin managing a Bluetooth connection
 * @param socket  The BluetoothSocket on which the connection was made
 * @param device  The BluetoothDevice that has been connected
 */
public synchronized void connected(BluetoothSocket socket, BluetoothDevice
        device, final String socketType){
    if (D) Log.d(TAG, "connected, Socket Type:" + socketType);

    // Cancel the thread that completed the connection
    if (mConnectThread != null){
        mConnectThread.cancel();
        mConnectThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null){
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    // Cancel the accept thread because we only want to connect to one device
    if (mSecureAcceptThread != null){
        mSecureAcceptThread.cancel();
        mSecureAcceptThread = null;
    }
    if (mInsecureAcceptThread != null){
        mInsecureAcceptThread.cancel();
        mInsecureAcceptThread = null;
    }

    // Start the thread to manage the connection and perform transmissions
    mConnectedThread = new ConnectedThread(socket, socketType);
    mConnectedThread.start();

    // Send the name of the connected device back to the UI Activity
    Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);
    Bundle bundle = new Bundle();
    bundle.putString(BluetoothChat.DEVICE_NAME, device.getName());
    msg.setData(bundle);
    mHandler.sendMessage(msg);

    setState(STATE_CONNECTED);
}

/**
 * This thread runs while attempting to make an outgoing connection
 * with a device. It runs straight through; the connection either
 * succeeds or fails.
 */
private class ConnectThread extends Thread{
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    private String mSocketType;

    public ConnectThread(BluetoothDevice device, boolean secure){
        mmDevice = device;
        BluetoothSocket tmp = null;
        mSocketType = secure ? "Secure" : "Insecure";

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try{
            if(secure){
                tmp = device.createRfcommSocketToServiceRecord(
                        MY_UUID_SECURE);
            }else{
                tmp = device.createInsecureRfcommSocketToServiceRecord(
                        MY_UUID_INSECURE);
            }
        }catch (IOException e){
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }
        mmSocket = tmp;
    }

    public void run(){
        Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
        setName("ConnectThread" + mSocketType);

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket
        try{
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();
        }catch (IOException e){
            // Close the socket
            try{
                mmSocket.close();
            }catch (IOException e2){
                Log.e(TAG, "unable to close() " + mSocketType +
                        " socket during connection failure", e2);
            }
            connectionFailed();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized(BluetoothChatService.this){
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice, mSocketType);
    }

    public void cancel(){
        try{
            mmSocket.close();
        }catch (IOException e){
            Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
        }
    }
}

/**
 * This thread runs during a connection with a remote device.
 * It handles all incoming and outgoing transmissions.
 */
private class ConnectedThread extends Thread{
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket, String socketType){
        Log.d(TAG, "create ConnectedThread: " + socketType);
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try{
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        }catch (IOException e) {
            Log.e(TAG, "temp sockets not created", e);
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[23];
        //byte[] buffer = new byte[1024]; //orig*
        int bytes;

        // Keep listening to the InputStream while connected
        while (true){
            try{
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
        //processing of the data goes here, i guess that's not necessary to include
     }

connect方法启动ConnectThread,该连接将成功运行,直到连接成功或失败。建立连接后,该线程将停止并且ConnectedThread将启动。这只是来自BluetoothChat Android示例的代码,稍作修改。

0 个答案:

没有答案