Connecting a Laser Distance Measurer (Bosch Disto GLM 50 C) with Smartphone (Android Studio)

Introduction

This article will guide you through the process of connecting a Bosch Disto GLM 50 C Laser Distance Measurer to your Android smartphone using Android Studio. We will explore the necessary components, setup steps, and coding techniques involved in this project.

Hardware Requirements

  • Bosch Disto GLM 50 C Laser Distance Measurer
  • Android Smartphone with Bluetooth capabilities
  • USB Cable (for connecting the Disto to your computer)

Software Requirements

  • Android Studio (with the latest SDK and tools)
  • Bluetooth Serial Communication Library for Android (e.g., BluetoothSPP or HC-05 Library)

Setting Up the Connection

1. Enabling Bluetooth on Your Smartphone

Ensure that Bluetooth is enabled on your smartphone.

2. Pairing the Disto with Your Smartphone

Put the Disto into pairing mode (consult the Disto’s user manual for instructions). Then, search for available Bluetooth devices on your smartphone and select the Disto GLM 50 C. Enter any required PIN if prompted.

3. Obtaining the Disto’s MAC Address

The MAC address of the Disto is needed for communication. You can find it on the Disto’s label or in your smartphone’s Bluetooth device list.

Developing the Android Application

1. Create a New Android Studio Project

Start a new Android Studio project with an empty activity.

2. Add the Bluetooth Library to Your Project

Import the chosen Bluetooth library into your project. For this example, we will use BluetoothSPP.

3. Create a Layout for Your App

Design the layout of your app in XML to display buttons for connecting, sending commands, and receiving data.

4. Write the Java Code

In your main activity’s Java file, implement the following:

  • Initialize the BluetoothSPP library
  • Connect to the Disto using its MAC address
  • Send commands to the Disto (e.g., “MEASURE”)
  • Receive measurement data from the Disto
  • Display the received data on the app’s UI

Sample Code

Java Code

package com.example.distoglm50capp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

import app.akexorcist.bluetooh.BluetoothSPP;
import app.akexorcist.bluetooh.BluetoothState;
import app.akexorcist.bluetooh.DeviceList;
import app.akexorcist.bluetooh.BluetoothSPP.BluetoothConnectionListener;

public class MainActivity extends AppCompatActivity {

    private BluetoothSPP bt;
    private TextView statusTextView;
    private Button connectButton;

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

        statusTextView = findViewById(R.id.statusTextView);
        connectButton = findViewById(R.id.connectButton);

        bt = new BluetoothSPP(this);

        // Set Bluetooth connection listener
        bt.setBluetoothConnectionListener(new BluetoothConnectionListener() {
            @Override
            public void onDeviceConnected(String name, String address) {
                statusTextView.setText("Connected to: " + name + " (" + address + ")");
                connectButton.setText("Disconnect");
            }

            @Override
            public void onDeviceDisconnected() {
                statusTextView.setText("Disconnected");
                connectButton.setText("Connect");
            }

            @Override
            public void onDeviceConnectionFailed() {
                statusTextView.setText("Connection Failed");
            }
        });

        connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
                    bt.disconnect();
                } else {
                    bt.connect("Disto GLM 50 C"); // Replace "Disto GLM 50 C" with your device name
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!bt.isBluetoothEnabled()) {
            bt.enable();
        } else {
            if (!bt.isServiceAvailable()) {
                bt.setupService();
                bt.startService(BluetoothState.DEVICE_OTHER);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bt.stopService();
    }
}

Layout XML (activity_main.xml)




    

    

Testing the Connection

Run your app on your Android device. The status TextView should display “Bluetooth Status”. Tap the Connect button to start the connection process. If the connection is successful, the status TextView will update with the connected device’s name and address, and the Connect button’s text will change to Disconnect.

Once connected, you can send commands and receive data from the Disto to test your app.


Leave a Reply

Your email address will not be published. Required fields are marked *