Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run vibrate continuously in iphone?

Tags:

iphone

In my application I'm using following coding pattern to vibrate my iPhone device

Include: AudioToolbox framework

Header File:

#import "AudioToolbox/AudioServices.h"

Code:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  

My problem is that when I run my application it gets vibrate but only for second but I want that it will vibrate continuously until I will stop it.

How could it be possible?

like image 315
AmanGupta007 Avatar asked Apr 27 '10 04:04

AmanGupta007


2 Answers

Thankfully, it's not possible to change the duration of the vibration. The only way to trigger the vibration is to play the kSystemSoundID_Vibrate as you have. If you really want to though, what you can do is to repeat the vibration indefinitely, resulting in a pulsing vibration effect instead of a long continuous one. To do this, you need to register a callback function that will get called when the vibration sound that you play is complete:

 AudioServicesAddSystemSoundCompletion (
        kSystemSoundID_Vibrate,
        NULL,
        NULL,
        MyAudioServicesSystemSoundCompletionProc,
        NULL
    );
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Then you define your callback function to replay the vibrate sound again:

#pragma mark AudioService callback function prototypes
void MyAudioServicesSystemSoundCompletionProc (
   SystemSoundID  ssID,
   void           *clientData
);

#pragma mark AudioService callback function implementation

// Callback that gets called after we finish buzzing, so we 
// can buzz a second time.
void MyAudioServicesSystemSoundCompletionProc (
   SystemSoundID  ssID,
   void           *clientData
) {
  if (iShouldKeepBuzzing) { // Your logic here...
      AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 
  } else {
      //Unregister, so we don't get called again...
      AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
  }  
}
like image 162
Jason Jenkins Avatar answered Sep 23 '22 14:09

Jason Jenkins


There are numerous examples that show how to do this with a private CoreTelephony call: _CTServerConnectionSetVibratorState, but it's really not a sensible course of action since your app will get rejected for abusing the vibrate feature like that. Just don't do it.

like image 21
warrenm Avatar answered Sep 19 '22 14:09

warrenm