I recently began developing a new iPhone application for work that will be used to treat various acupuncture points using sound, vibration and pressure. Sound and pressure are no-brainers, as sound is easily used via the exposed libraries in the iPhone SDK, but vibration is turning out to not be so easy.
Apple has only provided a single API for interacting with the vibrate feature of their iPhone OS devices. It looks like this:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
That’s it.
The astute among you may notice that there is no way to adjust things like the duration of the vibration, the frequency or amplitude (i.e., a fast or slow buzz, a”heavy” or “light” buzz, etc). In fact, the above API call will produce exactly 0.4 seconds of vibration and 0.1 seconds of silence.
In order to have a decent effect on an acupuncture point, the point needs to be massaged or vibrated for, say, 30 seconds at a time, so I set out to find a way to make it vibrate continuously for 30 seconds. After asking around on IRC and google, all I heard was “you can’t do that, so stop trying.” Unsatisfied with this, I came up with the idea of calling the vibrate function repeatedly such that each subsequent call fires just before the prior one enters it’s 0.1 seconds of silence. To do this, I hacked up the following:
for (int i = 1; i < 60; i++)
{
[self performSelector:@selector(vibe:) withObject:self afterDelay:i *.3f];
}
And the selector that it performs:
-(void)vibe:(id)sender
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
(note that in the above code I was not going for precisely 30 seconds – just a continuous buzz to see if it could be done.)
The first time I ran this, it actually worked. I was shocked. However, the above is a pretty nasty hack, kludge, and abuse of the API that Apple provides, and I’m pretty sure that if I tried to get an app approved with something like the above they’d smack me around some.
So, while it appears that you can beat up on the API enough to make the device vibrate continuously, I don’t recommend that you do.
Update: I was recently asked if I could provide a demo XCode project that incorporated the above code snippets. Here it is:
Another update: Apple has removed this API call, and replaced it with:
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
Leave a Reply