Java 通过蓝牙配对设备

Java 通过蓝牙配对设备,java,android,android-bluetooth,Java,Android,Android Bluetooth,在我的应用程序中,我需要连接一些蓝牙设备并向其发送一些字符串数据。因此,我生成了一个actionbar项目,通过单击它,可以执行以下步骤: 1.蓝牙开启(如果持续关闭) 2.设备设置为可发现 3.经过一定的延迟,配对设备的列表显示在屏幕上 现在我的问题是:“如何将我的手机连接到配对列表中的一个?!” 我知道我应该用SetonicClickListener,但是怎么用呢?我是否应该为“连接”生成一些函数?这是我的密码: 在主要活动中: private BluetoothAdapter BA

在我的应用程序中,我需要连接一些蓝牙设备并向其发送一些字符串数据。因此,我生成了一个actionbar项目,通过单击它,可以执行以下步骤: 1.蓝牙开启(如果持续关闭) 2.设备设置为可发现 3.经过一定的延迟,配对设备的列表显示在屏幕上

现在我的问题是:“如何将我的手机连接到配对列表中的一个?!” 我知道我应该用SetonicClickListener,但是怎么用呢?我是否应该为“连接”生成一些函数?这是我的密码:

在主要活动中:

    private BluetoothAdapter BA;
    private Set<BluetoothDevice>pairedDevices;
    ListView lv;
在my OnOptions ItemSelected中:

case R.id.action_bluetooth:


                if (!BA.isEnabled()) {
                    Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(turnOn, 0);
                    Toast.makeText(getApplicationContext(),"Bluetooth Turned on",Toast.LENGTH_LONG).show();
                }

                new CountDownTimer(2000, 1000) {
                    public void onFinish() {
                        Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                        startActivityForResult(getVisible, 0);
                    }

                    public void onTick(long millisUntilFinished) {
                    }
                }.start();


                new CountDownTimer(10000, 1000) {
                    public void onFinish() {
                        // When timer is finished
                        // Execute your code here
                        showBluetoothListView();
                    }

                    public void onTick(long millisUntilFinished) {
                    }
                }.start();

                return true;
在函数showBluetoothListView()中:

case R.id.action_bluetooth:


                if (!BA.isEnabled()) {
                    Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(turnOn, 0);
                    Toast.makeText(getApplicationContext(),"Bluetooth Turned on",Toast.LENGTH_LONG).show();
                }

                new CountDownTimer(2000, 1000) {
                    public void onFinish() {
                        Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                        startActivityForResult(getVisible, 0);
                    }

                    public void onTick(long millisUntilFinished) {
                    }
                }.start();


                new CountDownTimer(10000, 1000) {
                    public void onFinish() {
                        // When timer is finished
                        // Execute your code here
                        showBluetoothListView();
                    }

                    public void onTick(long millisUntilFinished) {
                    }
                }.start();

                return true;
public void showBluetoothListView (){
        final Dialog d = new Dialog(MainActivity.this);
        d.setTitle("Bluetooth Paired Devices");
        d.setContentView(R.layout.bluetooth_list);
        final ListView lv = (ListView) d.findViewById(R.id.bluetoothlistView);
        pairedDevices = BA.getBondedDevices();
        ArrayList list = new ArrayList();

        for(BluetoothDevice bt : pairedDevices)
            list.add(bt.getName());

        final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);

        d.show();
    }