国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

bluetooth藍牙連接

2019-11-09 16:34:20
字體:
來源:轉載
供稿:網友
#import <CoreBluetooth/CoreBluetooth.h>@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>@PRoperty(nonatomic,strong)NSMutableArray *peripherals;@property(nonatomic,strong)CBCentralManager *cmgr;@end@implementation ViewController- (NSMutableArray *)peripherals{    if (!_peripherals) {        _peripherals = [NSMutableArray array];    }    return _peripherals;}- (void)viewDidLoad {    [super viewDidLoad];    // 1. 創建中心管家,并且設置代理    self.cmgr = [[CBCentralManager alloc]initWithDelegate:self queue:nil];    // 2. 掃描外部設備    /**     *  scanForPeripheralsWithServices :如果傳入指定的數組,那么就只會掃描數組中對應ID的設備     *                                   如果傳入nil,那么就是掃描所有可以發現的設備     *  掃描完外部設備就會通知CBCentralManager的代理     */    if ([self.cmgr state]== CBCentralManagerStatePoweredOn) {        [self.cmgr scanForPeripheralsWithServices:nil options:0];    }}- (void)centralManagerDidUpdateState:(CBCentralManager *)central{    NSLog(@"%s %d",__func__,__LINE__);}#pragma mark - CBCentralManagerDelegate/** *  發現外部設備,每發現一個就會調用這個方法 *  所以可以使用一個數組來存儲每次掃描完成的數組 */- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData rssI:(NSNumber *)RSSI{    NSLog(@"%s %d",__func__,__LINE__);    // 有可能會導致重復添加掃描到的外設    // 所以需要先判斷數組中是否包含這個外設    if(![self.peripherals containsObject:peripheral]){        [self.peripherals addObject:peripheral];    }}/** *  連接外設成功調用 */- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{    NSLog(@"%s %d",__func__,__LINE__);    // 查找外設服務    [peripheral discoverServices:nil];}/** *  模擬開始連接方法 */- (void)start{    NSLog(@"%s %d",__func__,__LINE__);    // 3. 連接外設    for (CBPeripheral *ppl in self.peripherals) {        // 掃描外設的服務        // 這個操作應該交給外設的代理方法來做        // 設置代理        ppl.delegate = self;        [self.cmgr connectPeripheral:ppl options:nil];    }}#pragma mark - CBPeripheralDelegate/** *  發現服務就會調用代理方法 * *  @param peripheral 外設 */- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{    NSLog(@"%s %d",__func__,__LINE__);    // 掃描到設備的所有服務    NSArray *services = peripheral.services;    // 根據服務再次掃描每個服務對應的特征    for (CBService *ses in services) {        [peripheral discoverCharacteristics:nil forService:ses];    }}/** *  發現服務對應的特征 */- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{    NSLog(@"%s %d",__func__,__LINE__);    // 服務對應的特征    NSArray *ctcs = service.characteristics;    // 遍歷所有的特征    for (CBCharacteristic *character in ctcs) {        // 根據特征的唯一標示過濾        if ([character.UUID.UUIDString isEqualToString:@"XMG"]) {            NSLog(@"可以吃飯了");        }    }}/** *  斷開連接 */- (void)stop{    NSLog(@"%s %d",__func__,__LINE__);    // 斷開所有連接上的外設    for (CBPeripheral *per in self.peripherals) {        [self.cmgr cancelPeripheralConnection:per];    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 廊坊市| 清镇市| 崇信县| 青浦区| 三原县| 徐汇区| 深泽县| 呼伦贝尔市| 射洪县| 水富县| 泸西县| 石渠县| 临高县| 临武县| 宜川县| 吴旗县| 高密市| 平谷区| 平远县| 洪泽县| 隆德县| 嘉禾县| 青海省| 浦城县| 元阳县| 榆中县| 岢岚县| 四川省| 丽江市| 牙克石市| 玛曲县| 太保市| 金昌市| 墨竹工卡县| 铜陵市| 抚松县| 罗定市| 大港区| 灵寿县| 汾阳市| 房山区|