通讯
iOS设备
之间同一个应用
内连接iOS7
开始过期了最基本的
蓝牙通讯框架 // 初始化链接蓝牙控制器
GKPeerPickerController *peerCtr = [[GKPeerPickerController alloc]init];
// 显示匹配到的蓝牙设备
[peerCtr show];
GKPeerPickerController
代理/* * 连接蓝牙的方式 附近 在线 */
- (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type {
}
// 连接会话的方式 附近 在线
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {
return nil;
}
/* 连接成功 * peerID 连接成功的设备id * session 当前回话 只需要保存当前的会话 即 可 数据传递 */
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session {
// 隐藏选择器
[picker dismiss];
// 接收数据的回调 GameKIt 必须实现的
[session setDataReceiveHandler:self withContext:nil];
// 保存会话
self.session = session;
}
/* * 退出 */
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
}
// 只要有数据回来 那么就会调用
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
}
// 发送图片
- (IBAction)sendImage {
// 拿到需要发送出去的图片
UIImage *image = self.showImageView.image;
// 将图片转换成NSData类型
NSData *imgData = UIImagePNGRepresentation(image);
/** * 发送数据给所有匹配上的用户 * * @param GKSendDataMode 数据发送的模式:(安全/不安全模式) * GKSendDataUnreliable : 不安全模式:就像发10个传单,传单直接往人群中砸过去,能不能收到不管 * GKSendDataReliable:安全模式:就像发10个传单,每一个传单都得发到路人的手上,才再发下一个传单 * @return */
[self.m_Session sendDataToAllPeers:imgData withDataMode:GKSendDataUnreliable error:nil];
}
// 若执行下面方法,必须设置
// 接收数据的回调 GameKIt 必须实现的
// [session setDataReceiveHandler:self withContext:nil];
/** 监听传递过来的数据 * setDataReceiveHandler: 由哪个对象来监听数据的接受 * withContext : 监听需要传递的参数 */
[session setDataReceiveHandler:self withContext:nil];
/** 实现监听方法 * 只设置由谁监听传递过来的数据还是不足的, * 因为我们还是不能拿到传递过来的数据,进入监听方法的头文件可以看到 * SEL = -receiveData:fromPeer:inSession:context: * 所以我们必须实现这个方法才能拿到接收到的数据,这个回调方法方法 */
/** * 实现接收数据的回调方法 * * @param data 接收到的数据 * @param peer 传递数据的设备ID * @param session 当前回话 * @param context 注册监听传递过来的数据 */
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
}
* FireChat
* See You Around
* 以上近场聊天App都是基于mutipeerConnectivity框架
* 双方WIFI和蓝牙都没有打开:无法实现
* 双方都开启蓝牙:通过蓝牙发现和传输
* 双方都开启WIFI:通过WIFI Direct发现和传输,速度接近AirDrop
* 双方同时开启了WIFI和蓝牙:模拟AirDrop,通过低功耗蓝牙技术扫描发现握手,然后通过WIFI Direct传输
// 创建MCSession对象
// initWithPeer:设备的ID
// 用于存放当前的连接的会话
self.mc_Session = [ [MCSession alloc]initWithPeer:[[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name]];
// 广播对象,告诉其它的设备他们是可用的
// 创建广播对象
// initWithServiceType: 广播类型的标示(因为广播可能比较多,所以最好每个广播绑定一个唯一标示)(自定义字符串,类似cell的注册ID)
// session:当前会话
// discoveryInfo: 广播信息
MCAdvertiserAssistant *advertiserAssistant =[[MCAdvertiserAssistant alloc] initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:self.mc_Session];
}
// 开启广播
[self.advertiserAssistant start];
// 创建搜索蓝牙设备控制器
MCBrowserViewController *mbVC = [[MCBrowserViewController alloc]initWithServiceType:SERVICE_TYPE session:self.mc_Session];
// 设置控制器代理
mbVC.delegate = self;
// 跳转到搜索控制器
[self presentViewController:mbVC animated:YES completion:nil];
/** * 连接完成 * * @param browserViewController 搜索控制器 */
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - MCBrowserViewControllerDelegate
/** * 连接成功 * * @param browserViewController 搜索控制器 * @param peerID 连接上的设备ID * @param info 连接的信息 * * @return YES : 只发送连接上的用户 */
- (BOOL)browserViewController:(MCBrowserViewController *)browserViewController
shouldPresentNearbyPeer:(MCPeerID *)peerID
withDiscoveryInfo:(nullable NSDictionary<NSString *, NSString *> *)info
{
self.peerID = peerID;
NSLog(@"info == %@ peer = %@",info, peerID);
return YES;
}
// 获取图片
UIImage *image = self.showImage.image;
// 将图片转换成NSData类型
NSData *data = UIImagePNGRepresentation(image);
/** * 发送数据 * toPeers : 发给的设备ID的数组 * withMode: 发送模式,是否是安全模式 */
if (self.peerID != nil) {
[self.mc_Session sendData:data toPeers:@[self.peerID] withMode:MCSessionSendDataUnreliable error:nil];
}
}
// 接收的数据
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
UIImage *image = [[UIImage alloc]initWithData:data];
if(image != nil){
// 设置数据
dispatch_async(dispatch_get_main_queue(), ^{
self.showImage.image = image;
});
}
}
低功耗
著称,所以一般被称为BLE(bluetooth low energy)// 1. 创建中心管家,并且设置代理
self.cmgr = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
// 2. 在代理方法中扫描外部设备
/** * scanForPeripheralsWithServices :如果传入指定的数组,那么就只会扫描数组中对应ID的设备 * 如果传入nil,那么就是扫描所有可以发现的设备 * 扫描完外部设备就会通知CBCentralManager的代理 */
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if ([central state] == CBCentralManagerStatePoweredOn) {
[self.cmgr scanForPeripheralsWithServices:nil options:nil];
}
}
/** * 发现外部设备,每发现一个就会调用这个方法 * 所以可以使用一个数组来存储每次扫描完成的数组 */
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
// 有可能会导致重复添加扫描到的外设
// 所以需要先判断数组中是否包含这个外设
if(![self.peripherals containsObject:peripheral]){
[self.peripherals addObject:peripheral];
}
}
/** * 模拟开始连接方法 */
- (void)start
{
// 3. 连接外设
for (CBPeripheral *ppl in self.peripherals) {
// 扫描外设的服务
// 这个操作应该交给外设的代理方法来做
// 设置代理
ppl.delegate = self;
[self.cmgr connectPeripheral:ppl options:nil];
}
}
每个蓝牙4.0的设备都是通过服务和特征来展示自己的,一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征。
/** * 连接外设成功调用 */
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
// 查找外设服务
[peripheral discoverServices:nil];
}
/** * 发现服务就会调用代理方法 * * @param peripheral 外设 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
// 扫描到设备的所有服务
NSArray *services = peripheral.services;
// 根据服务再次扫描每个服务对应的特征
for (CBService *ses in services) {
[peripheral discoverCharacteristics:nil forService:ses];
}
}
/** * 发现服务对应的特征 */
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// 服务对应的特征
NSArray *ctcs = service.characteristics;
// 遍历所有的特征
for (CBCharacteristic *character in ctcs) {
// 根据特征的唯一标示过滤
if ([character.UUID.UUIDString isEqualToString:@"XMG"]) {
NSLog(@"可以吃饭了");
}
}
}
/** * 断开连接 */
- (void)stop
{
// 断开所有连接上的外设
for (CBPeripheral *per in self.peripherals) {
[self.cmgr cancelPeripheralConnection:per];
}
}