CodeForge QQ客服 CodeForge 400电话 客服电话 4006316121
首页 » 源代码 » iOS蓝牙开发 » ViewController.m

ViewController.m ( 文件浏览 )

文件源自:iOS蓝牙开发
  • dy1990 发布于2016-07-26
  • 浏览次数:0
  • 下载次数:0
  • 下载需 1 积分
  • 侵权举报
			//
//  ViewController.m
//  PeripheralApp
//
//  Created by Kenshin Cui on 14/04/05.
//  Copyright (c) 2014年 cmjstudio. All rights reserved.
//  外围设备(周边设备)

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#define kPeripheralName @"Kenshin Cui's Device" //外围设备名称
#define kServiceUUID @"C4FB2349-72FE-4CA2-94D6-1F3CB16331EE" //服务的UUID
#define kCharacteristicUUID @"6A3E4B28-522D-4B3B-82A9-D5E2004534FC" //特征的UUID

@interface ViewController ()<CBPeripheralManagerDelegate,UITextViewDelegate,CBCentralManagerDelegate>

@property (strong,nonatomic) CBPeripheralManager *peripheralManager;//外围设备管理器
@property (strong,nonatomic) NSMutableArray *centralM;//订阅此外围设备特征的中心设备
@property (strong,nonatomic) CBMutableCharacteristic *characteristicM;//特征
@property (weak, nonatomic) IBOutlet UITextView *log;//日志记录

@end

@implementation ViewController
#pragma mark - 视图控制器方法
- (void)viewDidLoad {

    [super viewDidLoad];
    
    self.log.delegate = self;

}

#pragma mark - UI事件
//创建外围设备
/*- (IBAction)startClick:(UIBarButtonItem *)sender {

    _peripheralManager=[[CBPeripheralManager alloc]initWithDelegate:self queue:nil];

}*/
- (IBAction)startClick:(UIButton *)sender {

       _peripheralManager=[[CBPeripheralManager alloc]initWithDelegate:self queue:nil];

}
//更新数据
- (IBAction)transferClick:(UIButton *)sender {

    [self updateCharacteristicValue];

}

#pragma mark - CBPeripheralManager代理方法
//外围设备状态发生变化后调用
-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{

    switch (peripheral.state) {

        case CBPeripheralManagerStatePoweredOn:
            NSLog(@"BLE已打开.");
            [self writeToLog:@"BLE已打开."];
            //添加服务
            [self setupService];
            break;
            
        default:
            NSLog(@"此设备不支持BLE或未打开蓝牙功能,无法作为外围设备.");
            [self writeToLog:@"此设备不支持BLE或未打开蓝牙功能,无法作为外围设备."];
            break;
    
}

}
//外围设备添加服务后调用
-(void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{

    if (error) {

        NSLog(@"向外围设备添加服务失败,错误详情:%@",error.localizedDescription);
        [self writeToLog:[NSString stringWithFormat:@"向外围设备添加服务失败,错误详情:%@",error.localizedDescription]];
        return;
    
}
    
    //添加服务后开始广播
    NSDictionary *dic=@{
CBAdvertisementDataLocalNameKey:kPeripheralName
};//广播设置
    [self.peripheralManager startAdvertising:dic];//开始广播
    NSLog(@"向外围设备添加了服务并开始广播...");
    [self writeToLog:@"向外围设备添加了服务并开始广播..."];

}
-(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{

    if (error) {

        NSLog(@"启动广播过程中发生错误,错误信息:%@",error.localizedDescription);
        [self writeToLog:[NSString stringWithFormat:@"启动广播过程中发生错误,错误信息:%@",error.localizedDescription]];
        return;
    
}
    NSLog(@"启动广播...");
    [self writeToLog:@"启动广播..."];

}
//订阅特征
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic{

    NSLog(@"中心设备:%@ 已订阅特征:%@.",central,characteristic);
    [self writeToLog:[NSString stringWithFormat:@"中心设备:%@ 已订阅特征:%@.",central.identifier.UUIDString,characteristic.UUID]];
    //发现中心设备并存储
    if (![self.centralM containsObject:central]) {

        [self.centralM addObject:central];
    
}
    /*中心设备订阅成功后外围设备可以更新特征值发送到中心设备,一旦更新特征值将会触发中心设备的代理方法:
     -(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
     */
    
    //    [self updateCharacteristicValue];

}
//取消订阅特征
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic{

    NSLog(@"didUnsubscribeFromCharacteristic");

}

//当peripheral接受到一个读ATT读请求,数据在CBATTRequest
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request{

    NSLog(@"didReceiveReadRequest");

}

//当peripheral接受到一个写请求的时候调用,参数有一个数组的CBATTRequest对象request
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests{


    CBATTRequest *requestData = requests[0];
    NSData *data = requestData.value;
    NSString *str =[[ NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"设备接收的结果--------%@",str);

}

/*-(void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(CBATTRequest *)request{

    NSLog(@"didReceiveWriteRequests");
    
//    NSData *data = request.characteristic.value;
//    NSString *str =[[ NSString alloc] initWithData:request.characteristic.value encoding:NSUTF8StringEncoding];
//    NSLog(@"设备接收的结果--------%@",str);

}
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request{

    NSString *str =[[ NSString alloc] initWithData:request.characteristic.value encoding:NSUTF8StringEncoding];
    NSLog(@"设备接收的结果--------%@",str);

}*/

- (void)respo
...
...
(完整源码请下载查看)
			
...
展开> <收缩

下载源码到电脑,阅读使用更方便

1 积分

快速下载
还剩0行未阅读,继续阅读
Ʋ

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
...
名称 大小 修改日期
01.97 kB
.DS_Store6.00 kB2016-05-24|14:10
01.97 kB
01.97 kB
._.DS_Store120.00 B2016-05-24|14:10
01.97 kB
AppDelegate.h272.00 B2016-05-21|16:18
01.97 kB
._AppDelegate.h171.00 B2016-05-21|16:18
AppDelegate.m1.98 kB2016-05-21|16:18
._AppDelegate.m171.00 B2016-05-21|16:18
01.97 kB
01.97 kB
Contents.json585.00 B2016-05-21|16:18
01.97 kB
01.97 kB
._Contents.json171.00 B2016-05-21|16:18
01.97 kB
LaunchScreen.storyboard1.62 kB2016-05-21|16:22
Main.storyboard6.26 kB2016-05-23|15:08
Info.plist1.18 kB2016-05-21|16:18
main.m329.00 B2016-05-21|16:18
._main.m171.00 B2016-05-21|16:18
ViewController.h210.00 B2016-05-21|16:18
._ViewController.h171.00 B2016-05-21|16:18
ViewController.m10.89 kB2016-07-15|14:00
._ViewController.m171.00 B2016-07-15|14:00
01.97 kB
project.pbxproj19.65 kB2016-05-21|16:19
01.97 kB
contents.xcworkspacedata155.00 B2016-05-21|16:18
01.97 kB
01.97 kB
UserInterfaceState.xcuserstate23.56 kB2016-07-15|14:13
01.97 kB
01.97 kB
01.97 kB
Breakpoints_v2.xcbkptlist824.00 B2016-07-15|14:12
01.97 kB
CentralApp.xcscheme4.12 kB2016-05-21|16:18
xcschememanagement.plist662.00 B2016-05-21|16:18
._CentralApp.xcodeproj152.00 B2016-05-21|16:19
01.97 kB
CentralAppTests.m903.00 B2016-05-21|16:18
01.97 kB
._CentralAppTests.m171.00 B2016-05-21|16:18
Info.plist733.00 B2016-05-21|16:18
01.97 kB
CentralAppUITests.m1.18 kB2016-05-21|16:18
01.97 kB
._CentralAppUITests.m171.00 B2016-05-21|16:18
Info.plist733.00 B2016-05-21|16:18
01.97 kB
.DS_Store6.00 kB2016-05-24|15:41
01.97 kB
._.DS_Store120.00 B2016-05-24|15:41
01.97 kB
AppDelegate.h275.00 B2016-05-21|16:17
01.97 kB
._AppDelegate.h171.00 B2016-05-21|16:17
AppDelegate.m1.98 kB2016-05-21|16:17
._AppDelegate.m171.00 B2016-05-21|16:17
01.97 kB
01.97 kB
Contents.json585.00 B2016-05-21|16:17
01.97 kB
01.97 kB
._Contents.json171.00 B2016-05-21|16:17
01.97 kB
LaunchScreen.storyboard1.63 kB2016-05-21|16:17
01.97 kB
._LaunchScreen.storyboard171.00 B2016-05-21|16:17
Main.storyboard6.05 kB2016-05-23|17:19
Info.plist1.18 kB2016-05-21|16:17
main.m332.00 B2016-05-21|16:17
._main.m171.00 B2016-05-21|16:17
ViewController.h213.00 B2016-05-21|16:17
._ViewController.h171.00 B2016-05-21|16:17
ViewController.m8.51 kB2016-05-24|10:20
._ViewController.m171.00 B2016-05-24|10:20
01.97 kB
project.pbxproj19.79 kB2016-05-21|16:17
01.97 kB
contents.xcworkspacedata158.00 B2016-05-21|16:17
01.97 kB
01.97 kB
UserInterfaceState.xcuserstate23.88 kB2016-07-26|10:40
01.97 kB
01.97 kB
01.97 kB
Breakpoints_v2.xcbkptlist809.00 B2016-07-26|10:40
01.97 kB
PeripheralApp.xcscheme4.17 kB2016-05-21|16:17
xcschememanagement.plist665.00 B2016-05-21|16:17
._PeripheralApp.xcodeproj152.00 B2016-05-21|16:17
01.97 kB
Info.plist733.00 B2016-05-21|16:17
PeripheralAppTests.m915.00 B2016-05-21|16:17
01.97 kB
._PeripheralAppTests.m171.00 B2016-05-21|16:17
01.97 kB
Info.plist733.00 B2016-05-21|16:17
PeripheralAppUITests.m1.20 kB2016-05-21|16:17
01.97 kB
._PeripheralAppUITests.m171.00 B2016-05-21|16:17
Ʋ

ViewController.m (76.72 kB)

需要 1 积分
您持有 积分

CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了 了解更多

您的积分不足

支付宝优惠套餐快速获取 30 积分

订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。

更多付款方式:网银PayPal

上传代码,免费获取

您本次下载所消耗的积分将转交上传作者。

同一源码,30天内重复下载,只扣除一次积分。

登录 CodeForge

还没有CodeForge账号? 立即注册
关注微博
联系客服

Switch to the English version?

Yes
CodeForge 英文版
No
CodeForge 中文版

完善个人资料,获价值¥30元积分奖励!

^_^"呃 ...

Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧
好的