Android 使用Unity 3D进行蓝牙读取
我有一个带有HC-05蓝牙模块的arduino nano。我需要从该模块接收一些数据到Unity中,所以我制作了一个android库(在android Studio中),可以处理蓝牙连接 问题是,在我收到数据的前2秒钟,过程似乎变慢了。我在数据集之间等待了10秒钟 我读到一些关于模块的标准绑定速率的信息,我认为这可能是问题的根源,但当我从AT命令模式更改它时,似乎什么都没有改变 *当我从蓝牙终端(Google Play)读取数据时,感觉比我的应用程序更快。 请给我一些想法好吗 图书馆代码Android 使用Unity 3D进行蓝牙读取,android,unity3d,bluetooth,Android,Unity3d,Bluetooth,我有一个带有HC-05蓝牙模块的arduino nano。我需要从该模块接收一些数据到Unity中,所以我制作了一个android库(在android Studio中),可以处理蓝牙连接 问题是,在我收到数据的前2秒钟,过程似乎变慢了。我在数据集之间等待了10秒钟 我读到一些关于模块的标准绑定速率的信息,我认为这可能是问题的根源,但当我从AT命令模式更改它时,似乎什么都没有改变 *当我从蓝牙终端(Google Play)读取数据时,感觉比我的应用程序更快。 请给我一些想法好吗 图书馆代码 pub
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();
}
}
}
公共类蓝牙连接器{
私有静态最终BluetoothConnector ourInstance=新BluetoothConnector();
公共静态BluetoothConnector getInstance(){
回归自然状态;
}
ArrayList devices_true=new ArrayList();
ArrayList设备_name=新的ArrayList();
最终UUID mUUID=UUID.fromString(“000011101-0000-1000-8000-00805F9B34FB”);
蓝牙适配器;
蓝牙插座;
蓝牙设备cur_设备;
蓝牙设备con_设备;
设置设备;
输入流输入流;
输出流输出流;
私有BluetoothConnector(){}
公共字符串looper_Connect(){
试一试{
adapter=BluetoothAdapter.getDefaultAdapter();
设备=适配器.getBondedDevices();
用于(蓝牙设备:设备)
{
设备\u true.添加(设备);
device_names.add(device.getName());
}
cur_device=devices_true.get(device_names.indexOf(“LooperController”);
如果(cur_device==null)返回“未找到控制器”;
con_device=adapter.getRemoteDevice(cur_device.getAddress());
skt=con_device.createInsurerCommsocketToServiceRecord(mUUID);
adapter.cancelDiscovery();
skt.connect();
inputStream=skt.getInputStream();
outputStream=skt.getOutputStream();
write(“设备已连接”.getBytes());
outputStream.flush();
返回“控制器已连接!”;
}
捕获(例外情况除外)
{
return“Error:+ex.toString();
}
}
公共字符串ReadData(){
字节[]主数据=新字节[256];
试一试{
if(inputStream.available()>0)
inputStream.read(主数据);
否则返回“”;
返回新字符串(主数据);
}捕获(IOE异常){
返回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();
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课主要内容:单一行为{
//用于初始化
AndroidJavaClass蓝牙通信;
AndroidJavaObject bluetoothCom;
公共游戏对象幼崽;
公共字符串sol;
字符串d;
无效开始(){
尝试
{
bluetoothComunication=新的AndroidJavaClass(“com.example.bluetoothlooper.BluetoothConnector”);
bluetoothCom=bluetoothComunication.CallStatic(“getInstance”);
sol=bluetoothCom.Call(“Loooper_Connect”);
}
捕获(例外情况除外)
{
sol=例如ToString();
}
}
字符串数据;
无效更新(){
尝试
{
data=bluetoothCom.Call(“ReadData”);
如果(data.Length>1)
{
cub.SetActive(真);
}
其他的
{
立方集活(假);
}
}
捕获(例外情况除外)
{
数据=例如ToString();
}
}
}
我知道了发生了什么,如果有人有同样的问题,我会解释这一点。java的inputStream无法识别行尾,因此当您读取当前缓冲区时,您可以从中获取所有信息。
例如,如果你每秒钟写一次“Hello”,当你读这篇文章时,2秒钟后的结果将是“HelloHello”,这会在主程序中产生一些非常奇怪的东西(unity)
所以…要解决这个问题,您需要实现一个BufferedReader,它将逐行读取接收到的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String d = reader.readLine();
return d;
我希望我正确地知道那里发生了什么。如果我错了,请纠正我 我知道发生了什么,如果有人有同样的问题,我会解释这一点。来自java的inputStream无法识别行尾,因此当您读取当前缓冲区时,您可以从中获取所有信息。 例如,如果你每秒钟写一次“Hello”,当你读这篇文章时,2秒钟后的结果将是“HelloHello”,这会在主程序中产生一些非常奇怪的东西(unity) 所以…要解决这个问题,您需要实现一个BufferedReader,它将逐行读取接收到的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String d = reader.readLine();
return d;
我希望我正确地知道那里发生了什么。如果我错了,请纠正我