0

I am working on an Android app that uses a bluetooth connection, and I want to read data from the device into the app, but need to know if I am connected to the device before reading data.

Below is the code I have that launches bluetooth settings where I can pair the device. How do I verify that the device that I have paired has an active connection to the app and is ready to send data?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connect);

    Button btn = (Button) findViewById(R.id.connect_button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentOpenBluetoothSettings = new Intent();
            intentOpenBluetoothSettings.setAction(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
            startActivity(intentOpenBluetoothSettings);
        }
    });

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    pairedDevices = mBluetoothAdapter.getBondedDevices();
user3316012
  • 97
  • 1
  • 6

1 Answers1

0

Pair and Connect are two different concepts. Your code only pairs a target Bluetooth device and remember it, whose information can be retrieved by calling:

BluetoothAdapter.getDefaultAdapter().getBondedDevices();

This function actually will return all the information of paired Bluetooth devices even though these devices are not near (connect to) you phone.

To connect a Bluetooth device, you need to write your own connection code and choose a specific UUID to tell your phone which type of connection (serial, audio, etc.) you want to establish.

To learn more about connect to a Bluetooth device, please see the official document under Connecting as a client.

solosodium
  • 723
  • 5
  • 15