Hi! Can anyone help me with this code I’m having an error converting string to float. the error is in “2A58”.
using System;
using TMPro;
using UnityEngine;
public class Fusion : MonoBehaviour
{
private string DeviceName = “SMART TATTOO”;
private string ServiceUUID = “180C”;
private string Characteristic = “2A58”;
private string Characteristic2 = “2A56”;
public float speed = 5.0f;
enum States
{
None,
Scan,
Connect,
Subscribe,
Unsubscribe,
Disconnect,
Communication,
}
private bool _workingFoundDevice = true;
private bool _connected = false;
private float _timeout = 0f;
private States _state = States.None;
private bool _foundID = false;
private string _deviceAddress;
[SerializeField] private TMP_Text stateText;
[SerializeField] private Transform cube;
//private Quaternion inverseQt;
//private Quaternion rawQt;
void Reset()
{
_workingFoundDevice =
false; // used to guard against trying to connect to a second device while still connecting to the first
_connected = false;
_timeout = 0f;
_state = States.None;
_foundID = false;
_deviceAddress = null;
}
void SetState(States newState, float timeout)
{
_state = newState;
_timeout = timeout;
}
void setStateText(string text)
{
if (stateText == null) return;
stateText.text = text;
}
void StartProcess()
{
setStateText("Initializing...");
Reset();
BluetoothLEHardwareInterface.Initialize(true, false, () =>
{
SetState(States.Scan, 0.1f);
setStateText("Initialized");
}, (error) => { BluetoothLEHardwareInterface.Log("Error: " + error); });
}
// Use this for initialization
void Start()
{
StartProcess();
string Characteristic = "2A58";
cube.transform.rotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if (_timeout > 0f)
{
_timeout -= Time.deltaTime;
if (_timeout <= 0f)
{
_timeout = 0f;
switch (_state)
{
case States.None:
break;
case States.Scan:
setStateText("Scanning for SMART TATTOO devices...");
BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, (address, name) =>
{
// we only want to look at devices that have the name we are looking for
// this is the best way to filter out devices
if (name.Contains(DeviceName))
{
_workingFoundDevice = true;
// it is always a good idea to stop scanning while you connect to a device
// and get things set up
BluetoothLEHardwareInterface.StopScan();
// add it to the list and set to connect to it
_deviceAddress = address;
SetState(States.Connect, 0.5f);
_workingFoundDevice = false;
}
}, null, false, false);
break;
case States.Connect:
// set these flags
_foundID = false;
setStateText("Connecting to SMART TATTOO");
// note that the first parameter is the address, not the name. I have not fixed this because
// of backwards compatiblity.
// also note that I am note using the first 2 callbacks. If you are not looking for specific characteristics you can use one of
// the first 2, but keep in mind that the device will enumerate everything and so you will want to have a timeout
// large enough that it will be finished enumerating before you try to subscribe or do any other operations.
BluetoothLEHardwareInterface.ConnectToPeripheral(_deviceAddress, null, null,
(address, serviceUUID, characteristicUUID) =>
{
if (IsEqual(serviceUUID, ServiceUUID))
{
// if we have found the characteristic that we are waiting for
// set the state. make sure there is enough timeout that if the
// device is still enumerating other characteristics it finishes
// before we try to subscribe
if (IsEqual(characteristicUUID, Characteristic))
{
_connected = true;
SetState(States.Subscribe, 2f);
setStateText("Connected to SMART TATTOO");
}
}
}, (disconnectedAddress) =>
{
BluetoothLEHardwareInterface.Log("Device disconnected: " + disconnectedAddress);
setStateText("Disconnected");
});
break;
case States.Subscribe:
setStateText("Subscribing to SMART TATTOO");
BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_deviceAddress,
ServiceUUID,
Characteristic, null,
(address, characteristicUUID, bytes) =>
{
var String = Characteristic.Split(","[0]);
float GX = float.Parse("2A58"[2]) / 10000f; // ** error "2A58" **
float GY = float.Parse("2A58"[4]) / 10000f;
float GZ = float.Parse("2A58"[6]) / 10000f;
cube.transform.Rotate(new Vector3(GZ, GX, -GY) * Time.deltaTime * speed);
});
// set to the none state and the user can start sending and receiving data
_state = States.None;
break;
case States.Unsubscribe:
BluetoothLEHardwareInterface.UnSubscribeCharacteristic(_deviceAddress, ServiceUUID,
Characteristic,
null);
SetState(States.Disconnect, 4f);
break;
case States.Disconnect:
if (_connected)
{
BluetoothLEHardwareInterface.DisconnectPeripheral(_deviceAddress, (address) =>
{
BluetoothLEHardwareInterface.DeInitialize(() =>
{
_connected = false;
_state = States.None;
});
});
}
else
{
BluetoothLEHardwareInterface.DeInitialize(() => { _state = States.None; });
}
break;
}
}
}
}
string FullUUID(string uuid)
{
return "0000" + uuid + "-0000-1000-8000-00805F9B34FB";
}
bool IsEqual(string uuid1, string uuid2)
{
if (uuid1.Length == 4)
uuid1 = FullUUID(uuid1);
if (uuid2.Length == 4)
uuid2 = FullUUID(uuid2);
return (uuid1.ToUpper().Equals(uuid2.ToUpper()));
}
}
,Hi! Can anyone help me with this code I’m having an error converting string to float. the error is in “2A58”.
public class Fusion : MonoBehaviour
{
private string DeviceName = “SMART TATTOO”;
private string ServiceUUID = “180C”;
private string Characteristic = “2A58”;
private string Characteristic2 = “2A56”;
public float speed = 5.0f;
void Start()
{
StartProcess();
string Characteristic = "2A58";
cube.transform.rotation = transform.rotation;
}
void Update()
{
var String = Characteristic.Split(“,”[0]);
float GX = float.Parse("2A58"[2]) / 10000f;
float GY = float.Parse("2A58"[4]) / 10000f;
float GZ = float.Parse("2A58"[6]) / 10000f;
cube.transform.Rotate(new Vector3(GZ, GX, -GY) * Time.deltaTime * speed);
}
}