蓝牙连接所需的变量

时间:2014-08-25 03:41:25

标签: android bluetooth

目前,我正试图弄清楚如何在整个活动期间通过蓝牙与设备保持联系。我有一些变量,我初始化以获得连接。

我之前的活动流程是主页>用户输入文本页面>蓝牙连接(发送信息)。

因此,通过这种方式,每次我返回用户输入文本页面时,蓝牙连接都将被重置,因为当我进入下一页时,它将重新运行所有接收器和内容。

现在我正在向前移动蓝牙连接。现在意思是主页>蓝牙连接>用户输入文本页面(发送)。

但在我连接蓝牙连接页面后,我不确定我应该在SharedPreferences内带来/保存哪些变量,以便蓝牙连接保持不变,我可以立即发送。

//This method runs when I click a device on my ListView.
    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // Cancel discovery because it's costly and we're about to connect

                bluetoothAdapter.cancelDiscovery();
                System.out.println("Bluetooth Adapter2 = "
                        + bluetoothAdapter.cancelDiscovery());
                SiriListItem item = delist.get(arg2);
                mAdapter.notifyDataSetChanged();
                // When device being clicked
                count++;
                click = 1;
                // Get the device MAC address, which is the last 17 chars in the
                // View
                String info = item.message;
                String address = info.substring(info.length() - 17);
                BlueToothAddress = address;
                if (click == 1) {
                    clientThread ct = new clientThread();
                    ct.start();

                }
            };
        };

//This is the clientThread if click == 1, it'll start this.
private class clientThread extends Thread {
        public void run() {

            try {
                //
                bdDevice = bluetoothAdapter.getRemoteDevice(BlueToothAddress);
                socket = bdDevice.createRfcommSocketToServiceRecord(UUID
                        .fromString("00001101-0000-1000-8000-00805F9B34FB"));
                Message msg2 = new Message();
                msg2.obj = "Please wait, connecting to server: "
                        + BlueToothAddress;
                msg2.what = 0;
                LinkDetectedHandler.sendMessage(msg2);
                socket.connect();
                Log.i("tag", "This is the pairing section");

                Message msg = new Message();
                msg.obj = "Device connected. Sending message is allowed.";
                msg.what = 0;
                LinkDetectedHandler.sendMessage(msg);

                readThread = new readThread();
                readThread.start();
                click++;

            } catch (IOException e) {
                Message msg = new Message();
                msg.obj = "Error! Can't connect to device. Please try again.";
                msg.what = 0;
                LinkDetectedHandler.sendMessage(msg);
                click--;

            }
        }
    };

public class readThread extends Thread {
        public void run() {

            byte[] buffer = new byte[1024];
            int bytes;
            InputStream mmInStream = null;
            String tmp = null;
            try {
                mmInStream = socket.getInputStream();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            while (true) {
                try {
                    // read the data from the inputStream
                    if ((bytes = mmInStream.read(buffer)) > 0) {
                        for (int i = 0; i < bytes; i++) {
                            tmp = "" + buffer[i];
                            String st = new String(tmp);
                            tmp = null;
                            Message msg = new Message();
                            msg.obj = st;
                            msg.what = 1;

                        }
                    }
                } catch (IOException e) {
                    try {
                        mmInStream.close();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

//On Click it'll send the message stored in the editText.
buttonConnect.setOnClickListener(new View.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(View arg0) {
                if (count == 0) {
                    Toast.makeText(bluetoothtest.this,
                            "Please connect to a device first.",
                            Toast.LENGTH_LONG).show();
                }
                // Need API=14
                else if (!socket.isConnected()) {
                    Toast.makeText(bluetoothtest.this,
                            "Connecting! Please wait.", Toast.LENGTH_LONG)
                            .show();
                } else {
                    try {
                        sendMessageHandle(contentRow1.getText().toString(),
                                contentRow2.getText().toString(), contentRow3
                                        .getText().toString(), contentRow4
                                        .getText().toString());
                        // sendMessageHandle(contentRow2.getText().toString());

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        });

所以最重要的是。我的用户输入文本页面应该有什么方法?我必须在用户输入文本页面中使用所有这些方法,还是只能通过SharedPreferences引入变量?

感谢。

1 个答案:

答案 0 :(得分:0)

在主线程上处理蓝牙连接可能是不好的做法。您应该通过服务/后台线程真正处理蓝牙连接/维护。然后,您的活动可以通过BroadcastReceiver和句柄与服务进行通信。