繁体   English   中英

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

[英]How connect to a device using the name device?

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

我在按钮上创建了一个点击监听器,首先检查蓝牙是否被激活,然后尝试使用 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){

        }
    }
}

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

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

一个简单的例子可以在这里找到: https : //developer.android.com/guide/topics/connectivity/bluetooth#ConnectAsAClient

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM