Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I determine / how, if a device has vibration or not?

I have some settings that enable/disable vibration for certain actions, but I find it pointless to display them if the device doesn't have the ability to vibrate. Is there a way to check if the person is using an iPod touch and if it has vibration?

like image 924
Alex Gosselin Avatar asked Aug 14 '11 02:08

Alex Gosselin


2 Answers

I'm not sure there is a way to do this other than doing model checks which is probably not a great approach. I do know that apple provides:

 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

If the device can vibrate, it will. On devices without vibration, it will do nothing. There is another call:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

This one will vibrate the device if it hash the capability or the device will beep.

It might be better to just have the settings and have some explanation around the setting because a user may want the beep when they do not have a vibrating device. Maybe call the setting something other than "Vibration Alert On/Off".

like image 73
Jake Dempsey Avatar answered Sep 24 '22 12:09

Jake Dempsey


This code should do it - be aware it 'assumes' the iPhone is the only device with Vibration capability. Which it is for the moment...

- (NSString *)machine
{
    static NSString *machine = nil;

    // we keep name around (its like 10 bytes....) forever to stop lots of little mallocs;
    if(machine == nil)
    {
        char * name = nil;
        size_t size;

        // Set 'oldp' parameter to NULL to get the size of the data
        // returned so we can allocate appropriate amount of space
        sysctlbyname("hw.machine", NULL, &size, NULL, 0); 

        // Allocate the space to store name
        name = malloc(size);

        // Get the platform name
        sysctlbyname("hw.machine", name, &size, NULL, 0);

        // Place name into a string
        machine = [[NSString stringWithUTF8String:name] retain];
        // Done with this
        free(name);
    }

    return machine;
}

-(BOOL)hasVibration
{
    NSString * machine = [self machine];

    if([[machine uppercaseString] rangeOfString:@"IPHONE"].location != NSNotFound)
    {
        return YES;
    }

    return NO;
}

Just edited to stop the machine call from doing lots of small mallocs each time its called.

like image 20
Tony Million Avatar answered Sep 24 '22 12:09

Tony Million