如何解决从Arduino获取数据时出现的“不是封闭类错误”。

huangapple 未分类评论44阅读模式
英文:

How to solve "Is not an enclosing class error"when get data from Arduino

问题

我正在尝试实现一个线程来从我的Arduino设备获取数据在另一个函数manageMyConnectedSocket(BluetoothSocket mmSocket)中创建这个线程以供使用会导致不是封闭类的错误当我将线程设置为static以解决此问题时我的Handler开始出现问题,“非静态字段'Handler'无法从静态上下文引用”。谢谢您的帮助

public static class MyBluetoothService {
    private static final String TAG = "MY_APP_DEBUG_TAG";
    private Handler handler; // 从蓝牙服务获取信息的处理程序

    // 在服务和UI之间传递消息时使用的常量
    private interface MessageConstants {
        public static final int MESSAGE_READ = 0;
        public static final int MESSAGE_WRITE = 1;
        public static final int MESSAGE_TOAST = 2;

        // ...(根据需要添加其他消息类型。)
    }

    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        private byte[] mmBuffer; // 用于存储流的mmBuffer

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

            // 获取输入和输出流;使用临时对象,因为成员流是final的。
            try {
                tmpIn = socket.getInputStream();
            } catch (IOException e) {
                Log.e(TAG, "创建输入流时出错", e);
            }
            try {
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "创建输出流时出错", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            mmBuffer = new byte[1024];
            int numBytes; // 从read()返回的字节

            // 保持监听InputStream,直到发生异常。
            while (true) {
                try {
                    // 从InputStream读取。
                    numBytes = mmInStream.read(mmBuffer);
                    // 将获取到的字节发送到UI活动。
                    Message readMsg = handler.obtainMessage(
                            MessageConstants.MESSAGE_READ, numBytes, -1,
                            mmBuffer);
                    readMsg.sendToTarget();
                } catch (IOException e) {
                    Log.d(TAG, "InputStream已断开连接", e);
                    break;
                }
            }
        }

        // 从主活动调用此方法将数据发送到远程设备。
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);

                // 将已发送的消息与UI活动共享。
                Message writtenMsg = handler.obtainMessage(
                        MessageConstants.MESSAGE_WRITE, -1, -1, mmBuffer);
                writtenMsg.sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "发送数据时出错", e);

                // 向活动发送发送失败消息。
                Message writeErrorMsg =
                        handler.obtainMessage(MessageConstants.MESSAGE_TOAST);
                Bundle bundle = new Bundle();
                bundle.putString("toast",
                        "无法将数据发送到其他设备");
                writeErrorMsg.setData(bundle);
                handler.sendMessage(writeErrorMsg);
            }
        }

        // 从主活动调用此方法关闭连接。
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "无法关闭连接套接字", e);
            }
        }
    }
}

//***管理已连接套接字******管理已连接套接字******管理已连接套接字******管理已连接套接字******管理已连接套接字***
private void manageMyConnectedSocket(BluetoothSocket mmSocket) {
    Log.println(Log.ERROR,"DEV","我在manageMyConnectedSocket方法中,似乎很有趣");
    MyBluetoothService.ConnectedThread dataThread = new MyBluetoothService.ConnectedThread(mmSocket);
    dataThread.start();
}
英文:

I am trying to implement a thread to get data from my arduino device. Creating this thread in another function(manageMyConnectedSocket(BluetoothSocket mmSocket)) to use causes "Is not an enclosing class" error. When i make the thread "static" to solve this problem my Handler starts causing problem "non-static field 'Handler' cannot be referenced from a static context" Thank you for your help!

public static class MyBluetoothService {
        private static final String TAG = "MY_APP_DEBUG_TAG";
        private Handler handler; // handler that gets info from Bluetooth service

        // Defines several constants used when transmitting messages between the
        // service and the UI.
        private interface MessageConstants {
            public static final int MESSAGE_READ = 0;
            public static final int MESSAGE_WRITE = 1;
            public static final int MESSAGE_TOAST = 2;

            // ... (Add other message types here as needed.)
        }

        private class ConnectedThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final InputStream mmInStream;
            private final OutputStream mmOutStream;
            private byte[] mmBuffer; // mmBuffer store for the stream

            public ConnectedThread(BluetoothSocket socket) {
                mmSocket = 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();
                } catch (IOException e) {
                    Log.e(TAG, "Error occurred when creating input stream", e);
                }
                try {
                    tmpOut = socket.getOutputStream();
                } catch (IOException e) {
                    Log.e(TAG, "Error occurred when creating output stream", e);
                }

                mmInStream = tmpIn;
                mmOutStream = tmpOut;
            }

            public void run() {
                mmBuffer = new byte[1024];
                int numBytes; // bytes returned from read()

                // Keep listening to the InputStream until an exception occurs.
                while (true) {
                    try {
                        // Read from the InputStream.
                        numBytes = mmInStream.read(mmBuffer);
                        // Send the obtained bytes to the UI activity.
                        Message readMsg = handler.obtainMessage(
                                MessageConstants.MESSAGE_READ, numBytes, -1,
                                mmBuffer);
                        readMsg.sendToTarget();
                    } catch (IOException e) {
                        Log.d(TAG, "Input stream was disconnected", e);
                        break;
                    }
                }
            }

            // Call this from the main activity to send data to the remote device.
            public void write(byte[] bytes) {
                try {
                    mmOutStream.write(bytes);

                    // Share the sent message with the UI activity.
                    Message writtenMsg = handler.obtainMessage(
                            MessageConstants.MESSAGE_WRITE, -1, -1, mmBuffer);
                    writtenMsg.sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "Error occurred when sending data", e);

                    // Send a failure message back to the activity.
                    Message writeErrorMsg =
                            handler.obtainMessage(MessageConstants.MESSAGE_TOAST);
                    Bundle bundle = new Bundle();
                    bundle.putString("toast",
                            "Couldn't send data to the other device");
                    writeErrorMsg.setData(bundle);
                    handler.sendMessage(writeErrorMsg);
                }
            }

            // Call this method from the main activity to shut down the connection.
            public void cancel() {
                try {
                    mmSocket.close();
                } catch (IOException e) {
                    Log.e(TAG, "Could not close the connect socket", e);
                }
            }
        }
    }

    //***MANAGE CONNECTED SOCKET ******MANAGE CONNECTED SOCKET ******MANAGE CONNECTED SOCKET ******MANAGE CONNECTED SOCKET ******MANAGE CONNECTED SOCKET ***
    private void manageMyConnectedSocket(BluetoothSocket mmSocket) {
        Log.println(Log.ERROR,"DEV","I AM IN THE MANAGEMYCONNECTEDSOCKET METHOD, SEEMS INTERESTING");
        MyBluetoothService.ConnectedThread dataThread= new MyBluetoothService.ConnectedThread(mmSocket);
        dataThread.start();
    }

huangapple
  • 本文由 发表于 2020年7月26日 00:54:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63091010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定