目前iOS智能硬件的开发交互方式主要分为两种,一种是基于低功耗的蓝牙4.0技术(由于耗电低,也称作为BLE(Bluetooth Low Energy))对应iOS的框架为CoreBluetooth,另外一种是基于Wi-Fi的连接方式,由于之前做过的两个项目用到的都是蓝牙,所以下面主要是介绍蓝牙4.0技术。

对应的在项目中添加的蓝牙开发权限

Important

An iOS app linked on or after iOS 10.0 must include in its Info.plist file the usage description
keys for the types of data it needs to access or it will crash. To access Bluetooth peripheral data specifically,
it must include NSBluetoothPeripheralUsageDescription.

在iOS中用于蓝牙开发的框架是CoreBluetooth,里面主要有以下几个核心概念:

CBCentralManager: 外部设备管理者

CBPeripheral: 连接的外部设备

CBService: 设备携带的服务

CBCharacteristic: 服务中包含的特征值

蓝牙的开发分两种模式,一种是app作为主设备,扫描连接其他外部蓝牙设备,另外一种是app作为外部设备被主设备连接。

目前开发的两个项目中主要用到的是第一种模式。

下面讲解一下第一种模式的流程:

1、首先先创建CBCentralManager对象,初始化完设置相应的代理对象之后就会有一个CBCentralManager的回调方法过来。

2、根据CBCentralManager初始化完之后的回调方法判断蓝牙目前的相应状态,如果处于开启状态则开始扫描包含你所需要的服务的外设。

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{

    switch (central.state) {
case CBManagerStateUnknown:
NSLog(@"CBManagerStateUnknown");
break;
case CBManagerStateResetting:
NSLog(@"CBManagerStateResetting");
break;
case CBManagerStateUnsupported:
NSLog(@"CBManagerStateUnsupported");
break;
case CBManagerStateUnauthorized:
NSLog(@"CBManagerStateUnauthorized");
break;
case CBManagerStatePoweredOff:
NSLog(@"CBManagerStatePoweredOff");
break;
case CBManagerStatePoweredOn:
NSLog(@"CBManagerStatePoweredOn");
//开始扫描周围的外设
/*
第一个参数nil就是扫描周围所有的外设,扫描到外设后会进入
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;
第二个参数可以添加一些option,来增加精确的查找范围, 如 :
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
[manager scanForPeripheralsWithServices:nil options:options]; */
[central scanForPeripheralsWithServices:nil options:nil]; break;
default:
break;
} }

3、扫面发现需要的外围设备

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{

    //连接外围设备
   //advertisementData 外围设备广播的信息,一般会在kCBAdvDataLocalName这个地方放置设备的mac地址,如果安卓收得到iOS设备收不到,一般来说是因为硬件设备没有调试好,可以找硬件工程师师调试
  if ([peripheral.name isEqualToString:@"外设名字"] && peripheral) { [self.centralManager connectPeripheral:peripheral options:nil]; } }

4、连接外部设备

//连接到外围设备
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"连接外围设备成功");
//设置外围设备的代理为当前视图控制器
peripheral.delegate = self;
//外围设备开始寻找服务
[peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
} //连接外围设备失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@"连接外围设备失败!");
}

5、发现外部设备的服务、发现外部设备的服务对应的特征值

//外围设备寻找到服务后
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
NSLog(@"已发现可用服务...");
if (error) {
NSLog(@"外围设备寻找服务过程中发生错误,错误信息:%@",error.localizedDescription);
} //遍历查找到的服务
CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
for (CBService *service in peripheral.services){
if ([service.UUID isEqual:serviceUUID]) {
//外围设备查找指定服务中的特征
[peripheral discoverCharacteristics:@[characteristicUUID] forService:service];
}
}
} //外围设备寻找到特征后
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
NSLog(@"已发现可用特征....");
if (error) {
NSLog(@"外围设备寻找特征过程中发生错误,错误信息:%@",error.localizedDescription);
} //遍历服务中的特征
CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
if ([service.UUID isEqual:serviceUUID]) {
for (CBCharacteristic *characteristic in service.characteristics){
if ([characteristic.UUID isEqual:characteristicUUID]) {
//情景一:通知
/*找到特征后设置外围设备为已通知状态(订阅特征):
1.调用此方法会触发代理方法-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
2.调用此方法会触发外围设备的订阅代理方法
*/
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
//情景二:读取
// [peripheral readValueForCharacteristic:characteristic];
// if (characteristic.value) {
// NSString *value = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
// NSLog(@"读取到特征值:%@",value);
// }
}
}
}
}

6、订阅特征值的状态发生更新

//特征值被更新后
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
NSLog(@"收到特征更新通知...");
if (error) {
NSLog(@"更新通知状态时发生错误,错误信息:%@",error.localizedDescription);
} //给特征值设置新的值
CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
if ([characteristic.UUID isEqual:characteristicUUID]) {
if (characteristic.isNotifying) {
if (characteristic.properties == CBCharacteristicPropertyNotify) {
NSLog(@"已订阅特征通知.");
return;
}else if (characteristic.properties == CBCharacteristicPropertyRead){
//从外围设备读取新值,调用此方法会触发代理方法:-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
[peripheral readValueForCharacteristic:characteristic];
}
}else{
NSLog(@"停止已停止."); //取消连接
[self.centralManager cancelPeripheralConnection:peripheral];
}
}
}

7、特征值发生变化时的回调方法

//更新特征值后(调用readValueForCharacteristic:方法或者外围设备在订阅后更新特征值都会调用此代理方法)
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error) {
NSLog(@"更新特征值时发生错误,错误信息:%@",error.localizedDescription);
return;
}
if (characteristic.value) {
NSString *value = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSLog(@"读取到特征值:%@",value);
}else{
NSLog(@"未发现特征值.");
}
}

8、往蓝牙设备写入数据

- (void)writeDataWithHexStr:(NSString *)hexStr {
NSData *data = [self convertHexStrToData:hexStr];
[self.peripheral writeValue:data forCharacteristic:self.characteristic type:CBCharacteristicWriteWithResponse];
} // 16进制转NSData
- (NSData *)convertHexStrToData:(NSString *)str
{
if (!str || [str length] == 0) {
return nil;
} NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
NSRange range;
if ([str length] % 2 == 0) {
range = NSMakeRange(0, 2);
} else {
range = NSMakeRange(0, 1);
}
for (NSInteger i = range.location; i < [str length]; i += 2) {
unsigned int anInt;
NSString *hexCharStr = [str substringWithRange:range];
NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr]; [scanner scanHexInt:&anInt];
NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
[hexData appendData:entity]; range.location += range.length;
range.length = 2;
}
return hexData;
}

9、关闭蓝牙设备

- (void)closeBlueTooth {
[self.centralManager stopScan];
if (self.peripheral) {
[self.centralManager cancelPeripheralConnection:self.peripheral];
}
self.centralManager = nil;
self.peripheral = nil;
self.characteristic = nil;
}

10、对应的还有swift版蓝牙相关的常用工具类方法

//MARK: -  工具类方法
//16进制字符串转化为data
class func HexStrToData(hexStr:String)->Data { assert(hexStr.count % 2 == 0, "输入字符串格式不对,8位代表一个字符")
var bytes = [UInt8]()
var sum = 0
// 整形的 utf8 编码范围
let intRange = 48...57
// 小写 a~f 的 utf8 的编码范围
let lowercaseRange = 97...102
// 大写 A~F 的 utf8 的编码范围
let uppercasedRange = 65...70
for (index, c) in hexStr.utf8CString.enumerated() {
var intC = Int(c.byteSwapped)
if intC == 0 {
break
} else if intRange.contains(intC) {
intC -= 48
} else if lowercaseRange.contains(intC) {
intC -= 87
} else if uppercasedRange.contains(intC) {
intC -= 55
} else {
assertionFailure("输入字符串格式不对,每个字符都需要在0~9,a~f,A~F内")
}
sum = sum * 16 + intC
// 每两个十六进制字母代表8位,即一个字节
if index % 2 != 0 {
bytes.append(UInt8(sum))
sum = 0
}
}
let data = Data(bytes: bytes)
return data } //16进制Data 转 String
class func string(from data:Data)->String { return data.map { String(format: "%02x", $0) }
.joined(separator: "") } // MARK: - 十进制转二进制
class func decTobin(number:Int) -> String {
var num = number
var str = ""
while num > 0 {
str = "\(num % 2)" + str
num /= 2
}
return str
} // MARK: - 二进制转十进制
class func binTodec(number num: String) -> Int {
var sum: Int = 0
for c in num {
let str = String(c)
sum = sum * 2 + Int(str)!
} return sum
} // MARK: - 十进制转十六进制
class func decTohex(number:Int) -> String {
return String(format: "%0X", number)
} // MARK: - 十六进制转十进制
class func hexTodec(number num:String) -> Int {
let str = num.uppercased()
var sum = 0
for i in str.utf8 {
sum = sum * 16 + Int(i) - 48 // 0-9 从48开始
if i >= 65 { // A-Z 从65开始,但有初始值10,所以应该是减去55
sum -= 7
}
}
return sum
}

iOS 蓝牙开发详解的更多相关文章

  1. iOS应用开发详解

    <iOS应用开发详解> 基本信息 作者: 郭宏志    出版社:电子工业出版社 ISBN:9787121207075 上架时间:2013-6-28 出版日期:2013 年7月 开本:16开 ...

  2. iOS自定义控件开发详解

    http://blog.csdn.net/zhangao0086/article/details/45622875

  3. iOS原生地图开发详解

    在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...

  4. [转载]Apple Watch 开发详解

    Apple Watch 开发详解 Apple Watch现在对于第三方开发者来说更多的还是一块额外的屏幕.暂时WatchKit没有能给出足够的接口.现在Watch App的主要运算逻辑需要依赖iPho ...

  5. Apple Watch 开发详解

    Apple Watch 开发详解 Apple Watch现在对于第三方开发者来说更多的还是一块额外的屏幕.暂时WatchKit没有能给出足够的接口.现在Watch App的主要运算逻辑需要依赖iPho ...

  6. 转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )

    原文地址:IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本作者:佐佐木小次郎 因为最近项目上要用有关LBS的功能.于是我便做一下预研. 一般说来LBS功能一般分为两块:一块是地理 ...

  7. javacv开发详解之1:调用本机摄像头视频(建议使用javaCV1.3版本)

    javaCV系列文章: javacv开发详解之1:调用本机摄像头视频 javaCV开发详解之2:推流器实现,推本地摄像头视频到流媒体服务器以及摄像头录制视频功能实现(基于javaCV-FFMPEG.j ...

  8. 了解iOS消息推送一文就够:史上最全iOS Push技术详解

    本文作者:陈裕发, 腾讯系统测试工程师,由腾讯WeTest整理发表. 1.引言 开发iOS系统中的Push推送,通常有以下3种情况: 1)在线Push:比如QQ.微信等IM界面处于前台时,聊天消息和指 ...

  9. Xamarin+Prism开发详解七:Plugin开发与打包测试

    有了上章[Xamarin+Prism开发详解六:DependencyService与IPlatformInitializer的关系]的基础,现在来理解Plugin开发就简单了. 本文实例代码地址:ht ...

随机推荐

  1. P1001 害死人不偿命的(3n+1)猜想 (Basic Level)

    转跳点:

  2. mariadb galera启动问题

    搭建了mariadb galera集群后,整个集群停掉后如何启动呢,我们要先启动那个节点呢,今天我尝试把集群机器全部停掉,然后任意启动一个节点,发现报如下的错误: 170620 21:32:55 [E ...

  3. redis主从遇到的问题

    rendis安装 主从切换 Redis的HA方案 Redis高可用架构(1)—Keepalive+VIP 还是配置的问题   从一直无法连接上主 redis.conf配置 # Examples:# 1 ...

  4. 091-PHP数组过滤函数array_filter,过滤非整数

    <?php function not_int($x){ //定义过滤非整数的函数 if(is_int($x)) return TRUE; } $arr=array(1,2,'a',3,4,'c' ...

  5. SQL优化工具 - SQL Server Profiler与数据库引擎优化顾问

    最近项目做到几千个学生分别去人脸识别记录(目前约630000行)中查询最后一次记录,可想而知性能这块是个麻烦.于是乎,GET到了SQL Server Profiler和数据库引擎优化顾问这俩工SHEN ...

  6. 编译Linux

    下载内核源文件 https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git/ 生成.config配置文件 make ...

  7. Spring Cloud Alibaba:Sentinel实现熔断与限流

    一.什么是Sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制.熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩效应”,为微服务系统提供了稳 ...

  8. 201912-1 报数 Java

    思路: String.valueOf(int i) : 将 int 变量 i 转换成字符串 String.contains()用于判断字符串是否包含子字符串 import java.util.Scan ...

  9. 【redis】redis底层数据结构原理--简单动态字符串 链表 字典 跳跃表 整数集合 压缩列表等

    redis有五种数据类型string.list.hash.set.zset(字符串.哈希.列表.集合.有序集合)并且自实现了简单动态字符串.双端链表.字典.压缩列表.整数集合.跳跃表等数据结构.red ...

  10. 15 —— npm —— package.json 与 package-lock.json 的作用

    一,package.json  是 npm init 时创建的一个文件,会记录当前整个项目中的一些基础信息. 二,package-lock.json 是 node_modules 文件夹或者 pack ...