您的位置:首页 > 移动开发 > IOS开发

iOS蓝牙4.0开发(BLE)

2016-04-07 17:58 555 查看
智能设备 和 app 通过 BLE通讯的两种模型

模型一:设备提供数据,app 展示数据; 比如小米手环

模型二:app提供数据,设备接收;

模型与corebluetooth的对应关系;

模型一:智能设备,对应 peripheral;app对应central

模型二:智能设备作为central ; app作为 peripheral

这里主要讨论模型一,这也是当前大多数手环设备和iOS 交互的方式;开发流程

1. 创建工程,导入CoreBluetooth.framework

2. 初始化 CBCentralManager 并准备扫描周围蓝牙设备

//初始化
theManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];

//初始化完成会调用如下代理,返回当前手机的蓝牙状态

//当前蓝牙主设备状态

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

if (central.state==CBCentralManagerStatePoweredOn) {

self.title = @"蓝牙已就绪";

self.connectBtn.enabled = YES;

}else

{

self.title = @"蓝牙未准备好";

[self.activeID stopAnimating];

switch (central.state) {

case CBCentralManagerStateUnknown:

NSLog(@">>>CBCentralManagerStateUnknown");

break;

case CBCentralManagerStateResetting:

NSLog(@">>>CBCentralManagerStateResetting");

break;

case CBCentralManagerStateUnsupported:

NSLog(@">>>CBCentralManagerStateUnsupported");

break;

case CBCentralManagerStateUnauthorized:

NSLog(@">>>CBCentralManagerStateUnauthorized");

break;

case CBCentralManagerStatePoweredOff:

NSLog(@">>>CBCentralManagerStatePoweredOff");

break;

default:

break;

}

}

}

3. 当蓝牙状态一切正常时,这时可以开始搜索周边设备了,比如手环

//开始连接action
- (IBAction)startConnectAction:(id)sender {

if (theManager.state==CBCentralManagerStatePoweredOn) {
NSLog(@"主设备蓝牙状态正常,开始扫描外设...");
self.title = @"扫描小米手环...";
//nil表示扫描所有设备
[theManager scanForPeripheralsWithServices:nil options:nil];
[self.activeID startAnimating];
self.connectBtn.enabled = NO;
self.resultTextV.text = @"";

}

}


4. 在扫描到设备的代理里面,连接我们找到的智能设备

//扫描到设备会进入方法
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"扫描连接外设:%@ %@",peripheral.name,RSSI);
[central connectPeripheral:peripheral options:nil];
if ([peripheral.name hasSuffix:@"MI"]) {
thePerpher = peripheral;
[central stopScan];
[central connectPeripheral:peripheral options:nil];
self.title = @"发现小米手环,开始连接...";
self.resultTextV.text = [NSString stringWithFormat:@"发现手环:%@\n名称:%@\n",peripheral.identifier.UUIDString,peripheral.name];
}
}


5. 连接成功之后,去扫描设备的服务;扫描到服务之后,紧接着去扫描设备的特征;好比一个类提供的接口,只有知道这些,我们才能操作智能设备

#pragma mark 设备信息处理
//扫描到具体的值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
{
if (error) {
NSLog(@"扫描外设的特征失败!%@-> %@",peripheral.name, [error localizedDescription]);
self.title = @"find value error.";
return;
}
NSLog(@"%@ %@",characteristic.UUID.UUIDString,characteristic.value);
if ([characteristic.UUID.UUIDString isEqualToString:kMI_STEP]) {
Byte *steBytes = (Byte *)characteristic.value.bytes;
int steps = TCcbytesValueToInt(steBytes);
NSLog(@"步数:%d",steps);
self.resultTextV.text = [NSString stringWithFormat:@"%@步数:%d\n",self.resultTextV.text,steps];
}
else if ([characteristic.UUID.UUIDString isEqualToString:kMI_BUTERY])
{
Byte *bufferBytes = (Byte *)characteristic.value.bytes;
int buterys = TCcbytesValueToInt(bufferBytes)&0xff;
NSLog(@"电池:%d%%",buterys);
self.resultTextV.text = [NSString stringWithFormat:@"%@电量:%d%%\n",self.resultTextV.text,buterys];

}
else if ([characteristic.UUID.UUIDString isEqualToString:kMI_DEVICE])
{
Byte *infoByts = (Byte *)characteristic.value.bytes;
//这里解析infoByts得到设备信息

}

[self.activeID stopAnimating];
self.connectBtn.enabled = YES;
self.title = @"信息扫描完成";

}


View Code
7. 完整的代码工程;

https://github.com/cocoajin/BLE-miband
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: