使用Unity 3D读取蓝牙

时间:2018-06-13 10:30:11

标签: android unity3d bluetooth

我有一个带有HC-05蓝牙模块的arduino nano。我需要从该模块接收一些数据到Unity,所以我制作了一个可以处理蓝牙连接的Android库(在Android Studio中)。

问题在于。在接收数据的前2秒内,进程似乎变慢。我在数据集之间等待最多10秒。

我读了一些关于模块的标准绑定速率的内容,我认为这可能是问题的根源,但是当我从AT命令模式更改它时。似乎没有任何改变。

*当我从蓝牙终端(来自Google Play)读取数据时。感觉比我的应用程序更快。 请问一些想法?

图书馆代码

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();
    }
}
}

我的统一代码:

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();
    }
}
}

1 个答案:

答案 0 :(得分:0)

我弄清楚发生了什么,我会在有人遇到同样问题的情况下解释这个问题。来自java的inputStream不会识别行结尾,所以当你读取当前缓冲区时,你会从中获取所有内容。 例如,如果你每秒写一次&#34; Hello&#34;当你读完这个时,2秒后的结果将是&#34; HelloHello&#34;这在主程序中产生了一些奇怪的东西(统一)。

所以...要解决这个问题,你需要实现一个BufferedReader,它将逐行读取接收到的数据。

     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String d = reader.readLine();

        return d;

我希望我能正确地弄清楚那里发生了什么。如果我错了,请纠正我!