如何连接到用户指定的配对蓝牙设备?

时间:2019-04-03 02:15:06

标签: java android

我的对话框显示已配对的蓝牙设备,并允许用户选择其中一个要连接的蓝牙设备,这似乎阻止了连接方法的执行。

我有一个BTinit方法和一个BTconnect方法。 BTinit查询已配对的设备;如果BTinit成功,则会调用BTconnect来建立蓝牙连接。当BTinit找到很难找到的配对设备时,连接成功。

现在,我不想连接到特定的硬编码设备,而是想在对话框中列出配对的蓝牙设备,然后让用户选择要连接的设备。因此,我添加了一个新类'BTDeviceSelect'扩展了AlertDialog。 BTinit调用BTDeviceSelect,但是现在BTinit总是返回false,因此从不调用BTconnect。

public void onClickConnect(View view) {
    if(BTinit()) 
    {
        if(BTconnect())
        {
        } else {
            TString = textView.getText().toString();
            TString = TString + "Can not CONNECT to Arduino\n";
            textView.setText(TString);
        }
    } else {
        TString = textView.getText().toString();
        TString = TString + "Can not INIT to Arduino\n";
        textView.setText(TString);
    }

public boolean BTinit()
{
    found=false;


    else { //bluetoothAdapter isEnabled
        Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
        if (bondedDevices.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Please Pair the Device first", Toast.LENGTH_SHORT).show();
        } else {
            // open a dialog that displays the name and MAC of each bonded device,
            // require the user select a device or cancel
            // if a device is selected, set device to the selected device and set found = true
            // if cancel is selected, set found = false
            btSelectConnect = new BTDeviceSelect(this, bondedDevices, new BTDeviceSelect.DialogListener() {
                @Override
                public void ready(BluetoothDevice n) {
                    device = n;
                    found = true;
                    TString = textView.getText().toString();
                    TString = TString + String.format(Locale.US,"%s selected\n", device.getName());
                    textView.setText(TString);
                }

                @Override
                public void cancelled() {
                    found = false;
                }
            });
            btSelectConnect.show();
        }
    }
    return found;
}

以下是对话框类“ BTDeviceSelect”的一些代码:

public class BTDeviceSelect extends AlertDialog {
    private ArrayList<String> btList;
    private ArrayList<String> btAddressList;
    private Context btContext;
    private Spinner btSpinner;
    private Set<BluetoothDevice> btDevices;

public interface DialogListener {
    public void ready(BluetoothDevice n);
    public void cancelled();
}

    buttonOK.setOnClickListener(new android.view.View.OnClickListener(){
        public void onClick(View v) {
            int selected = btSpinner.getSelectedItemPosition();
            // get the address of the selected device based on the position of the selected item
            // then iterate through the set of Bluetooth Devices until the selected device is found
            // this device is returned
            for (BluetoothDevice itr : btDevices) {
                if (itr.getAddress().equals(btAddressList.get(selected))) {
                    btReadyListener.ready(itr);
                    break;
                }
            }
            BTDeviceSelect.this.dismiss();
        }
    });
}

我希望系统与用户选择的设备建立连接。 但是BTinit不会限制TRUE,因此永远不会执行BTconnect,因此永远不会建立连接。

在应用程序的屏幕截图中,请注意红色轮廓区域,其中显示了一些短信;如果BTinit返回false,则首先是“无法初始化到Arduino”,这会在onClickConnect中写入。然后,当用户选择设备时,BTinit会写出“ HC-05 selected”。

图片:

click for image

0 个答案:

没有答案