日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > ios藍牙開發(三)app作為外設被連接的實現

ios藍牙開發(三)app作為外設被連接的實現

來源:程序員人生   發布時間:2017-02-20 09:48:07 閱讀次數:5154次

再上1節說了app作為central連接peripheral的情況,這1節介紹如何使用app發布1個peripheral,給其他的central連接


還是這張圖,central模式用的都是左側的類,而peripheral模式用的是右側的類

peripheral模式的流程


1. 打開peripheralManager,設置peripheralManager的拜托
2. 創建characteristics,characteristics的description 創建service,把characteristics添加到service中,再把service添加到peripheralManager中
3. 開啟廣播advertising
4. 對central的操作進行響應
    - 4.1 讀characteristics要求
    - 4.2 寫characteristics要求
    - 4.4 定閱和取消定閱characteristics

準備環境

  1 xcode
  2 開發證書和手機(藍牙程序需要使用使用真機調試,使用摹擬器也能夠調試,但是方法很蛋疼,我會放在最后說),如果不行可使用osx程序調試
  3 藍牙外設

實現步驟

1. 打開peripheralManager,設置peripheralManager的拜托

設置當前ViewController實現CBPeripheralManagerDelegate拜托

    @interface BePeripheralViewController : UIViewController<CBPeripheralManagerDelegate>

初始化peripheralManager

     /*
     和CBCentralManager類似,藍牙裝備打開需要1定時間,打開成功后會進入拜托方法
     - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;
     摹擬器永久也不會得CBPeripheralManagerStatePoweredOn狀態
     */
    peripheralManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];

2. 創建characteristics,characteristics的description ,創建service,把characteristics添加到service中,再把service添加到peripheralManager中

在拜托方法 - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral中,當peripheral成功打開后,才可以配置service和characteristics。這里創建的service和chara對象是CBMutableCharacteristic和CBMutableService。他們的區分就像NSArray和NSMutableArray區分類似。我們先創建characteristics和description,description是characteristics的描寫,描寫分很多種,這里不細說了,經常使用的就是CBUUIDCharacteristicUserDescriptionString。

//peripheralManager狀態改變
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    switch (peripheral.state) {
            //在這里判斷藍牙設別的狀態  當開啟了則可調用  setUp方法(自定義)
        case CBPeripheralManagerStatePoweredOn:
            NSLog(@"powered on");
            [info setText:[NSString stringWithFormat:@"裝備名%@已打開,可使用center進行連接",LocalNameKey]];
            [self setUp];
            break;
        case CBPeripheralManagerStatePoweredOff:
            NSLog(@"powered off");
            [info setText:@"powered off"];
            break;

        default:
            break;
    }
}
 //配置bluetooch的
 -(void)setUp{

        //characteristics字段描寫
        CBUUID *CBUUIDCharacteristicUserDescriptionStringUUID = [CBUUID UUIDWithString:CBUUIDCharacteristicUserDescriptionString];

        /*
         可以通知的Characteristic
         properties:CBCharacteristicPropertyNotify
         permissions CBAttributePermissionsReadable
         */
        CBMutableCharacteristic *notiyCharacteristic = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:notiyCharacteristicUUID] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

        /*
         可讀寫的characteristics
         properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead
         permissions CBAttributePermissionsReadable | CBAttributePermissionsWriteable
         */
        CBMutableCharacteristic *readwriteCharacteristic = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:readwriteCharacteristicUUID] properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable | CBAttributePermissionsWriteable];
        //設置description
        CBMutableDescriptor *readwriteCharacteristicDescription1 = [[CBMutableDescriptor alloc]initWithType: CBUUIDCharacteristicUserDescriptionStringUUID value:@"name"];
        [readwriteCharacteristic setDescriptors:@[readwriteCharacteristicDescription1]];


        /*
         只讀的Characteristic
         properties:CBCharacteristicPropertyRead
         permissions CBAttributePermissionsReadable
         */
        CBMutableCharacteristic *readCharacteristic = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:readCharacteristicUUID] properties:CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable];


        //service1初始化并加入兩個characteristics
        CBMutableService *service1 = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:ServiceUUID1] primary:YES];
        [service1 setCharacteristics:@[notiyCharacteristic,readwriteCharacteristic]];

        //service2初始化并加入1個characteristics
        CBMutableService *service2 = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:ServiceUUID2] primary:YES];
        [service2 setCharacteristics:@[readCharacteristic]];

        //添加后就會調用代理的- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
        [peripheralManager addService:service1];
        [peripheralManager addService:service2];
 }

3. 開啟廣播advertising

//perihpheral添加了service
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{
    if (error == nil) {
        serviceNum++;
    }

    //由于我們添加了2個服務,所以想兩次都添加完成后才去發送廣播
    if (serviceNum==2) {
        //添加服務后可以在此向外界發出通告 調用完這個方法后會調用代理的
        //(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
        [peripheralManager startAdvertising:@{
                                              CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:ServiceUUID1],[CBUUID UUIDWithString:ServiceUUID2]],
                                              CBAdvertisementDataLocalNameKey : LocalNameKey
                                             }
         ];

    }

}

//peripheral開始發送advertising
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{
    NSLog(@"in peripheralManagerDidStartAdvertisiong");
}

4. 對central的操作進行響應

- 4.1 讀characteristics要求
- 4.2 寫characteristics要求
- 4.3 定閱和取消定閱characteristics
//定閱characteristics
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic{
    NSLog(@"定閱了 %@的數據",characteristic.UUID);
    //每秒履行1次給主裝備發送1個當前時間的秒數
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sendData:) userInfo:characteristic  repeats:YES];
}

//取消定閱characteristics
-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic{
    NSLog(@"取消定閱 %@的數據",characteristic.UUID);
    //取消回應
    [timer invalidate];
}

//發送數據,發送當前時間的秒數
-(BOOL)sendData:(NSTimer *)t {
    CBMutableCharacteristic *characteristic = t.userInfo;
    NSDateFormatter *dft = [[NSDateFormatter alloc]init];
    [dft setDateFormat:@"ss"];
    NSLog(@"%@",[dft stringFromDate:[NSDate date]]);

    //履行回應Central通知數據
    return  [peripheralManager updateValue:[[dft stringFromDate:[NSDate date]] dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:nil];

}


//讀characteristics要求
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request{
    NSLog(@"didReceiveReadRequest");
    //判斷是不是有讀數據的權限
    if (request.characteristic.properties & CBCharacteristicPropertyRead) {
        NSData *data = request.characteristic.value;
        [request setValue:data];
        //對要求作出成功響應
        [peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
    }else{
        [peripheralManager respondToRequest:request withResult:CBATTErrorWriteNotPermitted];
    }
}


//寫characteristics要求
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests{
    NSLog(@"didReceiveWriteRequests");
    CBATTRequest *request = requests[0];

    //判斷是不是有寫數據的權限
    if (request.characteristic.properties & CBCharacteristicPropertyWrite) {
        //需要轉換成CBMutableCharacteristic對象才能進行寫值
        CBMutableCharacteristic *c =(CBMutableCharacteristic *)request.characteristic;
        c.value = request.value;
        [peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
    }else{
        [peripheralManager respondToRequest:request withResult:CBATTErrorWriteNotPermitted];
    }


}

代碼下載:

我博客中大部份示例代碼都上傳到了github,地址是:https://github.com/coolnameismy/demo,點擊跳轉代碼下載地址

本文代碼寄存目錄是BleDemo

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲人成人一区二区在线观看 | 免费一区区三区四区 | av大帝在线| 天天干天天射综合网 | 三级网站视频 | 日韩成人在线观看 | 91亚洲国产成人久久精品网站 | www日韩| 亚洲精品国产综合区久久久久久久 | 国产精品视频一区二区免费不卡 | 国产一区二区三区免费观看在线 | 亚洲美女一区 | 最新国产网站 | www高清| 18做爰免费视频网站 | 第九色激情 | 亚洲国产成人精品女人久久久 | 亚洲美女视频 | 国产三级在线播放 | 精品无码久久久久久国产 | 亚洲男人网 | 秋霞电影院午夜仑片 | 亚洲一级在线观看 | 亚洲不卡在线 | 国产伦精品一区二区 | 国产精品国产精品国产专区不蜜 | 九九九九九九九伊人 | 精品久久久久久久久久久久久久久 | 久久精品毛片 | 国产精品不卡av | 亚洲aav| 欧美一级欧美三级 | 成人影院免费观看 | 国产欧美日韩在线观看 | 久久精品国产免费 | 特黄一级大片 | 成人在线视频观看 | 日韩中文字幕在线视频 | 国产欧美日韩在线观看 | 国产在线观看免费麻豆 | www日韩|