Collecting combined data at the same time from the multiple sensors connected

edited August 2016 in iOS
Hello,

I have written the code for collecting combined data at the same from the 2 sensors connected to the iOS device. Here is the following code:



override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated);

        

        device1.addObserver(self, forKeyPath: "state", options: NSKeyValueObservingOptions.New, context: nil)

        device1.connectWithHandler { error in

            

        }

        

        device2.addObserver(self, forKeyPath: "state", options: NSKeyValueObservingOptions.New, context: nil)

        device2.connectWithHandler { error in

           

        }


But I am unable to connect 2 Metawear sensors at the same time and then getting the data from them.


Please guide.

Comments

  • What you have written should work, there is nothing wrong with calling connectWithHandler on multiple devices.

    Can double check that the device1 and device2 objects are non-null when this code is executed.  
    Do you receive a callback with error or does it just never fire?
    Also, what do you have in your observeValueForKeyPath function? 
  • I'm getting non-null value for device1, but getting nil value for device2.

    observeValueForKeyPath function -

    switch (self.device1.state) {

                case .Connected:

                    self.deviceStatus.text = "Connected";

                case .Connecting:

                    self.deviceStatus.text = "Connecting";

                .

    .

    .

                }

    Similarly, switch case for device2.

  • The observeValueForKeyPath function looks good, just making sure it wan't issuing connect/disconnect requests.

    You will have a trace backwards and see why device2 is nil.  Once that is solved, you should have no problems.
  • I am trying to figure out why device2 is nil for few days. But, I'm unable to do so. What could be the reason?
    What I am doing is in the application, I'm selecting the Device1 in 'MainTableViewController' which takes me to 'DeviceViewController'. After this, I'm going back to the 'MainTableViewController' using back button and then selecting the Device2 and as soon as I select Device2, my application is crashing. Is this the correct way selecting multiple sensors?
  • Are you only storing references to the devices on the view controller?  Since you don't control the lifetime of the view controllers you can't rely on them to keep your data when you switch between them (they will be deleted by iOS).

    Instead you should create some other global object that you control and store the devices their instead.


This discussion has been closed.