使用蓝牙套接字连接接收命令

时间:2014-01-21 05:17:12

标签: java android android-bluetooth

我正在尝试打开与笔记本电脑的连接,该笔记本电脑上有蓝牙。服务器程序在我的笔记本电脑中运行我的目的是与我的笔记本电脑建立连接,在同一个创建的套接字上连续发送一些输入。连接已成功建立,并且已将一个字节发送到我的笔记本电脑中运行的服务器,但在按下发送按钮后没有发送任

以下代码适用于第一次尝试。当我尝试使用相同的连接重新发送时,未发送字节值。当我第二次启动我的应用程序时,我的手机被绞死了。我正在寻找内存问题,但我找不到任何内存问题。我错过了什么 ?我需要做点什么吗?

public class BluetoothActivity extends Activity {
        private TextView myLabel;
        private EditText myTextbox;
        private BluetoothAdapter mBluetoothAdapter;
        private BluetoothSocket mmSocket;
        private BluetoothDevice mmDevice;
        private OutputStream mmOutputStream;
        private BufferedOutputStream mbuffStream;
        private ProgressDialog progressBar;
        private int progressBarStatus;
        private Context mContext;
        private TextView devicesLabel;
        private TextView devices;

        private final static int MAX_VALUE = 100;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_bluetooth);
            mContext = this;
            Button openButton = (Button) findViewById(R.id.open);
            Button sendButton = (Button) findViewById(R.id.send);
            Button closeButton = (Button) findViewById(R.id.close);
            myLabel = (TextView) findViewById(R.id.label);
            devicesLabel = (TextView) findViewById(R.id.devicelabel);
            devices = (TextView) findViewById(R.id.devices);
            myTextbox = (EditText) findViewById(R.id.entry);
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

            // Open Button


            openButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        openBTConnection();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            // Send Button
            sendButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    try {
                        sendData();
                    } catch (IOException ex) {
                    }
                }
            });

            // Close button
            closeButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    closeBT();
                }
            });
        }

        private void openBTConnection() throws IOException {
            UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); // Standard
                                                                                    // SerialPortService
                                                                                    // ID
            mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
            System.out.println("socket " + mmSocket.toString());
            if (mmSocket != null) {
                mmSocket.connect();
                mmOutputStream = mmSocket.getOutputStream();
                mbuffStream = new BufferedOutputStream(mmOutputStream, 1);
                myLabel.setText("Bluetooth Opened");
            } else {
                myLabel.setText("No bluetooth device found");
            }
        }

        void sendData() throws IOException {
            String msg = myTextbox.getText().toString();
            mbuffStream.write(msg.getBytes(), 0, msg.getBytes().length);
            mbuffStream.flush();
            myLabel.setText("Data Sent");
        }

        void closeBT() {
            try {
                if (mmOutputStream != null) {
                    mmOutputStream.close();
                }
                if (mbuffStream != null) {
                    mbuffStream.close();
                }
                if (mmSocket != null) {
                    mmSocket.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            myLabel.setText("Bluetooth Closed");
        }

        @Override
        public void onBackPressed() {
            closeBT();
            finish();
        }
    }

感谢 Shriram M

1 个答案:

答案 0 :(得分:0)

我使用非缓冲流也在服务器端代码中改变了相同。它奏效了。 谢谢史密斯..