Ziyad Tarek
Ziyad Tarek

Reputation: 23

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. 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!" from the raspberry Pi, I should get:

Hello world!

but I get:

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. also the developer of the library is inactive so I can't ask him

Upvotes: 0

Views: 45

Answers (1)

Rustam Samandarov
Rustam Samandarov

Reputation: 892

Hi you can connect to bluetooth device via Rfcomm or Gatt interface without using any third party library.

Rfcomm is preferred when data size bigger. Bluetooth Gatt is preferred when distance between devices is farther

Upvotes: 1

Related Questions