その後のその後

iOSエンジニア 堤 修一のブログ github.com/shu223

iPhoneバイブの制御

にゃらんの肉球なにょだ』という目覚まし機能付き肉球アプリ(無料)をつくってたときのメモより。




まずバイブを鳴らす(動作させる)方法は


#import
した上で、

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);


いろいろ調べたけどこれしかないっぽい。


で、これだと一度ブルッとなって終わるので、
アラームのように繰り返し再生するには、

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

#pragma mark AudioService callback function implementation

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);
  }  
}


(参考ページ)
http://stackoverflow.com/questions/2718837/how-to-run-vibrate-continuously-in-iphone



まだ問題がある。
これだとバイブの間隔がなく連続でブルブルなってしまうので、
繰り返し間隔を空けるには次のようにする。

void MyAudioServicesSystemSoundCompletionProc (
   SystemSoundID  ssID,
   void           *clientData
) {
  if (iShouldKeepBuzzing) { // Your logic here...
      [NSThread sleepForTimeInterval:(NSTimeInterval)interval];
      AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 
  } else {
      //Unregister, so we don't get called again...
      AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
  }  
}


(参考ページ)
http://stackoverflow.com/questions/1490028/iphone-dev-performselectorwithobjectafterdelay-or-nstimer