unable to connect to some devices but not others

I've written a function to connect to a list of devices that have been selected in a Picker, but it seems that two devices (out of seven) are not responding to the code. As far as I can tell, there is no difference between the way the code is running between the devices. Is there anything wrong with the code below? I basically copied and pasted the connection block from the iOS doc...






- (IBAction)connectToDevices:(id)sender {

    NSArray *indexPathArray = [_selectDevicesTable indexPathsForSelectedRows];

    NSMutableArray *selectedDeviceIdentifiers = [[NSMutableArray alloc] init];

    [connectedDevices removeAllObjects];

    

    // Get the devices that have been selected for connection.

    NSLog(@Devices selected for connection are at following rows:);

    for (NSIndexPath *i in indexPathArray) {

        NSLog(@%d,(int)i.row);

        [selectedDeviceIdentifiers addObject: [deviceIdentifiers objectAtIndex:i.row]];

    }

    // Only connected to selected devices.

    [manager retrieveSavedMetaWearsWithHandler:^(NSArray *array) {

        for (MBLMetaWear *currdevice in array) {

            // Connect to the device first.

            // connectWithTimeout:handler: is a simple way to limit the amount of

            // time spent searching for the device

            if ([selectedDeviceIdentifiers containsObject:currdevice.name]) {

                [currdevice connectWithTimeout:20 handler:^(NSError *error) {

                    if ([error.domain isEqualToString:kMBLErrorDomain] &&

                        error.code == kMBLErrorConnectionTimeout) {

                        [currdevice forgetDevice];

                        NSLog(@Connection Timeout);

                    }

                    else {

                        NSLog(@Connection succeeded with %@.",currdevice.name);

                        [currdevice.led flashLEDColorAsync:[UIColor greenColor] withIntensity:1.0 numberOfFlashes:2];

                        [connectedDevices addObject:currdevice.name];

                    }

                }];

            }//endif

        }

    }];

}

Comments

  • I'm working with an iPhone 5S with the Metawear sample iOS. 

    All the metawears have been updated to the latest firmware (as is the iPhone). I don't have the metawears on me, but I will post that information as soon as I can. 

    I don't have the latest SDK. I downloaded it on 2016/1/20.

    Please let me know if there's any other information that would help!
  • The hardware version is 0.3 and the firmware 1.1.3.
  • I'm wondering whats going on this line:
    if ([selectedDeviceIdentifiers containsObject:currdevice.name]) {

    Are you storing device identifiers or names?  The [MBLMetaWear name] is always "MetaWear" by default, whereas the [MBLMetaWear identifier] is a device unique ID (this is what I think you want)

  • I want all the Metawears to have the same name across different iphones, so I give it each one a permanent name. I store a list of these names when I connect to them. I've checked that the list corresponds to the devices I want to connect to.
  • Okay then, can you expand on what you mean by "not responding to the code"?  Are they timing out in the connection or they don't show up in the saved MetaWear list?

    Using retrieveSavedMetaWearsWithHandler: will only give devices you have previously called rememberDevice on, and since you call forgetDevice in the connection timeout block.  Is there somewhere else where you call rememberDevice?
  • Right. I loop through all the devices that I can discover and remember them. I also keep track of their names in the same loop.







    - (IBAction)startSearch:(id)sender {

        [self disable_button:_connectDevicesButton];

        [self disable_button:_refreshListButton];


        //Search for devices excluding duplicates.

        //NOTE: Might be a good idea to exclude ones with weak signals.

        [manager startScanForMetaWearsAllowDuplicates:NO handler:^(NSArray *listOfDevices) {

            int i=0;

            

            for (MBLMetaWear *foundDevice in listOfDevices) {

                if ([deviceIdentifiers indexOfObject:foundDevice.name]==NSNotFound) {

                    [foundDevice rememberDevice];

                    [deviceIdentifiers addObject: foundDevice.name];

                    i++;

                }

                [self enable_button:_connectDevicesButton];

                [self enable_button:_refreshListButton];

            }        

            // List found metaWears.

            NSLog(@MetaWears found:);

            NSLog(@%@",[deviceIdentifiers componentsJoinedByString:@\n]);

            [_selectDevicesTable reloadData];

        }];

    }


    The devices that "don't respond" actually are discoverable but I get a connection timeout when I try to connect to them. This doesn't happen with all the other devices I'm trying to connect to.

  • Hmm, what happens if you increase the timeout?  There are some limitations on how many devices you can connect to simultaneously, and performance starts to drop as the number increases, so it may take longer to connect. 
This discussion has been closed.