[蓝牙]CoreBluetooth获取蓝牙设备UUID

更多智能设备开始脱离音频口,改用蓝牙4.0。【iPhone 4S,iOS 7 以上设备】
所以接入产商SDK的时候,需要自行匹配蓝牙模块的UUID。

1)导入CoreBluetooth.framework
2)加入头#import “CoreBluetooth/CoreBluetooth.h”
3)引入协议
4)看下面代码:
[objc]
@interface BlueSearchViewCtrl : UIViewController<CBCentralManagerDelegate,CBPeripheralDelegate>{

NSMutableArray *blueDevs,*nameDevs;//用于存放蓝牙UUID,用于存放设备名,也可直接整成Arr+Dict
CBCentralManager *centralMgr;

}
[/objc]
5)创建事例,初始化2动态数组
[objc]
centralMgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

nameDevs = [NSMutableArray new];
[nameDevs addObject:@">>退出蓝牙匹配"];

blueDevs = [NSMutableArray new];
[/objc]
6) 查找设备
[objc]
– (void)centralManagerDidUpdateState:(CBCentralManager *)central{

switch (central.state) {
case CBCentralManagerStatePoweredOn:{
//这个字典,主要用来过滤不要出现重复情况,不然会不停的添加进来;当然下面options:直接nil也行;
NSDictionary * tempDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO],CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[centralMgr scanForPeripheralsWithServices:nil options:tempDict];
}break;

default:
NSLog(@"Central Manager State Other");
break;
}

}
[/objc]
7)设备回调
[objc]
– (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
[nameDevs addObject:peripheral.name];
[blueDevs addObject:peripheral.identifier.UUIDString];
UITableView *table = (UITableView*)[self.view viewWithTag:1];
[table reloadData];
NSLog(@"name : %@", peripheral.name);
NSLog(@"identifier : %@", peripheral.identifier.UUIDString);

}
[/objc]
8)由于接入产商只要得到UUID即可,后面部分就略了。
9)关于连接蓝牙,与回调
[objc]
[centralMgr connectPeripheral:peripheral options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
[/objc][objc]
– (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
}
[/objc]

76 thoughts on “[蓝牙]CoreBluetooth获取蓝牙设备UUID

Kmkrog进行回复 取消回复

电子邮件地址不会被公开。