久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

iOS 藍(lán)牙使用小結(jié) bluetooth

 睜開(kāi)眼就變帥 2017-01-05

demo下載 http://download.csdn.net/detail/swibyn/9717588

直接看代碼 http://blog.csdn.net/swibyn/article/details/53785249

首先推薦去看官方文檔哦


現(xiàn)將創(chuàng)建藍(lán)牙工程的要點(diǎn)總結(jié)一下,,由于工程主要涉及中心模式,所以只總結(jié)中心模式的用法

1,引入CoreBluetooth.framework

2,實(shí)現(xiàn)藍(lán)牙協(xié)議,,如:

.h文件如下

@protocolCBCentralManagerDelegate;

@protocolCBPeripheralDelegate;


@interface ViewController :UIViewController <CBCentralManagerDelegate,CBPeripheralDelegate>


.m文件如下

#import "CoreBluetooth/CoreBluetooth.h"

另外還有代理部分請(qǐng)自行添加


3,,下面是使藍(lán)牙動(dòng)起來(lái)的過(guò)程

3.1創(chuàng)建CBCentralManager實(shí)例

    self.cbCentralMgr = [[CBCentralManager allocinitWithDelegate:self queue:nil];

設(shè)置代理,,比如:

    self.cbCentralMgr.delegate =self;


創(chuàng)建數(shù)組管理外設(shè)

    self.peripheralArray = [NSMutableArrayarray];


3.2掃描周圍的藍(lán)牙

實(shí)際上周圍的藍(lán)牙如果可被發(fā)現(xiàn),則會(huì)一直往外發(fā)送廣告消息,,中心設(shè)備就是通過(guò)接收這些消息來(lái)發(fā)現(xiàn)周圍的藍(lán)牙的


    [self.cbCentralMgr scanForPeripheralsWithServices:nil options:nil];


3.3發(fā)現(xiàn)一個(gè)藍(lán)牙設(shè)備

也就是收到了一個(gè)周圍的藍(lán)牙發(fā)來(lái)的廣告信息,,這是CBCentralManager會(huì)通知代理來(lái)處理

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

{

}

如果周圍的藍(lán)牙有多個(gè),則這個(gè)方法會(huì)被調(diào)用多次,,你可以通過(guò)tableView或其他的控件把這些周圍的藍(lán)牙的信息打印出來(lái)

3.4連接一個(gè)藍(lán)牙

[self.cbCentralMgrconnectPeripheral:peripheral options:nil];

一個(gè)中心設(shè)備可以同時(shí)連接多個(gè)周圍的藍(lán)牙設(shè)備

當(dāng)連接上某個(gè)藍(lán)牙之后,,CBCentralManager會(huì)通知代理處理


- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

{

}


因?yàn)樵诤竺嫖覀円獜耐庠O(shè)藍(lán)牙那邊再獲取一些信息,并與之通訊,,這些過(guò)程會(huì)有一些事件可能要處理,,所以要給這個(gè)外設(shè)設(shè)置代理,比如:

peripheral.delegate =self;

3.5查詢藍(lán)牙服務(wù)

[peripheral discoverServices:nil];

返回的藍(lán)牙服務(wù)通知通過(guò)代理實(shí)現(xiàn)

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

{


   for (CBService* service in peripheral.services){

        

    }

}

3.6查詢服務(wù)所帶的特征值

[peripheral discoverCharacteristics:nil forService:service];

返回的藍(lán)牙特征值通知通過(guò)代理實(shí)現(xiàn)

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

{

    

   for (CBCharacteristic * characteristic in service.characteristics) {

    }

}

3.7給藍(lán)牙發(fā)數(shù)據(jù)

[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

這時(shí)還會(huì)觸發(fā)一個(gè)代理事件

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

{

}

3.8處理藍(lán)牙發(fā)過(guò)來(lái)的數(shù)據(jù)

一種方法是主動(dòng)讀取數(shù)據(jù),,不過(guò)更好的辦法是設(shè)置事件通知。

[peripheral setNotifyValue:YES forCharacteristic:characteristic];

這樣當(dāng)有數(shù)據(jù)時(shí)會(huì)自動(dòng)觸發(fā)代理事件

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

{

}


3.9 retrievePeripheralsWithIdentifiers使用例子

-(IBAction) Retrieve:(id)Sender

{

    [self.tvLogsetText:@""];

    NSMutableArray * Identifiers = [NSMutableArrayarray];

   for (CBPeripheral * peripheralinself.peripheralArray) {

        [IdentifiersaddObject:peripheral.identifier];

    }


    [selfaddLog:@"[self.cbCentralMgr retrievePeripheralsWithIdentifiers:self.PeripheralIdentifiers]"];

    self.retrievePeripherals = [self.cbCentralMgrretrievePeripheralsWithIdentifiers:Identifiers];

   for (CBPeripheral* peripheralinself.retrievePeripherals) {

        [selfaddLog:[NSStringstringWithFormat:@"%@ name:%@",peripheral,peripheral.name]];

    }

    [self.tableViewPeripheralreloadData];

}


3.10 retrieveConnectedPeripheralsWithServices使用例子


-(IBAction) Retrieve:(id)Sender

{

    [self.tvLogsetText:@""];

    NSMutableArray * services = [NSMutableArrayarray];

   for (CBPeripheral * peripheralinself.peripheralArray) {

       if (peripheral.isConnected) {

           for (CBService *servicein peripheral.services) {

                [servicesaddObject:service.UUID];

            }

        }

    }

    

    [selfaddLog:@"[self.cbCentralMgr retrieveConnectedPeripheralsWithServices:peripheral.services]"];

    self.retrievePeripherals = [self.cbCentralMgrretrieveConnectedPeripheralsWithServices:services];

   for (CBPeripheral* peripheralinself.retrievePeripherals) {

        [selfaddLog:[NSStringstringWithFormat:@"%@ name:%@",peripheral,peripheral.name]];

    }

    [self.tableViewPeripheralreloadData];

}




大概就這個(gè)個(gè)流程,例子中的參數(shù)設(shè)置,,及其其他的一些代理請(qǐng)自己研究,。

















    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多