Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用设备名称连接到设备?_Java_Android_Connection_Android Bluetooth - Fatal编程技术网

Java 如何使用设备名称连接到设备?

Java 如何使用设备名称连接到设备?,java,android,connection,android-bluetooth,Java,Android,Connection,Android Bluetooth,我必须在安卓系统中创建一个通过蓝牙连接到设备的应用程序。 这个设备已经设置了名称“1111”,在我的android应用程序中,我有四个editText来输入数字。 通过点击“collega”按钮,我希望应用程序直接连接到设备 我在按钮上创建了一个click侦听器,首先检查bluettoth是否被激活,然后尝试使用load()函数连接它。 load函数查找附近名为“1111”的设备,但之后我不知道如何连接它 final Button collega = (Button) findViewById(

我必须在安卓系统中创建一个通过蓝牙连接到设备的应用程序。 这个设备已经设置了名称“1111”,在我的android应用程序中,我有四个editText来输入数字。 通过点击“collega”按钮,我希望应用程序直接连接到设备

我在按钮上创建了一个click侦听器,首先检查bluettoth是否被激活,然后尝试使用load()函数连接它。 load函数查找附近名为“1111”的设备,但之后我不知道如何连接它

final Button collega = (Button) findViewById(R.id.button2);
    collega.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View arg0) {
            if (!btAdapter.isEnabled())
            { Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(turnOn, BLUETOOTH_ON); }
            else
                load(); }
    });
    }

private void load() {
    final EditText uno=(EditText)findViewById(R.id.numero1);
    final EditText due=(EditText)findViewById(R.id.numero2);
    final EditText tre=(EditText)findViewById(R.id.numero3);
    final EditText quattro=(EditText)findViewById(R.id.numero4);
    String deviceNumber=uno.getText().toString()+due.getText().toString()+tre.getText().toString()+quattro.getText().toString();
    TextView textView = (TextView) findViewById(R.id.simpleTextView);
    textView.setText(deviceNumber);

    devices = btAdapter.getBondedDevices();
    adapter.clear();
    for(BluetoothDevice bt : devices)
    {
        if(bt.getName()==deviceNumber){

        }
    }
}

我在网上搜索,但只找到了如何创建要连接的设备列表。是否有人可以创建连接以及如何传输数据?

您可以创建一个线程来连接蓝牙套接字

以下是一个简单的例子:

您可以创建一个线程来连接蓝牙插座

以下是一个简单的例子: