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();