Bluetooth connected to Android

The App receives the data from the simulator via Bluetooth sent by the Arduino uno. In the code snippet below, it is explained how the App respectively the Bluetooth adapter pairs with the adapter in the smartphone. If the smartphone's Bluetooth adapter is disabled, the phone shows the option to turn on the adapter in order to pair it with the adapter sending the data from the driving simulator(method turnOnBluetooth(...)). The findHC05Device(...) is used to really pair the two adapters in order to get the communication running. To inform the User about the actions, there are Toasts showing several messages in both error cases and successful cases.

 public void turnOnBluetooth(View v) {
        if (!BA.isEnabled()) {
            Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(turnOn, 0);
            Toast.makeText(getApplicationContext(), "Aktiviere Bluetooth", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Bluetooth bereits aktiviert", Toast.LENGTH_SHORT).show();
        }
        if (BA.isEnabled()) {
            button_turn_on_bl.setEnabled(false);
            text_bl_status.setText(getString(R.string.bl_on));
            text_bl_status.setTextColor(0xff3d7f12);
        }
    }

    public void findHC05Device(View v) {
        pairedDevices = BA.getBondedDevices();
        if (!pairedDevices.isEmpty()) {
            for (BluetoothDevice bt : pairedDevices) {
                if (bt.getAddress().equalsIgnoreCase("98:D3:31:F5:12:1E")) {
                    myDevice = bt;
                    Toast.makeText(getApplicationContext(), "HC-05 verbunden", Toast.LENGTH_SHORT).show();
                    try {
                        openBT();
                    } catch (IOException e) {
                        Toast.makeText(getApplicationContext(), "Arduino nicht gefunden", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                }
            }
        } else {
            Toast.makeText(getApplicationContext(), "HC-05 nicht verbunden", Toast.LENGTH_SHORT).show();
        }
    }

The following screenshot shows an example of a message within the app.

app message

The Screenshot shows a dialog where the user can either accept or decline the activation of the Bluetooth adapter. As the adapter is necessary for the app to work properly, it is highly recommended to accept the activation of the Bluetooth adapter. The toast on the lower end of the image shows that the Bluetooth adapter will be activated as the user pressed accept on the dialog. (The Dialog will dispose after pressing accept or decline, it is visible here for demonstration)

Continue reading
Rate this blog entry:
0
2039 Hits 0 Comments