//Receive Euler
    public static double[] ReceiveEuler()
    {
        double[] euler = new double[3];

        Debug.Log("ENTROU NO RECEBER Euler");
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBSNUUID(_serviceReadUUID), FullBSNUUID(_readEulerUUID), (deviceAddress, notification) =>
        {
        }, (deviceAddress2, characteristic, data) =>
        {
            //Receive ROLL data
            byte[] dataRoll = { data[0], data[1], data[2], data[3] };
            euler[0]        = System.BitConverter.ToInt32(dataRoll, 0) / 65536.0; // 44100.0
            //Debug.Log("Roll = " + euler[0]);

            //Receive PITCH data
            byte[] dataPitch = { data[4], data[5], data[6], data[7] };
            euler[1]         = System.BitConverter.ToInt32(dataPitch, 0) / 65536.0; // 44100.0
            //Debug.Log("Pitch = " + euler[1]);

            //Receive YAW data
            byte[] datayaw = { data[8], data[9], data[10], data[11] };
            euler[2]       = System.BitConverter.ToInt32(datayaw, 0) / 65536.0; // 44100.0
            //Debug.Log("Yaw = " + euler[2]);

            // Debug.Log("ENVIOU BYTES : " + data);
            // Debug.Log("ENVIOU BYTES STRING : " + Encoding.ASCII.GetString(data));
            // Debug.Log("ENVIOU BYTES STRING : " + Encoding.Default.GetString(data));
            // Debug.Log("ENVIOU BYTES Lenght : " + data.Length);
        });
        Debug.Log("DEPOIS DO READ");
        return(euler);
    }
Exemplo n.º 2
0
    IEnumerator SubscribeSensor3()
    {
        connectedAmount++;
        Debug.Log("Connected amount is:" + connectedAmount);
        Sup.text = "Subscribing to sensor 3...";
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(Address3, _serviceUUID, _characteristicUUID, (deviceAddress, notification) =>
        {
        }, (deviceAddress2, characteristic, data) =>
        {
            seonsor2msgArrayData = data;
            Sensor2DataToTransformArm(seonsor2msgArrayData);

            /*
             * isSensor3Subscribed = true;
             * Sup.text = "Subscribed to sensor 3.";
             * statusImageSensor2.texture = greenLight;
             *
             * limbAngle3 = handleMessage_Premius2(data);
             * ShoulderL.transform.eulerAngles = new Vector3(0, 0, (270+limbAngle3));        //Insert the handled data to a joint (only Z-axis)
             *
             * // Save highest ROM during game play
             * if ((limbAngle3 > GameControl.instance.maxRomAchievedRight) && (GameControl.instance.workoutTimer > 0.0f))
             * {
             *  GameControl.instance.maxRomAchievedRight = limbAngle3;
             * }
             */
        });
        yield return(null);
    }
Exemplo n.º 3
0
    public static void subscribe()
    {
        if (!isConnected)
        {
            return;
        }
        BTLog = "readStart!\n";

        /*BTLog += linkData.address;
         * BTLog += BluetoothData.fullUUID(linkData.service);
         * BTLog += BluetoothData.fullUUID(linkData.rCharacteristic);*/
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(
            linkData.address, BluetoothData.fullUUID(linkData.service), BluetoothData.fullUUID(linkData.rCharacteristic),
            (deviceAddress, notification) => {; },
            (deviceAddress2, characteristic, data) =>
        {
            if (deviceAddress2.CompareTo(linkData.address) == 0)                     //讀資料
            {
                if (data.Length > 0)
                {
                    string s = Encoding.UTF8.GetString(data);                             ///Byte to string
                    receiveText(s);
                }
            }
        });
    }
Exemplo n.º 4
0
    public IEnumerator Subscribe(string _deviceID, string _service, string _channel, Action <byte[]> _handler)
    {
        //  TODO: 去掉延时,改为等待某个特定事件
        yield return(new WaitForSeconds(0.5f));

        _channel = _channel.ToUpper();
        Debug.Log("Start Subscribe Service " + _service + ", Channel " + _channel);
        if (!subscribeHandlers.ContainsKey(_channel))
        {
            //  新增
            subscribeHandlers.Add(_channel, _handler);
        }
        else
        {
            //  替换
            subscribeHandlers[_channel] = _handler;
        }
        bool complete = false;

        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_deviceID, _service, _channel, (deviceID, characteristic) =>
        {
            Debug.Log("Subscribe " + characteristic + " of " + deviceID);
            complete = true;
        }, SubscribeHandler);
        yield return(new WaitUntil(() => complete));
    }
    //Receive Gravity Vector
    public static double[] ReceiveGravityVector()
    {
        double[] gravityVector = new double[3];
        Debug.Log("ENTROU NO RECEBER gravidade vetor");
        Debug.Log("NOME = " + bsnDevice.Name);
        Debug.Log("ADRESS = " + bsnDevice.Address);
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBSNUUID(_serviceReadUUID), FullBSNUUID(_readGravityVectorUUID), (deviceAddress, notification) =>
        {
        }, (deviceAddress2, characteristic, data) =>
        {
            //Receive X-AXYS data
            byte[] dataX     = { data[0], data[1], data[2], data[3] };
            gravityVector[0] = System.BitConverter.ToSingle(dataX, 0);
            //Debug.Log("X = " + gravityVector[0]);

            //Receive Y-AXYS data
            byte[] dataY     = { data[4], data[5], data[6], data[7] };
            gravityVector[1] = System.BitConverter.ToSingle(dataY, 0);
            //Debug.Log("Y = " + gravityVector[1]);

            //Receive Z-AXYS data
            byte[] dataZ     = { data[8], data[9], data[10], data[11] };
            gravityVector[2] = System.BitConverter.ToSingle(dataZ, 0);
            //Debug.Log("Z = " + gravityVector[2]);

            // Debug.Log("ENVIOU BYTES : " + data);
            // Debug.Log("ENVIOU BYTES STRING : " + Encoding.ASCII.GetString(data));
            // Debug.Log("ENVIOU BYTES STRING : " + Encoding.Default.GetString(data));
            // Debug.Log("ENVIOU BYTES Lenght : " + data.Length);
        });
        Debug.Log("DEPOIS DO READ");
        return(gravityVector);
    }
Exemplo n.º 6
0
        /// <summary>
        /// Subscribe to a given characteristic and sets a delegate where the data will be returned whenever the server
        /// device notifies any new data
        /// </summary>
        public static void Subscribe(Characteristic characteristic, OnDataReceived onDataReceived)
        {
            CheckIfInitialized();

            if (CurrentState != State.Connected && CurrentState != State.Subscribing)
            {
                throw new Exception("You need to be connected to a device before you can subscribe to any characteristic");
            }

            SetState(State.Subscribing);
            Observable
            .Timer(TimeSpan.FromSeconds(2f))
            .Subscribe(_ =>
            {
                Debug.Log("Subscribing..");
                BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(characteristic.Device.Address,
                                                                                      characteristic.ServiceUuid, characteristic.CharacteristicUuid,
                                                                                      (__, ___) => SetState(State.Communicating),
                                                                                      (address, characteristicUuid, bytes) =>
                {
                    onDataReceived?.Invoke(new Data
                    {
                        Characteristic = characteristic,
                        RawData        = bytes
                    });
                });
            });
        }
Exemplo n.º 7
0
    private IEnumerator SubscribeCharacteristic()
    {
        MyDebug.LogGreen("Subscribe Characteristic!");
        yield return(new WaitForSeconds(1f));

        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(BluetoothController.Instance.CurPeripheralInstance.address,
                                                                              BluetoothController.Instance.ServiceUUID,
                                                                              BluetoothController.Instance.ReadUUID, NotificationAction,
                                                                              BluetoothController.Instance.CentralReceiveMessage);
    }
Exemplo n.º 8
0
 /// <summary>
 /// 订阅蓝牙消息
 /// </summary>
 public void SubscribeBluetoothMsg(string bleAddress)
 {
     Show("开始订阅蓝牙消息... ...");
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bleAddress, _serviceUUID, _readCharacteristicUUID, (deviceAddress, notification) => { },
                                                                           (deviceAddress, characteristicUUID, data) =>
     {
         //Show(BitConverter.ToString(data));
         FireBtn(data);
     });
 }
 //Receive Compass data
 public static void ReceiveCompass(DeviceObject bsnDevice)
 {
     Debug.Log("ENTROU NO RECEBER Compass");
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBSNUUID(_serviceReadUUID), FullBSNUUID(_readCompassUUID), (deviceAddress, notification) =>
     {
     }, (deviceAddress2, characteristic, data) =>
     {
         //TODO RETORNAR BYTES ( data )
     });
     Debug.Log("DEPOIS DO READ");
 }
    void subscribeToDevice()
    {
        bluetoothStatus.text = "Subscribing...";

        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_JDY16, ServiceUUID, Characteristic, null, (address, characteristicUUID, bytes) => {
            bluetoothStatus.text = "" + Encoding.UTF8.GetString(bytes);
        });

        _state = States.None;
        bluetoothStatus.text = "All set!";
    }
Exemplo n.º 11
0
 //Receive Battery Status
 public static void ReceiveBatteryStatus()
 {
     Debug.Log("ENTROU NO RECEBER bateria");
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBatteryBSNUUID(_serviceBatteryUUID), FullBatteryBSNUUID(_readBatteryUUID), (deviceAddress, notification) =>
     {
     }, (deviceAddress2, characteristic, data) =>
     {
         int batteryStatus = data[0];
         Debug.Log("Status da Bateria = " + batteryStatus);
     });
     Debug.Log("DEPOIS DO READ");
 }
Exemplo n.º 12
0
 IEnumerator SubscribeSensor4()
 {
     Sup.text = "Subscribing to sensor 4...";
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(Address4, _serviceUUID, _characteristicUUID, (deviceAddress, notification) =>
     {
     }, (deviceAddress2, characteristic, data) =>
     {
         Sup.text = "Subscribed to sensor 4.";
         handleMessage_Premius4(data);                   //Transfer the received data to the function that handles it
     });
     yield return(null);
 }
Exemplo n.º 13
0
 //Receive steps previously calibrated in BSN
 public static void ReceiveSteps()
 {
     Debug.Log("ENTROU NO RECEBER passo");
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBSNUUID(_serviceReadUUID), FullBSNUUID(_readStepUUID), (deviceAddress, notification) =>
     {
     }, (deviceAddress2, characteristic, data) =>
     {
         int passo = data[0];
         Debug.Log("PASSOS = " + ((data[1] * 256) + data[0]));
         //TODO RETORNAR BYTES
     });
     Debug.Log("FIM DO RECEBER");
 }
Exemplo n.º 14
0
 IEnumerator SubscribeSensor4()
 {
     Sup.text = "Subscribing to sensor 4...";
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(Address4, _serviceUUID, _characteristicUUID, (deviceAddress, notification) =>
     {
     }, (deviceAddress2, characteristic, data) =>
     {
         Sup.text    = "Subscribed to sensor 4.";
         quaternion4 = handleMessage_Premius(data);                                   //Transfer the received data to the function that handles it
         ElbowL.transform.eulerAngles = new Vector3(0, 0, quaternion4.eulerAngles.z); //Insert the handled data to a joint (only Z-axis)
     });
     yield return(null);
 }
Exemplo n.º 15
0
    // Update is called once per frame
    void Update()
    {
        if (_readFound && _writeFound)
        {
            _readFound          = false;
            _writeFound         = false;
            _subscribingTimeout = 1.0f;
        }

        if (_subscribingTimeout > 0f) //讀資料
        {
            _subscribingTimeout -= Time.deltaTime;
            if (_subscribingTimeout <= 0f)
            {
                txtDebug.text      += FullUUID(_readCharacteristicUUID);
                txtDebug.text      += "\r\nrecieve : ";
                _subscribingTimeout = 0f;
                BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(
                    _connectedID, FullUUID(_serviceUUID), FullUUID(_readCharacteristicUUID),
                    (deviceAddress, notification) => {
                },
                    (deviceAddress2, characteristic, data) => {
                    BluetoothLEHardwareInterface.Log("id: " + _connectedID);
                    if (deviceAddress2.CompareTo(_connectedID) == 0)
                    {
                        BTStatus = true;

                        BluetoothLEHardwareInterface.Log(string.Format("data length: {0}", data.Length));
                        if (data.Length == 0)
                        {
                            //txtDebug.text += "readnothing";
                            // do nothing
                        }
                        else
                        {
                            string s = ASCIIEncoding.UTF8.GetString(data);    ///Byte to string
                            BluetoothLEHardwareInterface.Log("data: " + s);
                            receiveText(s);
                            //txtDebug.text += "reading";

                            //關閉顯示,如要看參數需打開
                            //txtDebug.gameObject.SetActive(false);
                            //txtReceive.gameObject.SetActive(false);
                            //txtDebug.GetComponent<MeshRenderer>().enabled =  false;
                            //txtReceive.GetComponent<MeshRenderer>().enabled = false;
                        }
                    }
                });
            }
        }
    }
Exemplo n.º 16
0
    //Receive Rotation Matrix
    public static double[,] ReceiveRotationMatrix()
    {
        double[,] rotationMatrix = new double[3, 3];
        Debug.Log("ENTROU NO RECEBER Rotation Matrix");
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBSNUUID(_serviceReadUUID), FullBSNUUID(_readRotationMatrixUUID), (deviceAddress, notification) =>
        {
        }, (deviceAddress2, characteristic, data) =>
        {
            //Receive ROLL data
            byte[] data00        = { data[0], data[1] };
            rotationMatrix[0, 0] = System.BitConverter.ToInt32(data00, 0) / 65536.0;   // 44100.0
            Debug.Log("M00 = " + rotationMatrix[0, 0]);

            //Receive PITCH data
            byte[] data01        = { data[2], data[3] };
            rotationMatrix[0, 1] = System.BitConverter.ToInt32(data01, 0) / 65536.0;   // 44100.0
            Debug.Log("M01 = " + rotationMatrix[0, 1]);

            //Receive YAW data
            byte[] data02        = { data[4], data[5] };
            rotationMatrix[0, 2] = System.BitConverter.ToInt32(data02, 0) / 65536.0;   // 44100.0
            Debug.Log("M02 = " + rotationMatrix[0, 2]);

            byte[] data10        = { data[6], data[7] };
            rotationMatrix[1, 0] = System.BitConverter.ToInt32(data10, 0) / 65536.0;   // 44100.0
            Debug.Log("M10 = " + rotationMatrix[1, 0]);

            byte[] data11        = { data[8], data[9] };
            rotationMatrix[1, 1] = System.BitConverter.ToInt32(data11, 0) / 65536.0;   // 44100.0
            Debug.Log("M11 = " + rotationMatrix[1, 1]);

            byte[] data12        = { data[10], data[11] };
            rotationMatrix[1, 2] = System.BitConverter.ToInt32(data12, 0) / 65536.0;   // 44100.0
            Debug.Log("M12 = " + rotationMatrix[1, 2]);

            byte[] data20        = { data[12], data[13] };
            rotationMatrix[2, 0] = System.BitConverter.ToInt32(data20, 0) / 65536.0;   // 44100.0
            Debug.Log("M20 = " + rotationMatrix[2, 0]);

            byte[] data21        = { data[14], data[15] };
            rotationMatrix[2, 1] = System.BitConverter.ToInt32(data21, 0) / 65536.0;   // 44100.0
            Debug.Log("M21 = " + rotationMatrix[2, 1]);

            byte[] data22        = { data[12], data[13] };
            rotationMatrix[2, 2] = System.BitConverter.ToInt32(data22, 0) / 65536.0;   // 44100.0
            Debug.Log("M22 = " + rotationMatrix[2, 2]);
        });
        Debug.Log("DEPOIS DO READ");
        return(rotationMatrix);
    }
Exemplo n.º 17
0
 IEnumerator SubscribeSensor4()
 {
     connectedAmount++;
     Debug.Log("Connected amount is:" + connectedAmount);
     Sup.text = "Subscribing to sensor 4...";
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(Address4, _serviceUUID, _characteristicUUID, (deviceAddress, notification) =>
     {
     }, (deviceAddress2, characteristic, data) =>
     {
         Sup.text         = "Subscribed to sensor 4.";
         mLockQuaternionR = handleMessage_PremiusLockAngle(data);
     });
     yield return(null);
 }
Exemplo n.º 18
0
 public void OnClick()
 {
     // is_busy_global = true;
     Debug.Log("TOAN101: CLicking " + serviceUUID + " " + characteristicUUID);
     if (BLEManager.I.IsEqual(characteristicUUID, "2a9d") ||
         BLEManager.I.IsEqual(characteristicUUID, "2a03") ||
         BLEManager.I.IsEqual(characteristicUUID, "2a05")
         )
     {
         BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(address, serviceUUID, characteristicUUID,
                                                                               (noti, noti2) =>
         {
             is_get_info = true;
             // is_busy_global = false;
             Debug.Log("TOAN5555: SubscribeCharacteristicWithDeviceAddress to " + address);
             BLEManager.I.MyLog("TOAN5555: SubscribeCharacteristicWithDeviceAddress to " + address);
         },
                                                                               (address, characteristicUUID, bytes) =>
         {
             Debug.Log("TOAN5555: SubscribeCharacteristicWithDeviceAddress to " + address);
             BLEManager.I.MyLog("TOAN5555: SubscribeCharacteristicWithDeviceAddress to " + address);
             string data = "";
             foreach (var b in bytes)
             {
                 data += b.ToString("X") + " ";
             }
             TEXT2.text  = data;
             TEXT1.text += " ***** ";
             is_get_info = true;
             //  is_busy_global = false;
         }
                                                                               );
     }
     else
     {
         BluetoothLEHardwareInterface.ReadCharacteristic(address, serviceUUID, characteristicUUID, (characteristicUUID2, bytes) =>
         {
             Debug.Log("TOAN189: ReadCharacteristic to " + characteristicUUID2 + " : " + "OK");
             BLEManager.I.MyLog("TOAN189: ReadCharacteristic to " + characteristicUUID2 + " : " + "OK");
             string data = "";
             foreach (var b in bytes)
             {
                 data += b.ToString("X") + " ";
             }
             TEXT2.text  = data;
             is_get_info = true;
             // is_busy_global = false;
         });
     }
 }
Exemplo n.º 19
0
 // Suscribe and send secure auth service token
 void connectSegureService()
 {
     BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(
         _connectedID,
         SecureService,
         SecureServiceConnection,
         (deviceAddress, notification) => {
         if (isFirstSecure)
         {
             isFirstSecure = false;
             // Send secure auth service token
             byte[] value = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
             SendByte(
                 value,
                 SecureService,
                 SecureServiceConnection,
                 (action2) => {
                 sendConnection = false;
                 // Notify that Urband is connected
                 urbanConnected = true;
                 // Se envia una peticion para que la urband vibre y confirmar la conneccion
                 MakeUrbandRumble(
                     0,
                     50,
                     100,
                     100,
                     0,
                     10,
                     10,
                     0,
                     5,
                     "FF",
                     "FF",
                     "FF",
                     2
                     );
                 // Suscribe to gesture service again
                 firstConnection();
             },
                 (error) => {
                 isFirstSecure = true;
                 connectSegureService();
             }
                 );
         }
     },
         (deviceAddress2, characteristic, data) => {
     }
         );
 }
Exemplo n.º 20
0
 void Update()
 {
     if (_readFound && _writeFound)
     {
         _readFound          = false;
         _writeFound         = false;
         _subscribingTimeout = 1.0f;//超时设定
         Debug.Log("已成功连接......");
     }
     //BluetoothLEHardwareInterface.DisconnectPeripheral()
     if (_subscribingTimeout > 0f)
     {
         _subscribingTimeout -= Time.deltaTime;
         if (_subscribingTimeout <= 0f)
         {
             _subscribingTimeout = 0f;
             //订阅设备
             BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(
                 _connectedID, FullUUID(_serviceUUID), FullUUID(_readCharacteristicUUID),
                 (deviceAddress, notification) =>
             {
                 // Debug.Log("订阅通知: " + deviceAddress + "    >>   " + notification);
             },
                 (deviceAddress2, characteristic, data) =>
             {
                 //Debug.Log("订阅特征改变:  " + deviceAddress2 + "  >>  " + characteristic + "  >> " + ASCIIEncoding.UTF8.GetString(data));
                 //Debug.Log(">>>>>>id: " + _connectedID);
                 if (deviceAddress2.CompareTo(_connectedID) == 0)
                 {
                     //Debug.Log(string.Format(">>>>>>data length: {0}", data.Length));
                     if (data.Length == 0)
                     {
                         // do nothing
                         Debug.Log("The data length is 0");
                     }
                     else
                     {
                         //OnReceive(data);
                         //string s = ASCIIEncoding.UTF8.GetString(data);
                         //Debug.Log("Current data string:  " + s);
                         UnpackDispatcher(data);
                         //receiveText(s);
                     }
                 }
             });
         }
     }
 }
Exemplo n.º 21
0
    void ReceiveNotification()
    {
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(m_DeviceAddress, ServiceUUID, GENERAL_PURPOSE_NOTIFICATION, null, (address, characteristicUUID, bytes) => {
            if (bytes != null)
            {
                /*string data = "";
                 * foreach (var b in bytes)
                 *  data += b.ToString("X") + " ";
                 *
                 * DebugLog(data);*/
                m_ReceiveCallback(Hot2gEnumerations.EnumHot2gCommBtMsgEvent.Notified, bytes);
            }

            //SetMode(eMode.WaitData);
        });
    }
Exemplo n.º 22
0
    void StartSubscribe()
    {
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(deviceAddress, ServiceUUID, CharacteristicUUID, null, (address, characteristicUUID, bytes) => {
            Debug.Log("StartSubscribe : Recive data ");
            if (curretnRequest != null)
            {
                curretnRequest.addResponce(bytes);
            }
            else
            {
                EventDispatcher.SendEvent(eBT_DATA_RECIVED, bytes);
            }
        });

        SetState(BtStates.Connected, 2f);
    }
Exemplo n.º 23
0
 public void ConnectToDevice(Device device)
 {
     if (!_connecting)
     {
         if (Connected)
         {
             DisconnectDevice(_connectedDevice);
         }
         else
         {
             BluetoothLEHardwareInterface.Log("Connecting to peripheral");
             _connecting = true;
             BluetoothLEHardwareInterface.ConnectToPeripheral(device.address, (address) => {
             },
                                                              (address, serviceUUID) => {
             },
                                                              (address, serviceUUID, characteristicUUID) => {
                 BluetoothLEHardwareInterface.Log("uuidFound: " + serviceUUID + " uuidExpected: " + _heartRateServiceUUID);
                 // discovered characteristic
                 if (IsEqual(serviceUUID, _heartRateServiceUUID))
                 {
                     _connectedID     = address;
                     _connectedDevice = new HRMDevice(device.name, device.address);
                     Connected        = true;
                     BluetoothLEHardwareInterface.Log("^^^Connected to peripheral");
                     if (IsEqual(characteristicUUID, _readHRCharacteristicUUID))
                     {
                         BluetoothLEHardwareInterface.Log("Subscribing to HR characteristic");
                         BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_connectedID, _heartRateServiceUUID, _readHRCharacteristicUUID, (deviceAddress, notification) => {
                         }, (deviceAddress2, characteristic, data) => {
                             OnNewHRMeasurement(deviceAddress2, characteristic, data);
                         });
                     }
                 }
             }, (address) => {
                 // this will get called when the device disconnects
                 // be aware that this will also get called when the disconnect
                 // is called above. both methods get call for the same action
                 // this is for backwards compatibility
                 Connected = false;
             });
             _connecting = false;
         }
     }
 }
Exemplo n.º 24
0
    private void SubscribeToTargetDevice()
    {
        BluetoothLEHardwareInterface.Log("Subscribing to characteristics...");

        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(deviceAddress, ServiceUUID, TXUUID, null,
                                                                              (address, characteristicUUID, bytes) =>
        {
            BluetoothLEHardwareInterface.Log("Waiting for user action (2)...");
            connected = true;

            if (state != States.None)
            {
                state = States.None;
            }

            // we received some data from the puck
            ProcessButton(bytes);
        });
    }
 public void GetAllCharacteristics(string address, string serviceUUID, string characteristicUUID)
 {
     if (IsEqual(characteristicUUID, "2a9d") ||
         IsEqual(characteristicUUID, "2a03") ||
         IsEqual(characteristicUUID, "2a05")
         )
     {
         BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(address, serviceUUID, characteristicUUID,
                                                                               (noti, noti2) =>
         {
             is_get_info = true;
             Debug.Log("TOAN5555: SubscribeCharacteristicWithDeviceAddress to " + address);
         },
                                                                               (address2, characteristicUUID2, bytes) =>
         {
             Debug.Log("TOAN5555: SubscribeCharacteristicWithDeviceAddress to " + address2);
             string data = "";
             foreach (var b in bytes)
             {
                 data += b.ToString("X") + " ";
             }
             DICT_OBTAINED.Add(characteristicUUID, new characteristicUUIDInfo(address, serviceUUID, characteristicUUID, data, bytes));
             is_get_info = true;
         }
                                                                               );
     }
     else
     {
         BluetoothLEHardwareInterface.ReadCharacteristic(address, serviceUUID, characteristicUUID, (characteristicUUID2, bytes) =>
         {
             Debug.Log("TOAN189: ReadCharacteristic to " + characteristicUUID2 + " : " + "OK");
             string data = "";
             foreach (var b in bytes)
             {
                 data += b.ToString("X") + " ";
             }
             DICT_OBTAINED.Add(characteristicUUID, new characteristicUUIDInfo(address, serviceUUID, characteristicUUID, data, bytes));
             is_get_info = true;
         });
     }
 }
Exemplo n.º 26
0
    // the Unity MonoBehaviour Update() mathod
    void Update()
    {
        if (_readFound && _writeFound)
        {
            _readFound          = false;
            _writeFound         = false;
            _subscribingTimeout = 3.0f;
        }

        if (_subscribingTimeout > 0.0f)
        {
            _subscribingTimeout -= Time.deltaTime;
            if (_subscribingTimeout <= 0.0f)
            {
                _subscribingTimeout = 0.0f;
                BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(
                    _connectedID, FullUUID(_serviceUUID), FullUUID(RX_UUID),
                    (deviceAddress, notification) => {
                },
                    (deviceAddress2, characteristic, data) => {
                    BluetoothLEHardwareInterface.Log("id: " + _connectedID);
                    if (deviceAddress2.CompareTo(_connectedID) == 0)
                    {
                        BluetoothLEHardwareInterface.Log(string.Format("data length: {0}", data.Length));
                        if (data.Length == 0)
                        {
                            // do nothing
                        }
                        else
                        {
                            //Data
                            string s = ASCIIEncoding.UTF8.GetString(data);
                            BluetoothLEHardwareInterface.Log("data: " + s);
                            receiveText(s);
                        }
                    }
                });
            }
        }
    }
Exemplo n.º 27
0
    // Update is called once per frame
    void Update()
    {
        if (_readFound && _writeFound)
        {
            _readFound  = false;
            _writeFound = false;

            _subscribingTimeout = 1f;
        }

        if (_subscribingTimeout > 0f)
        {
            _subscribingTimeout -= Time.deltaTime;
            if (_subscribingTimeout <= 0f)
            {
                _subscribingTimeout = 0f;

                BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_connectedID, FullUUID(_serviceUUID), FullUUID(_readCharacteristicUUID), (deviceAddress, notification) => {
                }, (deviceAddress2, characteristic, data) => {
                    BluetoothLEHardwareInterface.Log("id: " + _connectedID);
                    if (deviceAddress2.CompareTo(_connectedID) == 0)
                    {
                        BluetoothLEHardwareInterface.Log(string.Format("data length: {0}", data.Length));
                        if (data.Length == 0)
                        {
                        }
                        else
                        {
                            string s = ASCIIEncoding.UTF8.GetString(data);
                            BluetoothLEHardwareInterface.Log("data: " + s);
                            Receive.text += s;
                        }
                    }
                });

                SendButton.SetActive(true);
            }
        }
    }
Exemplo n.º 28
0
    public override void UpdateSpiroController()
    {
        if (_readFound && _writeFound)
        {
            _readFound          = false;
            _writeFound         = false;
            _subscribingTimeout = 1.0f;
        }

        if (_subscribingTimeout > 0f)
        {
            _subscribingTimeout -= Time.deltaTime;
            if (_subscribingTimeout <= 0f)
            {
                _subscribingTimeout = 0f;
                BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(
                    _connectedID, _serviceUUID, _readCharacteristicUUID,
                    (deviceAddress, notification) =>
                {
                },
                    (deviceAddress2, characteristic, data) =>
                {
                    isConnected = true;
                    BluetoothLEHardwareInterface.Log("id: " + _connectedID);
                    if (deviceAddress2.CompareTo(_connectedID) == 0)
                    {
                        BluetoothLEHardwareInterface.Log(string.Format("data length: {0}", data.Length));
                        if (data.Length > 0)
                        {
                            double flowMLSec = BitConverter.ToInt16(data, 1);   //raw data represents airflow in milliliters per second
                            flowLSec         = flowMLSec / 1000;                //converted to flow in liters per second
                            DeviceManager.Instance.FlowLMin = -(flowLSec * 60); //converted to flow in liters per minute
                        }
                    }
                });
            }
        }
    }
Exemplo n.º 29
0
    //Receive Quaternions
    public static double[] ReceiveQuaternions()
    {
        double[] quaternions = new double[4];
        Debug.Log("ENTROU NO RECEBER QUATERNOS");
        Debug.Log("ANTES DO READ QUATERNOS");
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBSNUUID(_serviceReadUUID), FullBSNUUID(_readQuaternionUUID), (deviceAddress, notification) =>
        {
        }, (deviceAddress2, characteristic, data) =>
        {
            //Receive W data
            byte[] dataW   = { data[0], data[1], data[2], data[3] };
            quaternions[0] = System.BitConverter.ToInt32(dataW, 0) / 65536.0;   // 44100.0
            // Debug.Log("W = " + quaternions[0]);

            //Receive X data
            byte[] dataX   = { data[4], data[5], data[6], data[7] };
            quaternions[1] = System.BitConverter.ToInt32(dataX, 0) / 65536.0;   // 44100.0
            // Debug.Log("X = " + quaternions[1]);

            //Receive Y data
            byte[] dataY   = { data[8], data[9], data[10], data[11] };
            quaternions[2] = System.BitConverter.ToInt32(dataY, 0) / 65536.0;   // 44100.0
            // Debug.Log("Y = " + quaternions[2]);

            //Receive Z data
            byte[] dataZ   = { data[12], data[13], data[14], data[15] };
            quaternions[3] = System.BitConverter.ToInt32(dataZ, 0) / 65536.0;   // 44100.0
            //Debug.Log("Z = " + quaternions[3]);

            // Debug.Log("ENVIOU BYTES : " + data);
            // Debug.Log("ENVIOU BYTES STRING : " + Encoding.ASCII.GetString(data));
            // Debug.Log("ENVIOU BYTES STRING : " + Encoding.Default.GetString(data));
            // Debug.Log("ENVIOU BYTES Lenght : " + data.Length);
        });
        Debug.Log("DEPOIS DO READ QUATERNOS");

        return(quaternions);
    }
Exemplo n.º 30
0
    public static double[] ReceiveRawDataTest()
    {
        double[] rawData = new double[3];
        Debug.Log("ENTROU NO RECEBER RawDatatest");
        BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(bsnDevice.Address, FullBSNUUID(_serviceReadUUID), FullBSNUUID(_readRawDataUUID), (deviceAddress, notification) =>
        {
        }, (deviceAddress2, characteristic, data) =>
        {
            byte[] dataX = { data[0], data[1] };
            rawData[0]   = (System.BitConverter.ToInt16(dataX, 0) / 65536.0) * 9.8; // 44100.0
            //Debug.Log("X = " + rawData[0]);

            //Receive Y data
            byte[] dataY = { data[2], data[3] };
            rawData[1]   = (System.BitConverter.ToInt16(dataY, 0) / 65536.0) * 9.8; // 44100.0
            //Debug.Log("Y = " + rawData[1]);

            //Receive Z data
            byte[] dataZ = { data[4], data[5] };
            rawData[2]   = (System.BitConverter.ToInt16(dataZ, 0) / 65536.0) * 9.8; // 44100.0
            //Debug.Log("Z = " + rawData[2]);

            // //RECEIVE ACELEROMETER DATA
            // //Receive X data
            // rawData[0] = (data[1] * 256) + data[0];
            // Debug.Log("X = " + rawData[0]);

            // //Receive Y data
            // rawData[1] = (data[3] * 256) + data[2];
            // Debug.Log("Y = " + rawData[1]);

            // //Receive Z data
            // rawData[2] = (data[5] * 256) + data[4];
            // Debug.Log("Z = " + rawData[2]);
        });
        Debug.Log("FIM DO RECEBER");
        return(rawData);
    }