CoreBluetooth的实现代码
来源:互联网 发布:淘宝装修助手 编辑:程序博客网 时间:2024/05/19 17:58
//
// ViewController.m
// CoreBluetooth
//
// Created by 陈强 on 15/10/23.
// Copyright © 2015年 com.chenqiang. All rights reserved.
//
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property (nonatomic ,strong)CBCentralManager *manager;
;
@property (nonatomic ,strong)NSMutableArray *peripherals;
@property (nonatomic ,strong)CBCharacteristic *dataInteracther;
@property (nonatomic ,strong)CBCharacteristic *datainterperipheral;
@end
@implementation ViewController
- (NSMutableArray *)peripherals
{
if (!_peripherals) {
self.peripherals = [NSMutableArray array];
}
return self.peripherals;
}
- (CBCentralManager *)manager
{
//建立中心设备 用来管理中心设备,
if (!_manager) {
self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
}
return _manager;
}
- (void)viewDidLoad {
[super viewDidLoad];
//1.建立中心设备
self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
//扫描外设
#warning 通过传入一个存放服务的UDID的数组进去,过滤掉一些不要的外设
[self.manager scanForPeripheralsWithServices:@[@"123",@"345"] options:nil];
}
/**
* 建立连接
*/
- (void)buildConnect
{
for (CBPeripheral *peripheral in self.peripherals) {
[self.manager connectPeripheral:peripheral options:nil];
}
}
#warning mark -- CBcentralManagerdelegate
//发现外围设备的时候调用
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
//添加外围设备
if (![self.peripherals containsObject:peripheral]) {
//设置外设的代理
peripheral.delegate = self;
[self.peripherals addObject:peripheral];
}
}
//连接到某个外围设备的时候调用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
//查找外设中所有服务
[peripheral discoverServices:nil];
}
/**
* 跟某个外接设备失去连接
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
}
#warning mark --- 外设的代理方法
/**
* 外设已经找到服务
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
//遍历所有的服务
for (CBService *service in peripheral.services) {
//过滤不要的服务
if ([service.UUID isEqual:@"外设UUID"]) {
//扫描服务下面的特征
#warning 通过传入一个存放服务的UDID的数组进去,过滤掉一些不要的外设
[peripheral discoverCharacteristics:@[@"123",@"345"] forService:service];
}
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
//遍历所有的特征
for (CBCharacteristic *characteristic in service.characteristics) {
//过滤不要的特征
if ([characteristic.UUID isEqual:@"外设UUID"]) {
//找到想要的特征
self.dataInteracther = characteristic;
}else if ([characteristic.UUID isEqual:@"789"]){
self.datainterperipheral = characteristic;
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// ViewController.m
// CoreBluetooth
//
// Created by 陈强 on 15/10/23.
// Copyright © 2015年 com.chenqiang. All rights reserved.
//
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property (nonatomic ,strong)CBCentralManager *manager;
;
@property (nonatomic ,strong)NSMutableArray *peripherals;
@property (nonatomic ,strong)CBCharacteristic *dataInteracther;
@property (nonatomic ,strong)CBCharacteristic *datainterperipheral;
@end
@implementation ViewController
- (NSMutableArray *)peripherals
{
if (!_peripherals) {
self.peripherals = [NSMutableArray array];
}
return self.peripherals;
}
- (CBCentralManager *)manager
{
//建立中心设备 用来管理中心设备,
if (!_manager) {
self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
}
return _manager;
}
- (void)viewDidLoad {
[super viewDidLoad];
//1.建立中心设备
self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
//扫描外设
#warning 通过传入一个存放服务的UDID的数组进去,过滤掉一些不要的外设
[self.manager scanForPeripheralsWithServices:@[@"123",@"345"] options:nil];
}
/**
* 建立连接
*/
- (void)buildConnect
{
for (CBPeripheral *peripheral in self.peripherals) {
[self.manager connectPeripheral:peripheral options:nil];
}
}
#warning mark -- CBcentralManagerdelegate
//发现外围设备的时候调用
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
//添加外围设备
if (![self.peripherals containsObject:peripheral]) {
//设置外设的代理
peripheral.delegate = self;
[self.peripherals addObject:peripheral];
}
}
//连接到某个外围设备的时候调用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
//查找外设中所有服务
[peripheral discoverServices:nil];
}
/**
* 跟某个外接设备失去连接
*/
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
}
#warning mark --- 外设的代理方法
/**
* 外设已经找到服务
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
//遍历所有的服务
for (CBService *service in peripheral.services) {
//过滤不要的服务
if ([service.UUID isEqual:@"外设UUID"]) {
//扫描服务下面的特征
#warning 通过传入一个存放服务的UDID的数组进去,过滤掉一些不要的外设
[peripheral discoverCharacteristics:@[@"123",@"345"] forService:service];
}
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
//遍历所有的特征
for (CBCharacteristic *characteristic in service.characteristics) {
//过滤不要的特征
if ([characteristic.UUID isEqual:@"外设UUID"]) {
//找到想要的特征
self.dataInteracther = characteristic;
}else if ([characteristic.UUID isEqual:@"789"]){
self.datainterperipheral = characteristic;
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
1 0
- CoreBluetooth的实现代码
- CoreBluetooth后台运行的实现
- iOS CoreBluetooth的swift实现 (1)
- CoreBluetooth 完整实现
- CoreBluetooth
- CoreBluetooth
- CoreBluetooth
- CoreBluetooth
- CoreBluetooth 框架的常用方法
- CoreBluetooth的使用问题汇总
- iOS CoreBluetooth 的使用讲解
- iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth 蓝牙中心设备的实现 蓝牙外设的实现 有Demo
- iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth 蓝牙中心设备的实现 蓝牙外设的实现 有Demo
- CoreBluetooth之数据的读取与发送
- iOS-蓝牙简介及CoreBluetooth.framework的使用
- iOS coreBluetooth编程中需要注意的问题
- IOS CoreBluetooth系列四:一个简单的BlueTooth管理类
- CoreBluetooth学习
- UIView笔记
- 九度OJ 1114:神奇的口袋 (DFS、DP)
- 在Android程序中使用已有的SQLite数据库
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- TFS 无法找到新加的Windows用户
- CoreBluetooth的实现代码
- 递归算法复杂度的求法
- PHP:6种方法获取文件的扩展名
- 01-复杂度2 Maximum Subsequence Sum (25分)
- c#简单实现记事本功能
- 筛素数
- 【鸟哥的linux私房菜-学习笔记】首次使用相关知识、在线求助 man page
- Douglas Crockford 大神写的 JavaScript 异步控制库:RQ(上)
- 网络编程 笔记(九)Echo客户端 Utniy实现