0

我正在尝试通过蓝牙与 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 示例的代码,稍作修改。

4

0 回答 0