ios|ios corebluetooth蓝牙4.0事例
【ios|ios corebluetooth蓝牙4.0事例】转载:http://www.cocoachina.com/bbs/read.php?tid=162212
蓝牙4.0 不需要配对。
每个device 有UUID来标识。
CBCentralManager 蓝牙管理
CBPeripheral 蓝牙外设 CFUUIDRef 标识
CBService CBUUID 标识
CBCharacteristic 特征(例如蓝牙里面点亮led、打开蜂鸣等) CBUUID 标识
// scaning........
- (IBAction)scanBle:(id)sender {
UIButton *btn = (UIButton *)sender;
[_centralManager scanForPeripheralsWithServices:nil options:0];
[btn setTitle:@"scaning..." forState:UIControlStateNormal];
}
// 选取符合的蓝牙
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// if (RSSI.integerValue > -15) {
// NSLog(@"信号太强,好像不对");
// return;
// }
// if (RSSI.integerValue < -35) {
// NSLog(@"信号太弱了,离近点再试");
// return;
// }
if (self.discoveredPeripheral != peripheral
&& [[self UUIDToString:peripheral.UUID] isEqualToString:TRANSFER_PERIPHERAL_UUID]) {
self.discoveredPeripheral = peripheral;
self.discoveredPeripheral.delegate = self;
[_btnScan setTitle:peripheral.name forState:UIControlStateNormal];
}
}
// connect the ble
- (IBAction)connectTheBle:(id)sender {
UIButton *btn = (UIButton *)sender;
[btn setTitle:@"connecting" forState:UIControlStateNormal];
[self.centralManager connectPeripheral:self.discoveredPeripheral options:nil];
}
// 蜂鸣指令
- (IBAction)btnSoundPressed:(id)sender {
Byte buzVal = 0x04;
NSData *data = https://www.it610.com/article/[[NSData alloc] initWithBytes:&buzVal length:1];
CBCharacteristic *characteristic = nil;
for (CBService *service in self.discoveredPeripheral.services) {
NSLog(@"--------service = %s ------nn",[self CBUUIDToString:service.UUID]);
const char *CServieUUID = [self CBUUIDToString:service.UUID];
const char *CSoundUUID = [TRANSFER_SERVICE_SOUND_UUID cStringUsingEncoding:NSASCIIStringEncoding];
int n = strcmp(CServieUUID, CSoundUUID);
if (n == 0) {
for (CBCharacteristic *cha in service.characteristics) {
NSLog(@"--------characteristic = %s ------nn",[self CBUUIDToString:cha.UUID]);
if (strcmp(,) == 0) {
characteristic = cha;
break;
}
}
break;
}
}
[self.discoveredPeripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}
以上为蓝牙4.0 外设和 iphone的交互。
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- Android|Android BLE蓝牙连接异常处理
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密
- iOS,打Framework静态库