多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

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

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

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

再上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

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 黄色大片日本 | 国产精品久久一区一区 | 手机看片福利盒子 | 国产精品亚洲精品久久成人 | 日本爱爱免费视频 | 手机看片日韩日韩韩 | 非洲黑人最猛性xxxx交 | 国产成人影院一区二区 | 日本亚洲视频 | 免费一级大毛片a一观看不卡 | 久久久久久久久久久大尺度免费视频 | freexxx性韩国| 亚洲欧美精品一区二区 | 国产亚洲精品美女久久久久 | 国产精品自拍一区 | 亚洲精品免费网站 | 99久久精品毛片免费播放 | 国产精品亚洲高清一区二区 | 69久久 | 欧美一区二区丝袜高跟鞋 | 欧美日韩免费看 | freexxx性欧美vide0高清 | 日本中文字幕一区二区有码在线 | 老司机午夜免费福利 | 手机在线一区二区三区 | 欧美高清另类videosbestsex | 国产玖玖在线观看 | 欧美视频一区二区在线观看 | 动漫日本在线免费观看 | h视频无遮挡免费网站 | 国内精品免费视频精选在线观看 | 日本二区免费一片黄2019 | 国内一区二区三区精品视频 | 香港aa三级久久三级不卡 | 九色国产在线 | 欧美高清18 | 亚洲综合在线观看视频 | 欧美18videosex性欧美乱任 | 澳门特级α片免费观看视频 | 一二三四视频在线观看免费高清 | 大陆三级午夜理伦三级三 |