1, 使用
CBCentralManager * _manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
初始化一个中央设备(_manager);
判断手机是否开启了蓝牙只有central.state=CBCentralManagerStatePoweredOn 才能去链接蓝牙设备
//开始查看服务,蓝牙开启
-(void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state) {
case CBCentralManagerStatePoweredOn:
[self updateLog:@"蓝牙已打开,请扫描外设"];
break;
default:
break;
}
}
调用
[_manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
扫描外设蓝牙。
//查到外设后,停止扫描,连接设备
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
[self updateLog:[NSString stringWithFormat:@"已发现 peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData]];
_peripheral = peripheral;
NSLog(@"%@",_peripheral);
//停止扫描,在这里可根据peripheral.UUID去链接想要的蓝牙设备
[self.manager stopScan];
//[_activity stopAnimating];
BOOL replace = NO;
// Match if we have this device from before
for (int i=0; i < _nDevices.count; i++) {
CBPeripheral *p = [_nDevices objectAtIndex:i];
if ([p isEqual:peripheral]) {
[_nDevices replaceObjectAtIndex:i withObject:peripheral];
replace = YES;
}
}
if (!replace) {
[_nDevices addObject:peripheral];
[_deviceTable reloadData];
}
}
_deviceTable可显示多个蓝牙设备。
使用
[self.manager connectPeripheral:_peripheral options:nil];
[self.manager cancelPeripheralConnection:_peripheral];
//连接外设成功,开始发现服务
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
[self updateLog:[NSString stringWithFormat:@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.UUID]];
[self.peripheral setDelegate:self];
[self.peripheral discoverServices:nil];
[self updateLog:@"扫描服务"];
}
//连接外设失败
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"%@",error);
}
当rssi发生变化时
-(void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error
{
//NSLog(@"%s,%@",__PRETTY_FUNCTION__,peripheral);
int rssi = abs([peripheral.RSSI intValue]);
CGFloat ci = (rssi - 49) / (10 * 4.);
NSString *length = [NSString stringWithFormat:@"发现BLT4.0热点:%@,距离:%.1fm",_peripheral,pow(10,ci)];
NSLog(@"距离:%@",length);
}
//已发现服务
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
[self updateLog:@"发现服务."];
int i=0;
for (CBService *s in peripheral.services) {
[self.nServices addObject:s];
}
for (CBService *s in peripheral.services) {
[self updateLog:[NSString stringWithFormat:@"%d :服务 UUID: %@(%@)",i,s.UUID.data,s.UUID]];
i++;
[peripheral discoverCharacteristics:nil forService:s];
}
}
//已搜索到Characteristics
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
[self updateLog:[NSString stringWithFormat:@"发现特征的服务:%@ (%@)",service.UUID.data ,service.UUID]];
for (CBCharacteristic *c in service.characteristics) {
[self updateLog:[NSString stringWithFormat:@"特征 UUID: %@ (%@)",c.UUID.data,c.UUID]];
if ([c.UUID isEqual:[CBUUID UUIDWithString:@"2A06"]]) {
_writeCharacteristic = c;
}
if ([c.UUID isEqual:[CBUUID UUIDWithString:@"2A19"]]) {
[_peripheral readValueForCharacteristic:c];
}
if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FFA1"]]) {
[_peripheral readRSSI];
}
[_nCharacteristics addObject:c];
}
}
//获取外设发来的数据,不论是read和notify,获取数据都是从这个方法中读取。
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
// BOOL isSaveSuccess;
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A19"]]) {
NSString *value = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
_batteryValue = [value floatValue];
NSLog(@"电量%f",_batteryValue);
}
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFA1"]]) {
NSString *value = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
//_batteryValue = [value floatValue];
NSLog(@"信号%@",value);
}
else
NSLog(@"didUpdateValueForCharacteristic%@",[[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]);
}