Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OutputStream到DataOutputStream,用于蓝牙通信_Android_Sockets_Bluetooth_Datainputstream_Dataoutputstream - Fatal编程技术网

Android OutputStream到DataOutputStream,用于蓝牙通信

Android OutputStream到DataOutputStream,用于蓝牙通信,android,sockets,bluetooth,datainputstream,dataoutputstream,Android,Sockets,Bluetooth,Datainputstream,Dataoutputstream,我已经在互联网上找到的一个Android应用程序的帮助下编写了一个连接蓝牙设备的应用程序。此应用程序使用InputStream和OutputStream对象连接并建立连接。因为我必须传输少量数据,所以这个解决方案还可以,因为我只能发送字节 现在我想更改我的旧代码,使用DataInputStream和DataOutputStream轻松发送复杂数据。我试图通过简单地在InputStream和OutputStream之前添加数据标识符来修改我的原始代码,但这在我的代码中产生了错误。有人能告诉我如何正

我已经在互联网上找到的一个Android应用程序的帮助下编写了一个连接蓝牙设备的应用程序。此应用程序使用InputStream和OutputStream对象连接并建立连接。因为我必须传输少量数据,所以这个解决方案还可以,因为我只能发送字节

现在我想更改我的旧代码,使用DataInputStream和DataOutputStream轻松发送复杂数据。我试图通过简单地在InputStream和OutputStream之前添加数据标识符来修改我的原始代码,但这在我的代码中产生了错误。有人能告诉我如何正确使用DataInputStream和DataOutputStream,这样我就不会出错。这是我的旧代码:

 private class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                hBluetooth.obtainMessage(RECEIVE_MESSAGE, bytes, -1, buffer).sendToTarget();        // Send to message queue Handler
            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void send(String message) {
        if(D)   Log.d(TAG, "...Data to send: " + message + "...");
        if (btSocket != null && btSocket.isConnected()) {

        byte[] msgBuffer = message.getBytes();
        try {
            mmOutStream.write(msgBuffer);
            Log.e(TAG, "Int" +msgBuffer);
        } catch (IOException e) {
            Log.d(TAG, "...Error data send: " + e.getMessage() + "...");     
          }
        }

    }
这里是修改后的版本:

 private class ConnectedThread extends Thread {
     DataInputStream mmInStream;
     DataOutputStream mmOutStream;


    public ConnectedThread(BluetoothSocket socket) {
       InputStream tmpIn = null;
       OutputStream tmpOut = null;

        mmInStream = new DataInputStream(tmpIn);
        mmOutStream = new DataOutputStream(tmpOut);

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

谢谢你的意见

如果我正确理解了您的问题,请执行以下操作以获取DataInputStream和DataOutputStream对象:

  mmInStream = new DataInputStream(tmpIn);
  mmOutStream = new DataInputStream(tmpOut);
如果您需要将变量声明为全局变量(正如我从您所做的编辑中看到的),则变量将在类外声明如下:

   DataInputStream mmInStream;
   DataOutputStream mmOutStream;
编辑更新后的问题:

public ConnectedThread(BluetoothSocket socket) {
   InputStream tmpIn = null;
   OutputStream tmpOut = null;


    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = new DataInputStream(tmpIn);
    mmOutStream = new DataOutputStream(tmpOut);


}

我认为这个解决方案和我修改过的版本完全一样,但效果不好?不,它不在你的版本中,你正在铸造对象。需要使用正确的构造函数创建DataInputStream和DataoutputStream的对象;尝试一下,让我知道许多感谢,我已经做了一些修改,你可以看到在原来的帖子,但现在我有一个类型不匹配在mmInStream=tmpIn;和mmOutStream=tmpOut;也许我做错了什么……看看我的答案,像这样做:mmInStream=newdatainputstream(tmpIn);mmOutStream=新数据输入流(tmpOut);我在我的帖子“这在我的代码中造成了什么错误”中看到了修改后的版本?