目前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. Kafka--Kafka简述

    Kafka的诞生 Kafka最初是LinkedIn的一个内部基础设施系统.我们发现,虽然有很多数据库和系统可以用来存储数据,但在我们的架构里,刚好缺一个可以帮助处理持续数据流的组件. 我们希望能够把数 ...

  2. C++面试常见问题——09static关键字

    static关键字 局部变量 局部变量 局部变量是最常见的量,编译器不会对其进行初始化,除非对其显式赋值,不然初始值是随机的. 普通局部变量存储在栈空间,使用完毕后会立即被释放. 静态局部变量 静态局 ...

  3. 【转】R语言函数总结

    原博: R语言与数据挖掘:公式:数据:方法 R语言特征 对大小写敏感 通常,数字,字母,. 和 _都是允许的(在一些国家还包括重音字母).不过,一个命名必须以 . 或者字母开头,并且如果以 . 开头, ...

  4. STM32中ARM系列编译工具链的编译宏选择(__CC_ARM、__ICCARM__、__GNUC__、__TASKING__)

    一 前言 stm32 f103中.关系到一个选择何种编译宏的问题.这里就梳理一下吧. 二 正文 1  在 core_cm3.h 文件中,有如下代码: #if defined ( __CC_ARM ) ...

  5. 本机无oracle,远程连接

    描述 本机无oracle,通过PLSQL Developer程序,连接虚拟机中的oracle11gR2 1 下载instant-client 需要和服务端版本对应 下载相应的instant-clien ...

  6. 《Python爬虫技术:深入理解原理、技术与开发》已经出版,送Python基础视频课程

    好消息,<Python爬虫技术:深入理解原理.技术与开发>已经出版!!!   JetBrains官方推荐图书!JetBrains官大中华区市场部经理赵磊作序!送Python基础视频课程!J ...

  7. 13.swoole学习笔记--DNS查询

    <?php //执行DNS查询 swoole_async_dns_lookup("www.baidu.com",function($host,$ip){ echo $ip; ...

  8. Swift 枚举enum

    enum methodType{ case get case post case put case delete } 枚举赋值 enum methodType:String{ case get=&qu ...

  9. 学术Essay写作如何体现逻辑的应用

    作为一篇学术essay,逻辑要求是必不可少的.那么,学术essay如何写作才能体现逻辑呢?这就需要从语言逻辑和科学逻辑出发. 语言逻辑指的是三C原则:(1)complete(完整),(2)concis ...

  10. AIDL使用绑定启动远程Service出现Service Intent must be explicit: Intent

    Intent intent = new Intent(); intent.setAction("remote.MyRemoteService.Action"); 使用AIDL调用远 ...