前期回顧
iOS 藍牙開發(二)
iOS 藍牙開發(三)
iOS 藍牙開發(四)
一、基本概念
- 1.1,中心設備(central):是發起連接的主設備,如:App。
- 1.2,外設(peripheral):被連接的設備稱為外部設備,即外設, 如:體脂稱,健康手環等;
- 1.3,服務(CBService): 外設可以包含一個或多個服務,它是用于實現裝置的功能或特征數據相關聯的行為集合。
- 1.4,特征(CBCharacteristic):每個服務可以對應多個特征,它是提供外設服務進一步的細節。
開始前先記錄運行后的掃描結果
二,藍牙相關實現
在iOS中藍牙相關實現都是在CoreBluetooth這個framework中的,所以我們創建一個單例類中需要先導入#import <CoreBluetooth/CoreBluetooth.h>
,再后即可使用這個單例類進行管理我們藍牙的掃描、連接、狀態等實現。
- 2.1, 初始化藍牙
CBCentralManager
dispatch_queue_t centralQueue = dispatch_queue_create("com.acadsoc.wgazjbuletoothsdk",DISPATCH_QUEUE_SERIAL);
NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey : [NSNumber numberWithBool:YES], CBCentralManagerOptionRestoreIdentifierKey : @"com.hykj.wgazjbuletoothsdk"};
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue options:options];
// self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
self.resultStr = @"";
self.services = [[NSMutableArray alloc]init];
- 2.2,掃描周邊可連接藍牙設備
當我們在掃描周邊外設之前需要判斷藍牙是否可用,在- (void)centralManagerDidUpdateState:(CBCentralManager *)central
函數中進行獲取設備藍牙的更新。
判斷手機藍牙狀態
CBManagerStateUnknown = 0, 未知
CBManagerStateResetting, 重置中
CBManagerStateUnsupported, 不支持
CBManagerStateUnauthorized, 未驗證
CBManagerStatePoweredOff, 未啟動
CBManagerStatePoweredOn, 可用
當central.state
為CBManagerStatePoweredOn即可開始掃描, 具體方法[self.centralManager scanForPeripheralsWithServices:nil options:nil]
當調用scanForPeripheralsWithServices:options:
函數時就會實時調用其代理方法- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if (self.stateUpdateBlock) {
self.stateUpdateBlock(central.state);
}
switch (central.state)
{
case CBManagerStatePoweredOn:
NSLog(@"藍牙已開啟");
// 根據SERVICE_UUID來掃描外設,如果不設置SERVICE_UUID,則掃描所有藍牙設備
// 第一個參數為CBUUID的數組,需要搜索特點服務的藍牙設備,只要每搜索到一個符合條件的藍牙設備都會調用didDiscoverPeripheral代理方法
[self startScan];
break;
case CBManagerStatePoweredOff:
NSLog(@"藍牙已關閉, 請打開藍牙");
break;
case CBManagerStateUnsupported:
NSLog(@"設備不支持藍牙");
break;
case CBManagerStateUnauthorized:
NSLog(@"應用尚未被授權使用藍牙");
break;
case CBManagerStateUnknown:
NSLog(@"未知錯誤,請重新開啟藍牙");
break;
case CBManagerStateResetting:
NSLog(@"藍牙重置中");
[self stopScan];
break;
default:
NSLog(@"Central Manager did change state");
break;
}
}
// 開始掃描周圍可用藍牙
- (void)startScan {
//不重復掃描已發現設備
//CBCentralManagerScanOptionAllowDuplicatesKey設置為NO表示不重復掃瞄已發現設備,為YES就是允許。
//CBCentralManagerOptionShowPowerAlertKey設置為YES就是在藍牙未打開的時候顯示彈框
NSDictionary *option = @{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO],CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:YES]};
// 第一個參數填nil代表掃描所有藍牙設備,第二個參數options也可以寫nil
[self.centralManager scanForPeripheralsWithServices:nil options:option];
}
// 掃描外設的代理方法,在該函數中揭開篩選出周邊的藍牙外設
// 在藍牙于后臺被殺掉時,重連之后會首先調用此方法,可以獲取藍牙恢復時的各種狀態
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSString *perName = [[NSUserDefaults standardUserDefaults] objectForKey:@"conPeripheral"];
NSString *setOrDelNum = [[NSUserDefaults standardUserDefaults] objectForKey:@"setOrDel"];
// 檢索到設備
if (self.discoverPeripheralBlcok) {
if (self.prefixString.length > 0) {
if ([peripheral.name hasPrefix:self.prefixString]) {
self.discoverPeripheralBlcok(central,peripheral,advertisementData,RSSI);
}
} else {
self.discoverPeripheralBlcok(central,peripheral,advertisementData,RSSI);
}
[self startScan];
}
if ([setOrDelNum isEqualToString:@"0"]) {
//有自動重連的設備
[[NSNotificationCenter defaultCenter]postNotificationName:PostAutoConnectionNotificaiton object:nil];
if ([peripheral.name isEqualToString:perName]) {
self.peripheral = peripheral;
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
}
peripheral
是外設類advertisementData
是廣播的值,一般攜帶設備名,serviceUUID
等信息。RSSI
絕對值越大,表示信號越差,設備離的越遠。如果想裝換成百分比強度,(RSSI+100)/1001
(這是一個約數,藍牙信號值并不一定是-100 - 0的值)
3,連接
藍牙的連接是當中心設備掃描到可用外設后, 利用函數[self.centralManager connectPeripheral:peripheral options:nil];
進行鏈接, 當函數被調用后, 就會回調其對應的代理函數。
- 3.1,鏈接成功后的回調
// 藍牙設備連接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[self.centralManager stopScan];
[self.peripheral setDelegate:self];
[self.peripheral discoverServices:nil];
//延時操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (self.connectSuccessBlock) {
self.connectSuccessBlock(peripheral,nil,self.writeCharacteristic);
} else {
//返回連接成功
if ([self.delegate respondsToSelector:@selector(automaticConnectionWithPerpheral:)]) {
[self.delegate automaticConnectionWithPerpheral:peripheral];
}
}
});
}
- 3.2, 鏈接失敗后的回調
// 連接失敗
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
if (self.connectFailureBlock) {
self.connectFailureBlock(peripheral,error);
}
}
4, 其他函數和代理方法
- 4.1,斷開鏈接
// 斷開藍牙連接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
[self disconectPeripheral];
if (error)
{
NSLog(@">>> didDisconnectPeripheral for %@ with error: %@", peripheral.name, [error localizedDescription]);
}
}
// 斷開鏈接
- (void)disconectPeripheral {
if (self.peripheral) {
[self.centralManager cancelPeripheralConnection:self.peripheral];
}
}
- 4.2,停止掃描
// 停止掃描
- (void)stopScan
{
[self.centralManager stopScan];
}
5,中心管理供給外界使用方法
// 單例
+ (ZJAWGBluetoothManager *)defaultManager;
/**
當前藍牙的狀態
@param state 狀態
*/
- (void)returnBluetoothStateWithBlock:(BTStateUpdateBlock)state;
/**
搜索藍牙外設, 每次回調表示返回一個藍牙外設信息
@param name 模糊搜索設備名稱, 即目標設備名稱包含的字段
@param discoverBlock 搜索到藍牙外設后的回調
*/
- (void)scanPeripheralsWithName:(NSString *)name
discoverPeripheral:(BTDiscoverPeripheralBlock)discoverBlock;
/**
搜索藍牙外設
@param discoverBlock 搜索藍牙外設
*/
- (void)scanPeripheralsWithDiscoverPeripheral:(BTDiscoverPeripheralBlock)discoverBlock;
/**
連接某個藍牙外設,并查詢服務,特性,服務等描述
@param peripheral 要連接的藍牙外設
@param complete 連接成功后的回調
@param failure 連接失敗后的回調
*/
- (void)connectPeripheral:(CBPeripheral *)peripheral
complete:(BTConnectSuccessBlock)complete
failure:(BTConnectFailureBlock)failure;
總結
本篇筆記主要是記錄如何初始化藍牙的CBCentralManager
的中心管理類,并記錄如何實現掃描周邊外設、如何鏈接、獲取藍牙當前狀態。