最近看了不少关于ios蓝牙语音通信的文章,网上错误不少。最近自己写了个小Demo。
具体的每个函数干什么的请自行查询。都是Gamekit框架的api。
代码实现如下
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize size=[[UIScreen mainScreen] bounds].size;
UIButton *muteBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[muteBtn setTitle:@"聊天中" forState:UIControlStateHighlighted];
[muteBtn setTitle:@"聊天" forState:UIControlStateNormal];
[muteBtn setFrame:CGRectMake(size.width/2.0-140.0, 20, 280, 280)];
[muteBtn addTarget:self action:@selector(muteAction:) forControlEvents:UIControlEventTouchDown];
[muteBtn addTarget:self action:@selector(UnmuteAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *connectBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[connectBtn setTitle:@"连接" forState:UIControlStateNormal];
[connectBtn setFrame:CGRectMake(size.width/2.0-50.0, 320, 100, 40)];
[connectBtn addTarget:self action:@selector(connectAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *disConnectBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[disConnectBtn setTitle:@"断开连接" forState:UIControlStateNormal];
[disConnectBtn setFrame:CGRectMake(size.width/2.0-50.0, 380, 100, 40)];
[disConnectBtn addTarget:self action:@selector(disConnectAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:muteBtn];
[self.view addSubview:connectBtn];
[self.view addSubview:disConnectBtn];
}
-(void)connectAction:(id)sender{
if (!picker) {
picker=[[GKPeerPickerController alloc] init];
picker.connectionTypesMask=GKPeerPickerConnectionTypeNearby;
picker.delegate=self;
}
if (!isConnect) {
[picker show];
}
}
-(GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type{
if (!_session) {
_session=[[GKSession alloc] initWithSessionID:(_sessionID ? _sessionID : @"Sample Session") displayName:nil sessionMode:GKSessionModePeer];
_session.delegate=self;
}
return _session;
}
-(void)peerPickerController:(GKPeerPickerController *)picker1 didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
[picker dismiss];
isConnect=YES;
[_session setDataReceiveHandler:self withContext:nil];
NSError *error;
AVAudioSession *audioSession=[AVAudioSession sharedInstance];
if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]) {
NSLog(@"设置播放记录错误:%@",[error localizedDescription]);
return;
}
if (![audioSession setActive:YES error:&error]) {
NSLog(@"激活失败:%@",[error localizedDescription]);
return;
}
[GKVoiceChatService defaultVoiceChatService].client=self;
if (![[GKVoiceChatService defaultVoiceChatService] startVoiceChatWithParticipantID:peerID error:&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) {
[[GKVoiceChatService defaultVoiceChatService] stopVoiceChatWithParticipantID:peerID];
//[self disConnectAction:nil];
}
}
-(void)disConnectAction:(id)sender{
[_session disconnectFromAllPeers];
_session.available=NO;
_session.delegate=nil;
[_session setDataReceiveHandler:nil withContext:nil];
_session=nil;
isConnect=NO;
}
-(void)muteAction:(id)sender{
[GKVoiceChatService defaultVoiceChatService].microphoneMuted=YES;
}
-(void)UnmuteAction:(id)sender{
[GKVoiceChatService defaultVoiceChatService].microphoneMuted=NO;
}
-(NSString *)participantID{
return _session.peerID;
}
-(void)voiceChatService:(GKVoiceChatService *)voiceChatService sendData:(NSData *)data toParticipantID:(NSString *)participantID{
[_session sendData:data toPeers:[NSArray arrayWithObject:participantID] withDataMode:GKSendDataReliable error:nil];
}
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
[[GKVoiceChatService defaultVoiceChatService] receivedData:data fromParticipantID:peer];
}
加红的方法大家注意下。第一个加红的方法如果不写可能也能实现,但会有一些内存上的错误。关闭蓝牙连接不是简单的 [_session disconnectFromAllPeers];就行的 要像第二个红色方法里那写全才能真正正确关闭。(水平有限欢迎拍砖)。源码至http://down.51cto.com/data/703329下载。或联系478043385@qq.com免费索取。