IOS 蓝牙与外设交互
来源:互联网 发布:php 5.3.27.tar.gz 编辑:程序博客网 时间:2024/05/01 18:37
1, 使用
CBCentralManager * _manager = [[CBCentralManageralloc] initWithDelegate:selfqueue:nil];
初始化一个中央设备(_manager);
判断手机是否开启了蓝牙只有central.state=CBCentralManagerStatePoweredOn 才能去链接蓝牙设备
//开始查看服务,蓝牙开启
-(void)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch (central.state) {
caseCBCentralManagerStatePoweredOn:
[selfupdateLog:@"蓝牙已打开,请扫描外设"];
break;
default:
break;
}
}
调用
[_managerscanForPeripheralsWithServices:niloptions:@{CBCentralManagerScanOptionAllowDuplicatesKey :@YES }];
扫描外设蓝牙。
//查到外设后,停止扫描,连接设备
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
[selfupdateLog:[NSStringstringWithFormat:@"已发现 peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData]];
_peripheral = peripheral;
NSLog(@"%@",_peripheral);
//停止扫描,在这里可根据peripheral.UUID去链接想要的蓝牙设备
[self.managerstopScan];
//[_activity stopAnimating];
BOOL replace = NO;
// Match if we have this device from before
for (int i=0; i <_nDevices.count; i++) {
CBPeripheral *p = [_nDevicesobjectAtIndex:i];
if ([p isEqual:peripheral]) {
[_nDevicesreplaceObjectAtIndex:i withObject:peripheral];
replace =YES;
}
}
if (!replace) {
[_nDevicesaddObject:peripheral];
[_deviceTablereloadData];
}
}
_deviceTable可显示多个蓝牙设备。
使用
[self.managerconnectPeripheral:_peripheraloptions:nil];
[self.managercancelPeripheralConnection:_peripheral];
//连接外设成功,开始发现服务
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
[selfupdateLog:[NSStringstringWithFormat:@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.UUID]];
[self.peripheralsetDelegate:self];
[self.peripheraldiscoverServices:nil];
[selfupdateLog:@"扫描服务"];
}
//连接外设失败
-(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.RSSIintValue]);
CGFloat ci = (rssi - 49) / (10 * 4.);
NSString *length = [NSStringstringWithFormat:@"发现BLT4.0热点:%@,距离:%.1fm",_peripheral,pow(10,ci)];
NSLog(@"距离:%@",length);
}
//已发现服务
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
[selfupdateLog:@"发现服务."];
int i=0;
for (CBService *sin peripheral.services) {
[self.nServicesaddObject:s];
}
for (CBService *sin peripheral.services) {
[selfupdateLog:[NSStringstringWithFormat:@"%d :服务 UUID: %@(%@)",i,s.UUID.data,s.UUID]];
i++;
[peripheral discoverCharacteristics:nilforService:s];
}
}
//已搜索到Characteristics
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
[selfupdateLog:[NSStringstringWithFormat:@"发现特征的服务:%@ (%@)",service.UUID.data ,service.UUID]];
for (CBCharacteristic *cin service.characteristics) {
[selfupdateLog:[NSStringstringWithFormat:@"特征 UUID: %@ (%@)",c.UUID.data,c.UUID]];
if ([c.UUIDisEqual:[CBUUIDUUIDWithString:@"2A06"]]) {
_writeCharacteristic = c;
}
if ([c.UUIDisEqual:[CBUUIDUUIDWithString:@"2A19"]]) {
[_peripheralreadValueForCharacteristic:c];
}
if ([c.UUIDisEqual:[CBUUIDUUIDWithString:@"FFA1"]]) {
[_peripheralreadRSSI];
}
[_nCharacteristicsaddObject:c];
}
}
//获取外设发来的数据,不论是read和notify,获取数据都是从这个方法中读取。
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
// BOOL isSaveSuccess;
if ([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"2A19"]]) {
NSString *value = [[NSStringalloc]initWithData:characteristic.valueencoding:NSUTF8StringEncoding];
_batteryValue = [value floatValue];
NSLog(@"电量%f",_batteryValue);
}
if ([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"FFA1"]]) {
NSString *value = [[NSStringalloc]initWithData:characteristic.valueencoding:NSUTF8StringEncoding];
//_batteryValue = [value floatValue];
NSLog(@"信号%@",value);
}
else
NSLog(@"didUpdateValueForCharacteristic%@",[[NSStringalloc] initWithData:characteristic.valueencoding:NSUTF8StringEncoding]);
}
- IOS 蓝牙与外设交互
- 蓝牙与外设交互(防丢器)
- BLE 蓝牙 与APP 得交互 内设 外设
- iOS蓝牙连接外设
- iOS蓝牙4.0开发(一)与外设通讯
- IOS蓝牙主动断开外设的问题
- iOS 蓝牙外设OTA升级记录
- iOS开发—蓝牙4.0(BLE)与外设连接及收发数据的流程
- BluetoothHDP 蓝牙与外设的通信
- 【转载】CPU如何与外设交互工作
- 外设与存储器交互方式总结
- iOS 浅析蓝牙交互之蓝牙客户端
- ios编程之蓝牙交互
- iOS蓝牙开发入门06--demo的外设代码释义
- iOS 蓝牙外设检索、发送、接受数据问题。
- iOS 蓝牙开发 连接外设的代码实现(二)
- (二)iOS Core Bluetooth 蓝牙 “外设模式”
- Android与PC蓝牙交互
- cocos setup.py 主要是用它配置项目环境
- 字库管理程序源码
- ACdream 神奇的%系列一 (素数筛)
- poj-1061
- 设计模式--门面模式(tomcat中门面模式使用很多,Socket也是一个门面模式)
- IOS 蓝牙与外设交互
- Jmeter测试报表相关参数说明
- jQuery幻灯片插件Skippr
- Reading the contents of a file using POSIX functions
- 一个奇怪的事情,和自己的推理
- apache2 + mysql + thinkphp
- 在C/C++程序中打印当前函数调用栈
- 嵌入式软件测试的十大方法《转》
- Qt Model/View 学习笔记 (六)