There's an old topic on triggering the iPhone vibrator, but it states there is no means of varying the duration. Does anyone know whether that can be done - so that it could just give a brief sensation?
Forum Archive
Phone vibrator
It’s possible, though it requires using private APIs (might break in future iOS updates etc.). Here’s an example, adapted from this StackOverflow answer:
from objc_util import *
from ctypes import c_ulong
def vibrate_pattern(on_off_times):
pattern = ns([])
for i, t in enumerate(on_off_times):
on = NSNumber.numberWithBool_(i % 2 == 0)
pattern.addObject_(on)
pattern.addObject_(t)
c.AudioServicesPlaySystemSoundWithVibration.argtypes = [c_ulong, c_void_p, c_void_p]
c.AudioServicesPlaySystemSoundWithVibration.restype = None
vibe_info = ns({'VibePattern': pattern, 'Intensity': 1.0})
c.AudioServicesPlaySystemSoundWithVibration(4095, None, vibe_info.ptr)
if __name__ == '__main__':
vibrate_pattern([200, 50, 200, 50, 200, 50, 800])
The on_off_times parameter should be a list of numbers, specifying a sequence of “vibrate” and “pause” durations in milliseconds.
Thank you for posting this! I just wrote a Morse code sender class that vibrates.
I also found "AudioServicesStopSystemSound" using Google. That will stop vibrations mid-stream.
Do you know if there is a way to detect when all vibrations have been played?
I found "AudioServicesAddSystemSoundCompletion" at:
https://developer.apple.com/documentation/audiotoolbox/1405244-audioservicesaddsystemsoundcompl
I have not yet figured out whether I can use that function, and if so, how to use it.
It’s possible to use AudioServicesAddSystemSoundCompletion, but I guess it would be easier to just calculate the sum of the vibration durations, and wait that amount of time.
@technoway , I have a feeling I have to learn Morse code now :) Might be the new encryption:)
Thanks omz. I ended up using the total play time to wait.
I had thought of that, but polling for a time with short sleeps, so that the wait can be interrupted, is probably less efficient than a notification, although admittedly only slightly so. It certainly is a lot simpler.