Reputation: 79
I have an arduino nano with a HC-05 bluetooth module.I need to receive some data from that module into Unity so i made an android librarie(in Android Studio) that can handle the bluetooth connection.
The problem is that.For the first 2 seconds i receive data then the process seems to slow down.I wait up to 10 seconds between the data sets.
I read something about the standard bound rate of the module and i think that could be the source of the problem but when i change it from AT commands mode.Nothing seems to change.
*When i read the data from an Bluetooth terminal(from Google Play).It feels mutch faster than my app. Some ideas please?
The library code
public class BluetoothConnector {
private static final BluetoothConnector ourInstance = new BluetoothConnector();
public static BluetoothConnector getInstance() {
return ourInstance;
}
ArrayList<BluetoothDevice> devices_true = new ArrayList<>();
ArrayList<String>device_names = new ArrayList<>();
final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothAdapter adapter;
BluetoothSocket skt;
BluetoothDevice cur_device;
BluetoothDevice con_device;
Set<BluetoothDevice> devices;
InputStream inputStream;
OutputStream outputStream;
private BluetoothConnector() {}
public String Loooper_Connect(){
try{
adapter = BluetoothAdapter.getDefaultAdapter();
devices = adapter.getBondedDevices();
for(BluetoothDevice device:devices)
{
devices_true.add(device);
device_names.add(device.getName());
}
cur_device = devices_true.get(device_names.indexOf("LooperController"));
if(cur_device == null)return "No controller found";
con_device = adapter.getRemoteDevice(cur_device.getAddress());
skt = con_device.createInsecureRfcommSocketToServiceRecord(mUUID);
adapter.cancelDiscovery();
skt.connect();
inputStream = skt.getInputStream();
outputStream = skt.getOutputStream();
outputStream.write("Device Connected".getBytes());
outputStream.flush();
return "Controller Connected!";
}
catch (Exception ex)
{
return "Error : " + ex.toString();
}
}
public String ReadData(){
byte[] primary_data = new byte[256];
try {
if(inputStream.available() > 0)
inputStream.read(primary_data);
else return "";
return new String(primary_data);
} catch (IOException e) {
return e.toString();
}
}
}
And my unity code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Main : MonoBehaviour {
// Use this for initialization
AndroidJavaClass bluetoothComunication;
AndroidJavaObject bluetoothCom;
public GameObject cub;
public string sol;
string d;
void Start () {
try
{
bluetoothComunication = new AndroidJavaClass("com.example.bluetoothlooper.BluetoothConnector");
bluetoothCom = bluetoothComunication.CallStatic<AndroidJavaObject>("getInstance");
sol = bluetoothCom.Call<string>("Loooper_Connect");
}
catch (Exception ex)
{
sol = ex.ToString();
}
}
string data;
void Update () {
try
{
data = bluetoothCom.Call<string>("ReadData");
if (data.Length > 1)
{
cub.SetActive(true);
}
else
{
cub.SetActive(false);
}
}
catch(Exception ex)
{
data = ex.ToString();
}
}
}
Upvotes: 1
Views: 6246
Reputation: 79
I figured out what happend and i will explain this in case that somebody has the same problem.The inputStream from java doesn't recognize line endings, so when you read the current buffer, you take everything from there. For example if you write every second "Hello",when you will read this the result after 2 seconds will be "HelloHello" and that creates something verry strange in the main program(unity).
SO...to resolve this problem you need to implement an BufferedReader that will read line by line the received data.
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String d = reader.readLine();
return d;
I hope i figured out correctly what happens there.If i am wrong please correct me!
Upvotes: 4