简体   繁体   中英

How connect to a device using the name device?

I have to create an app in android that connects via bluetooth to a device. This device has set the name "1111" and in my android application I have four editText to enter the numbers. By clicking on the "collega" button I would like the app to connect directly to the devices.

I created a click listener on the button, first check that the bluettoth is activated and then try connecting it using the load () function. The load function looks for nearby devices with the name "1111" but afterwards I can't figure out how to connect it.

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){

        }
    }
}

I searched online but found only how to create a list of devices to connect to. Can someone how create the connection and how stream the data?

You can create a thread to connect the bluetooth socket.

A simple example can be found here: https://developer.android.com/guide/topics/connectivity/bluetooth#ConnectAsAClient

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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