- Home /
How to receive Bluetooth-Data as Controller Input?
We are currently working on a project in which we try to use android-smartphone sensor data as cotroller input of our unity-(windows)-desktop application. We managed to program a bluetooth SPP server for C# but couldn't integrate the server into Unity3d. Our Bluetooth-Server does wait for incoming bluetooth-connections and accepts them (thanks to the 32feet.Net libary). As soon as the client has connected to the Server it should tranfer String-data over a stream.
Now the problem is, that Unity won't accept any GUID we create for our server (see code below). Is there a way we can set up a bluetooth server in a other way in unity/c# (we are used to java programming) or could we start the bluetooth-Server with "normal" .Net 3.5 and stream the data into unity ?
this is some code we tried. as we mentioned, unity won't accept our guid:
using UnityEngine;
using System.Collections;
using BluetoothServerDLL;
using InTheHand;
using InTheHand.Net;
using InTheHand.Windows;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Ports;
using InTheHand.Net.Sockets;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class BlueToothControler : MonoBehaviour
{
public static CharacterController BtController;
public static BlueToothControler Instance;
public BluetoothServer bts;
public Stream mStream;
Guid mUUID = new Guid("00011111-0000-1000-8000-00805F9B34FB");
// Use this for initialization
void Awake()
{
//set up server
try
{
//unity won't accept the mUUID in the next line
BluetoothListener blueListener = new BluetoothListener(mUUID);
blueListener.Start();
BluetoothClient conn = blueListener.AcceptBluetoothClient();
mStream = conn.GetStream();
}
catch (Exception e)
{
print(e.Message + "\n");
}
// Set up Player Control
BtController = GetComponent("CharacterController") as CharacterController;
Instance = this;
}
// Update is called once per frame
void Update()
{
if (Camera.main == null)
{
return;
}
//receive data and use it as controller input
try
{
//handle server connection
byte[] received = new byte[1024];
mStream.Read(received, 0, received.Length);
String moveString = Encoding.ASCII.GetString(received);
print("Received: " + Encoding.ASCII.GetString(received));
//OnRaiseMoveEvent(moveString);
}
catch (IOException exception)
{
print("Client has disconnected!!!!");
}
PlayerMotor.Instance.UpdateMotor();
}
}
thanks in advance.
@username Did you figure this out? I am currently working on a project that receives a constant stream of data through a Bluetooth socket in order to transform GameObjects. Also, where did you attach this script? If I am trying to establish the Bluetooth connection, would I put that piece of code in a start() function? Or awake() like you have.. does it matter? Anything helps. Thanks
Answer by mafemontoya94 · Aug 22, 2017 at 11:43 AM
Hi, do you solved this problem? I am trying to connect a balance board in unity but i am a little new in this library, could you help me please?
i succed to adapt this plugin https://github.com/Flafla2/Unity-Wiimote . You will need to make some research on the protocol used by Nintendo for balance.
Thank you alibatiste, but I realy don't understand what i should find in this code that allow me the conection witn the bluetooth dedvice, will you give me some ligth on it...
This plugin allow you to connect with Wii device in Bluetooth. You juste need to extend it to work with the balance.
@alibatiste, could you please make a tutorial on how to this.. really need it..
Answer by sofienjaballah · Jul 17, 2018 at 09:35 AM
Hi everyone! I'm developing an app for Android with unity 3D. I need that i include script in my actuel script to send app via bluetooth to another device for making vibration ( my app it turn on active vibration using smartphone coding by c#)
please give me the suggestion.
Your answer
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
My game can't find any controllers when streaming to a Steam Link or NVIDIA Shield! 0 Answers
UI issue when Streaming with 3DStreamingToolkit 0 Answers
Does Unity support Xbox 1 S controller input via bluetooth on Hololens? 0 Answers
Unity android bluetooth controller disconnect doesn't make array empty 1 Answer