0
\$\begingroup\$

I have a scene where I'm connected to a BLE device and receive data in bytes. I want to switch scenes from the current Update and continue to receive the data without disconnecting. How should I do this. Here is my current code in Update:

            case States.Subscribe:
                HM10_Status.text = "Subscribing to HM10";

                BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress (_hm10, ServiceUUID, Characteristic, null, (address, characteristicUUID, bytes) => {

                    float floatprint = EnterDataQueue(bytes);
                    Debug.Log("floatprint" + floatprint.ToString());
                    HM10_Status.text = "Received Serial: " + floatprint.ToString();
                    //HM10_Status.text = "Received Serial: " + Encoding.UTF8.GetString (bytes);
                });

And the method outside:

static float EnterDataQueue(byte[] bytes)
    {
        ArduinoHM10Test aTest = new ArduinoHM10Test();
        //always load the data byte array in full
        for (int i = 0; i < bytes.Length; i++)
        {
            aTest.myQueue.Enqueue(bytes[i]);
        }

        //Dequeue the queue and check if 4 bytes have been dequeued
        if (aTest.myQueue.Count >= 4)
        {


            byte[] byteArray = new byte[4];
            for (int i = 0; i < byteArray.Length; i++)
            {
                byteArray[i] = aTest.myQueue.Dequeue();
                if (i == 3)
                {
                    break;
                }
            }

            aTest.floatnum = BitConverter.ToSingle(byteArray, 0);
        }

        Debug.Log("floatnum: " + aTest.floatnum.ToString());
        return aTest.floatnum;
        //throw new NotImplementedException();
    }
\$\endgroup\$
  • \$\begingroup\$ Presumably you considered putting your Bluetooth-handling object in a separate scene from the one you're unloading/loading alongside it, or using DontDestroyOnLoad (which does effectively this under the hood)? \$\endgroup\$ – DMGregory Jun 21 at 10:46
  • \$\begingroup\$ Hi @DMGregory, I haven't tried yet. I'll have a go with DontDestroyOnLoad as it sounds easier to implement. Thanks. \$\endgroup\$ – Conrad Addo Jun 21 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.