最近看了不少關(guān)于ios藍(lán)牙語音通信的文章,網(wǎng)上錯誤不少。最近自己寫了個小Demo。
10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有南山免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
具體的每個函數(shù)干什么的請自行查詢。都是Gamekit框架的api。
代碼實現(xiàn)如下
- (void)viewDidLoad
{
[superviewDidLoad];
CGSize size=[[UIScreen mainScreen] bounds].size;
UIButton *muteBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[muteBtnsetTitle:@"聊天中"forState:UIControlStateHighlighted];
[muteBtnsetTitle:@"聊天"forState:UIControlStateNormal];
[muteBtn setFrame:CGRectMake(size.width/2.0-140.0, 20, 280, 280)];
[muteBtnaddTarget:selfaction:@selector(muteAction:)forControlEvents:UIControlEventTouchDown];
[muteBtnaddTarget:selfaction:@selector(UnmuteAction:)forControlEvents:UIControlEventTouchUpInside];
UIButton *connectBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[connectBtnsetTitle:@"連接"forState:UIControlStateNormal];
[connectBtn setFrame:CGRectMake(size.width/2.0-50.0, 320, 100, 40)];
[connectBtnaddTarget:selfaction:@selector(connectAction:)forControlEvents:UIControlEventTouchUpInside];
UIButton *disConnectBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[disConnectBtn setTitle:@"斷開連接" forState:UIControlStateNormal];
[disConnectBtn setFrame:CGRectMake(size.width/2.0-50.0, 380, 100, 40)];
[disConnectBtnaddTarget:selfaction:@selector(disConnectAction:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:muteBtn];
[self.view addSubview:connectBtn];
[self.view addSubview:disConnectBtn];
}
-(void)connectAction:(id)sender{
if (!picker) {
picker=[[GKPeerPickerControlleralloc]init];
picker.connectionTypesMask=GKPeerPickerConnectionTypeNearby;
picker.delegate=self;
}
if (!isConnect) {
[picker show];
}
}
-(GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type{
if (!_session) {
_session=[[GKSessionalloc]initWithSessionID:(_sessionID ?_sessionID :@"Sample Session")displayName:nilsessionMode:GKSessionModePeer];
_session.delegate=self;
}
return_session;
}
-(void)peerPickerController:(GKPeerPickerController *)picker1 didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
[picker dismiss];
isConnect=YES;
[_sessionsetDataReceiveHandler:selfwithContext:nil];
NSError *error;
AVAudioSession *audioSession=[AVAudioSessionsharedInstance];
if (![audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:&error]) {
NSLog(@"設(shè)置播放記錄錯誤:%@",[errorlocalizedDescription]);
return;
}
if (![audioSession setActive:YES error:&error]) {
NSLog(@"激活失敗:%@",[errorlocalizedDescription]);
return;
}
[GKVoiceChatServicedefaultVoiceChatService].client=self;
if (![[GKVoiceChatServicedefaultVoiceChatService]startVoiceChatWithParticipantID:peerIDerror:&error]) {
NSLog(@"開啟語音失敗 :%@",[error userInfo]);
}
}
-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker1{
picker.delegate=nil;
picker=nil;
}
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
if (state==GKPeerStateConnected) {
isConnect=YES;
}
if (state==GKPeerStateDisconnected) {
[[GKVoiceChatServicedefaultVoiceChatService]stopVoiceChatWithParticipantID:peerID];
//[self disConnectAction:nil];
}
}
-(void)disConnectAction:(id)sender{
[_sessiondisconnectFromAllPeers];
_session.available=NO;
_session.delegate=nil;
[_sessionsetDataReceiveHandler:nilwithContext:nil];
_session=nil;
isConnect=NO;
}
-(void)muteAction:(id)sender{
[GKVoiceChatServicedefaultVoiceChatService].microphoneMuted=YES;
}
-(void)UnmuteAction:(id)sender{
[GKVoiceChatServicedefaultVoiceChatService].microphoneMuted=NO;
}
-(NSString *)participantID{
return_session.peerID;
}
-(void)voiceChatService:(GKVoiceChatService *)voiceChatService sendData:(NSData *)data toParticipantID:(NSString *)participantID{
[_sessionsendData:datatoPeers:[NSArrayarrayWithObject:participantID]withDataMode:GKSendDataReliableerror:nil];
}
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
[[GKVoiceChatServicedefaultVoiceChatService]receivedData:datafromParticipantID:peer];
}
加紅的方法大家注意下。第一個加紅的方法如果不寫可能也能實現(xiàn),但會有一些內(nèi)存上的錯誤。關(guān)閉藍(lán)牙連接不是簡單的 [_session disconnectFromAllPeers];就行的 要像第二個紅色方法里那寫全才能真正正確關(guān)閉。(水平有限歡迎拍磚)。源碼至http://down.51cto.com/data/703329下載。或聯(lián)系478043385@qq.com免費索取。