简体   繁体   English

通过蓝牙接收的消息在android应用程序中拆分

[英]Received message through Bluetooth is splitted in android app

I'm developing an Android APP which relays on Bluetooth communication between the Android device and a micro controller (Raspberry Pi), I'm using this library to achieve that.我正在开发一个 Android APP,它通过 Android 设备和微控制器(Raspberry Pi)之间的蓝牙通信进行中继,我正在使用这个 来实现这一点。 I can send data normally without any problems, However, when I try to receive data from the micro controller the String gets splitted into 2, example: when I send "Hello world!"我可以正常发送数据,没有任何问题,但是,当我尝试从微控制器接收数据时,字符串被拆分为 2,例如:当我发送“Hello world!”时from the raspberry Pi, I should get:从树莓派,我应该得到:

Hello world!你好,世界!

but I get:但我得到:

H H

ello world!你好世界!

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.ahmedabdelmeged.bluetoothmc.BluetoothMC;
import com.ahmedabdelmeged.bluetoothmc.ui.BluetoothDevices;
import com.ahmedabdelmeged.bluetoothmc.util.BluetoothStates;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    public BluetoothMC mBluetoothmc = new BluetoothMC();
    private final HomeFragment mHomeFragment = new HomeFragment();
    private final MapFragment mMapFragment = new MapFragment();
    private final LogFragment mLogFragment = new LogFragment();

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

        BottomNavigationView BottomNav = findViewById(R.id.bottom_navigation);
        BottomNav.setOnNavigationItemSelectedListener(navListener);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                mHomeFragment).commit();

        mBluetoothmc.setOnDataReceivedListener(new BluetoothMC.onDataReceivedListener() {
        @Override
        public void onDataReceived(String data) {
            log.e("Main", data);
        }
    });

    }

I used this library before and it was working okay, the only difference is that I'm using a linux machine instead of windows.我之前使用过这个库,它工作正常,唯一的区别是我使用的是 linux 机器而不是 windows。 also the developer of the library is inactive so I can't ask him图书馆的开发者也不活跃,所以我不能问他

Hi you can connect to bluetooth device via Rfcomm or Gatt interface without using any third party library.嗨,您可以通过 Rfcomm 或 Gatt 接口连接到蓝牙设备,而无需使用任何第三方库。

Rfcomm is preferred when data size bigger.当数据大小较大时,首选 Rfcomm。 Bluetooth Gatt is preferred when distance between devices is farther当设备之间的距离较远时,首选蓝牙 Gatt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM