将激光测距仪 (Bosch Disto GLM 50 C) 与智能手机 (Android Studio) 连接

Posted

技术标签:

【中文标题】将激光测距仪 (Bosch Disto GLM 50 C) 与智能手机 (Android Studio) 连接【英文标题】:Connecting a Laser Distance Measurer (Bosch Disto GLM 50 C) with Smartphone (Android Studio) 【发布时间】:2017-03-13 22:32:44 【问题描述】:

我遇到了一个特殊问题(我认为)。对于一个研究项目,我必须制作一个可以连接到激光测距仪(Bosch GLM 50 C Distometer)的 android 应用程序。到目前为止,我在 *** 和其他来源阅读了无数教程和提示。

我是 Android 新手,有点不知所措。任务是创建一个应用程序,读取博世设备上的测量距离并通过蓝牙在智能手机上显示/保存。

现在我的具体问题是:是否可以读取从蓝牙设备发送的数据(例如 2.083m)?有什么建议可以实现吗?

按照我找到的本教程,我能够与设备建立连接:

package com.test.bluetooth;

import java.io.DataInputStream;

public class Main_Activity extends Activity implements OnItemClickListener 

TextView measuredValue;
ArrayAdapter<String> listAdapter;
ListView listView;
BluetoothAdapter btAdapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
IntentFilter filter;
BroadcastReceiver receiver;
String tag = "debugging";
Handler mHandler = new Handler()
    @Override
    public void handleMessage(Message msg) 
        // TODO Auto-generated method stub
        Log.i(tag, "in handler");
        super.handleMessage(msg);
        switch(msg.what)
        case SUCCESS_CONNECT:
            // DO something
            ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
            // Toast.makeText(getApplicationContext(), "VERBUNDEN", 0).show();
            String s = "Verbindung erfolgreich";
            connectedThread.write(s.getBytes());
            Log.i(tag, "connected");
            break;
        case MESSAGE_READ:
            byte[] readBuf = (byte[])msg.obj;
            String string = new String(readBuf);
            // Toast.makeText(getApplicationContext(), string, 0).show();
            break;
        
    
;
@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();
    if(btAdapter==null)
        // Toast.makeText(getApplicationContext(), "Bluetooth nicht verfügbar", 0).show();
        finish();
    
    else
        if(!btAdapter.isEnabled())
            turnOnBT();
        

        getPairedDevices();
        startDiscovery();
    



private void startDiscovery() 
    // TODO Auto-generated method stub
    btAdapter.cancelDiscovery();
    btAdapter.startDiscovery();


private void turnOnBT() 
    // TODO Auto-generated method stub
    Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(intent, 1);

private void getPairedDevices() 
    // TODO Auto-generated method stub
    devicesArray = btAdapter.getBondedDevices();
    if(devicesArray.size()>0)
        for(BluetoothDevice device:devicesArray)
            pairedDevices.add(device.getName());

        
    

private void init() 
    // TODO Auto-generated method stub
    listView=(ListView)findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    listAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
    listView.setAdapter(listAdapter);
    btAdapter = BluetoothAdapter.getDefaultAdapter();
    pairedDevices = new ArrayList<String>();
    filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    devices = new ArrayList<BluetoothDevice>();
    receiver = new BroadcastReceiver()
        @Override
        public void onReceive(Context context, Intent intent) 
            // TODO Auto-generated method stub
            String action = intent.getAction();

            if(BluetoothDevice.ACTION_FOUND.equals(action))
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                devices.add(device);
                String s = "";
                for(int a = 0; a < pairedDevices.size(); a++)
                    if(device.getName().equals(pairedDevices.get(a)))
                        //append 
                        s = "Gekoppelt";
                        break;
                    
                

                listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());
            

            else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
                // run some code
            
            else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
                // run some code



            
            else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action))
                if(btAdapter.getState() == btAdapter.STATE_OFF)
                    turnOnBT();
                
            

        
    ;

    registerReceiver(receiver, filter);
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    registerReceiver(receiver, filter);
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(receiver, filter);
     filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(receiver, filter);



@Override
protected void onPause() 
    // TODO Auto-generated method stub
    super.onPause();
    unregisterReceiver(receiver);


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_CANCELED)
            Toast.makeText(getApplicationContext(), "Bluetooth muss aktiviert sein", Toast.LENGTH_SHORT).show();
            finish();
        
    
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) 
        // TODO Auto-generated method stub

        if(btAdapter.isDiscovering())
            btAdapter.cancelDiscovery();
        
        if(listAdapter.getItem(arg2).contains("Gekoppelt"))

            BluetoothDevice selectedDevice = devices.get(arg2);
            ConnectThread connect = new ConnectThread(selectedDevice);
            connect.start();
            Log.i(tag, "in click listener");
        
        else
            // Toast.makeText(getApplicationContext(), "Gerät ist nicht gekoppelt", 0).show();
        
    

    private class ConnectThread extends Thread 

        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) 
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;
            Log.i(tag, "construct");
            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try 
                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
             catch (IOException e)  
                Log.i(tag, "get socket failed");

            
            mmSocket = tmp;
        

        public void run() 
            // Cancel discovery because it will slow down the connection
            btAdapter.cancelDiscovery();
            Log.i(tag, "Verbindung - läuft");
            try 
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
                Log.i(tag, "Verbindung - erfolgreich");
             catch (IOException connectException)     Log.i(tag, "connect failed");
                // Unable to connect; close the socket and get out
                try 
                    mmSocket.close();
                 catch (IOException closeException)  
                return;
            

            // Do work to manage the connection (in a separate thread)

            mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
        



        /** Will cancel an in-progress connection, and close the socket */
        public void cancel() 
            try 
                mmSocket.close();
             catch (IOException e)  
        
    

    private class ConnectedThread extends Thread 
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) 
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try 
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
             catch (IOException e)  

            mmInStream = tmpIn;
            mmOutStream = tmpOut;

        

        public void run() 
            byte[] buffer;  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) 
                try 
                    // Read from the InputStream
                    buffer = new byte[1024];
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();

                 catch (IOException e) 
                    break;
                
            
        

        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) 
            try 
                mmOutStream.write(bytes);
             catch (IOException e)  
        

        /* Call this from the main activity to shutdown the connection */
        public void cancel() 
            try 
                mmSocket.close();
             catch (IOException e)  
        
    


在那之后,我找到了这个例子,它应该从设备读取传入的数据,但它没有工作:

try 
        Log.d((String) this.getTitle(), "Closing Server Socket.....");
        mmServerSocket.close();``

        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams

        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();

        DataInputStream mmInStream = new DataInputStream(tmpIn);
        DataOutputStream mmOutStream = new DataOutputStream(tmpOut);

        // here you can use the Input Stream to take the string from the client  whoever is connecting
        //similarly use the output stream to send the data to the client


        text.setText(mmInStream.toString());
     catch (Exception e) 
        //catch your exception here
    

【问题讨论】:

能否分享您提到的教程链接? 关于这个问题的任何更新? 您好,您有什么可行的解决方案吗? 您好,您找到解决方案了吗? 博世现在拥有an official API。当你问的时候它不可用吗?还是您的练习目标是在没有官方 API 的情况下进行? 【参考方案1】:

博世 PLR 50 C(基本上是更便宜的版本)使用博世 PLR 15 中已知的协议。我可以想象 GLM 50 C 也使用它。

有一个post on EEVBlog.com 列出了最重要的命令:

示例措施:发送C04000EE --> 回复00 04 13 0E 00 00 32 更改字节顺序:13 0E 00 00 --> 00 00 0E 13 距离单位:0x00000E13*0,05 = 180mm

【讨论】:

【参考方案2】:

有一个逆向工程协议:pymtprotocol 可在 github page 获得,用于 Bosch GLM 100 C,它是同一系列的设备,所以希望它可以与 GLM 50 C 一起使用。不幸的是,它是仅限 Mac OS X,我希望它独立于操作系统,因此我可以在尝试为 Android 重写协议之前在 Ubuntu 上试用它。我是编程的初学者,无论我尝试什么,我都走入了一条死胡同。如果有人可以派生 pymtprotocol 并使其可用于 Linux 或 Android,我将不胜感激。

附:所有学分都转到Peter Iannucci。

【讨论】:

以上是关于将激光测距仪 (Bosch Disto GLM 50 C) 与智能手机 (Android Studio) 连接的主要内容,如果未能解决你的问题,请参考以下文章

详述激光三角测距法原理

“天琴计划”激光测距实现大突破,地月激光测距究竟怎么回事?

STM32F103实现激光测距传感器测距WT-VL53L0 L1

STM32F103实现激光测距传感器测距WT-VL53L0 L1

[激光原理与应用-43]:《光电检测技术-10》- 激光测距原理方案与案例分析:TOF VL53L0X模块

三角测距激光雷达与TOF激光雷达大比拼