寫(xiě)給藍(lán)牙開(kāi)發(fā)的初學(xué)者:
這個(gè)需求是前幾天公司提出的,有個(gè)藍(lán)牙設(shè)備需要連接自己的app,作為重來(lái)沒(méi)有接觸過(guò)藍(lán)牙的我也是費(fèi)了好些時(shí)間才摸索出來(lái)的. 首先來(lái)說(shuō)下步驟.
1.app需要搜索到你藍(lán)牙設(shè)備的信息.
2.判斷搜索到的設(shè)備是否是你所需設(shè)備
3.根據(jù)該藍(lán)牙設(shè)備信息讀取,服務(wù)以及特征值(有好多的)
4.根據(jù)讀取的特征以及服務(wù)判斷哪個(gè)是你所需的特征(可以寫(xiě)入的).
5.進(jìn)行交互,根據(jù)特征值寫(xiě)入數(shù)據(jù),然后在回調(diào)中可以接收到藍(lán)牙設(shè)備返回的信息.
然后步入正題:
在此之前需要介紹一個(gè)非常厲害的第三方庫(kù)簡(jiǎn)單方便快捷就可以搞定這一系列的步驟.
< BabyBluetooth> 自己從github上下載
下載地址:https://github.com/coolnameismy/BabyBluetooth
簡(jiǎn)單易用的藍(lán)牙ble庫(kù)怔蚌,基于CoreBluetooth 作者:劉彥瑋
//導(dǎo)入
#import <CoreBluetooth/CoreBluetooth.h>
#import "BabyBluetooth.h"
#pragma mark -初始化藍(lán)牙
-(void)initBLE{
//初始化BabyBluetooth 藍(lán)牙庫(kù)
_baby = [BabyBluetooth shareBabyBluetooth];
//設(shè)置藍(lán)牙委托
[self babyDelegate];
//停止之前的連接
[_baby cancelAllPeripheralsConnection];
//設(shè)置委托后直接可以使用爹土,無(wú)需等待CBCentralManagerStatePoweredOn狀態(tài)。
////查找Peripherals
_baby.scanForPeripherals().begin();
}
#pragma mark -藍(lán)牙配置和操作
-(void)babyDelegate{
__weak typeof(self)weakSelf = self;
//設(shè)備狀態(tài)改變的委托
[_baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) {
if (central.state == CBCentralManagerStatePoweredOn) {
NSLog(@"設(shè)備打開(kāi)成功,開(kāi)始掃描設(shè)備");
}else{
NSLog(@"請(qǐng)打開(kāi)手機(jī)藍(lán)牙");
}
}];
//掃描到設(shè)備
[_baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {
weakSelf.la.text=[NSString stringWithFormat:@"搜索到了設(shè)備:%@",peripheral.name];
[SVProgressHUD showWithStatus:@"正在連接..."];
//連接設(shè)備
weakSelf.baby.having(peripheral).and.channel(channelOnPeropheralView).then.connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
}];
//設(shè)置設(shè)備連接成功的委托,同一個(gè)baby對(duì)象饱溢,使用不同的channel切換委托回調(diào)
[_baby setBlockOnConnectedAtChannel:channelOnPeropheralView block:^(CBCentralManager *central, CBPeripheral *peripheral) {
weakSelf.la.text=[NSString stringWithFormat:@"連接成功"];
[SVProgressHUD showWithStatus:@"藍(lán)牙連接成功"];
}];
//設(shè)置設(shè)備連接失敗的委托
[_baby setBlockOnFailToConnectAtChannel:channelOnPeropheralView block:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
[SVProgressHUD dismiss];
weakSelf.la.text=[NSString stringWithFormat:@"連接失敗請(qǐng)重試!"];
}];
//設(shè)置設(shè)備斷開(kāi)連接的委托
[_baby setBlockOnDisconnectAtChannel:channelOnPeropheralView block:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) {
weakSelf.la.text=[NSString stringWithFormat:@"連接已斷開(kāi)請(qǐng)重試!"];
}];
[_baby setBlockOnReadValueForCharacteristicAtChannel:channelOnPeropheralView block:^(CBPeripheral *peripheral, CBCharacteristic *characteristic, NSError *error) {
if([characteristic.UUID.UUIDString isEqualToString:@"00001524-1212-EFDE-1523-785FEABCD123"]){
[SVProgressHUD showWithStatus:@"正在寫(xiě)入數(shù)據(jù)..."];
if(![characteristic isNotifying]){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"開(kāi)始監(jiān)聽(tīng)");
}
//要寫(xiě)入的數(shù)據(jù)(需要根據(jù)藍(lán)牙協(xié)議來(lái)寫(xiě))
//這里寫(xiě)數(shù)據(jù)可能會(huì)需要先求出校驗(yàn)和checkSUM
//比如:下面的字節(jié)協(xié)議要求最后一位是校驗(yàn)和(0x1a)至于怎么計(jì)算下一篇中我會(huì)貼出公式
Byte byte[] = {0x51,0x26,0x00,0x00,0x00,0x00,0xa3,0x1a};
NSData *data = [NSData dataWithBytes:byte length:sizeof(byte)];
//寫(xiě)入數(shù)據(jù)
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
[weakSelf.baby notify:peripheral characteristic:characteristic block:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) {
[SVProgressHUD showWithStatus:@"正在讀取數(shù)據(jù)..."];
Byte *testByte = (Byte *)[characteristics.value bytes];
if([characteristics.value length]>2){
[SVProgressHUD showWithStatus:@"讀取數(shù)據(jù)成功!"];
//斷開(kāi)所有設(shè)備連接
[weakSelf.baby cancelAllPeripheralsConnection];
}
}];
}
}];
}
點(diǎn)此查看校驗(yàn)和計(jì)算
到此為止就完事了,如果本文有哪里不足和欠缺之處請(qǐng)私信我,或者留言.
歡迎各位前來(lái)切磋,共同進(jìn)步~~