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

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

bluetooth藍牙連接

2019-11-09 17:37:27
字體:
來源:轉載
供稿:網友
#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];    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙游县| 柘城县| 渝北区| 巴塘县| 阳曲县| 临泽县| 连州市| 清原| 保康县| 西林县| 砚山县| 陆良县| 犍为县| 岳西县| 平陆县| 衡山县| 调兵山市| 肃北| 丰城市| 万盛区| 长武县| 施秉县| 大洼县| 普陀区| 樟树市| 洛宁县| 乐山市| 焦作市| 宁阳县| 团风县| 长春市| 安国市| 子长县| 威信县| 于都县| 读书| 肇东市| 浑源县| 桑日县| 四川省| 革吉县|