BlueTooth作为center使用总结

摘要:
建立centerManagermyCentralManager=[[CBCentralManageralloc]initWithDelegate:selfqueue:nil]扫描外部设备1.开始扫描:[myCentralManagerscanForPeripheralsWithServices:nil];2.扫描到设备:-centralManager:centraldDiscoveryPeripheral:peripheraladvertisementData:pertisementdataRSSI:RSSI,其中外设包括标识符、名称、isConnected和状态。您可以保存此外围设备,或者只保存其标识符,然后在必要时使retrievePeripheralsWithIdentifiers获取外围设备。此时,外围设备可能不包括其服务。在此处启动设备发现服务-centralManager:centraldidConnectPeripheral:peripheral{[peripheral discoverServices:nil];}3.发现设备的服务后的回调。服务存储在外围设备中。在服务中,您可以遍历所有服务器,并通过UUID找到所需的服务器。然后搜索服务器的特征特征:peripheraldiscoverServices:error{NSArray*services=peripheral.services;CBService*service=services[0];[peripheraldiscoverCharacteristics:nilforService:services[0]];}4.找到服务特征后的回调。

概述

每个外部蓝牙设备有自己的UUID。

每个蓝牙设备可能有多个server,每个server可能有多个characteristic,每个server和characteristic都有自己的UUID。

建立centerManager

myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]

扫描外部设备

1.开始扫描:

[myCentralManager scanForPeripheralsWithServices:nil options:nil];

2.扫描到设备:

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

其中,peripheral中包括identifier、name、isConnected和state。可以保存此peripheral,也可以只保存它的identifier,然后在需要时使retrievePeripheralsWithIdentifiers获取peripheral。此时peripheral很可能还不包含它的services。

advertisementData包括一些其他信息,设备自定义的广播信息也可以在其中:

NSData *data = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];

3.结束扫描:

[myCentralManager stopScan];

连接设备

1.启动连接设备

[mTsInstance.centralManager connectPeripheral:mThisPeripheral options:nil];

 2.连接成功回调。在这里启动搜寻设备的服务

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [peripheral discoverServices:nil];
}

 3.发现设备的服务后回调。服务存放在peripheral.services中,可以遍历所有server,通过UUID找到自己需要的server。然后搜寻该server的characteristics

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    NSArray *services = peripheral.services;
    CBService *service = services[0];
    
    [peripheral discoverCharacteristics:nil forService:services[0]];
}

 4.发现服务的特征后回调。特征放在service.characteristics中,可以遍历所有的characteristics,通过UUID找到自己需要的特征。并保存该特征以便读写等操作。 

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
    for (CBCharacteristic *ch in service.characteristics)
    {
        if ([ch.UUID isEqual:[CBUUID UUIDWithString:@"XXXXX"]])
        {
            //save the characteristic
        }
    }
}

与设备通信-读写

- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type

免责声明:文章转载自《BlueTooth作为center使用总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇applicationContext.xml 配置文件的存放位置WebSocket-java实现下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

aspnetcore源码学习(一)

---恢复内容开始--- 笔者从事netcore相关项目开发已经大半年了,从netcore 1.0到现在3.0大概经过了3年左右的时间,记得netcore刚出来的时候国内相关的学习资料缺乏,限制于外语不大熟练的限制国外的相关书籍看起来相当吃力,于是在当当网上买了一本价值70多的入门书籍,买回来却发现内容都是挂羊头卖狗肉,深深地鄙视这些为了赚钱不顾内容的作者...

ASP.NET CORE 学习之原生DI实现批量注册

以前使用Autofac的时候,只需一句AsImplementInterfaces()就可以很轻松实现批量注册功能。而asp.net core内置的DI框架没有现成的批量注册方法,考虑到替换Autofac框架过程有些繁琐,于是自己写扩展实现了一个简易的原生DI批量注册功能 Startup.cs扩展 1 public static classStartUpEx...

.Net Core使用AspNetCoreRateLimit实现限流

1.添加Nuget程序包  2.在start up中进行配置【有两种模式可选 存储在redis中会哦这内存中】 // 需要从appsettings.json中加载配置 services.AddOptions(); // 存储IP计数器在内存中 services.AddMemo...

从零开始实现ASP.NET Core MVC的插件式开发(九)

标题:从零开始实现ASP.NET Core MVC的插件式开发(九) - 升级.NET 5及启用预编译视图 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/13992077.html 源代码:https://github.com/lamondlu/Mystique 适用版本:.NET Core 3.1, ....

ISR吞吐性能问题

ISR大致可以分几类: Cisco 860、880、890 ISR1800 (fixed)、1800 (modular)、2800、3800 Series ISR1900、2900、3800、3900 Series ISR4K 每一代的设备,设备的性能肯定都不一样,本摘要,将主要记录ISR的吞吐性能问题。 1、下图主要体现的是800 Series和19...

IOptions、IOptionsMonitor、IOptionsSnapshot的区别

背景 ASP.NET Core引入了Options模式,使用类来表示相关的设置组。简单的来说,就是用强类型的类来表达配置项,这带来了很多好处。初学者会发现这个框架有3个主要的面向消费者的接口:IOptions<TOptions>、IOptionsMonitor<TOptions>以及IOptionsSnapshot<TOpt...