iOS 藍(lán)牙4.0開發(fā)
背景:
1.iOS的藍(lán)牙不能用來傳輸文件甚带。
2.iOS與iOS設(shè)備之間進(jìn)行數(shù)據(jù)通信拇厢,使用gameKit.framework
3.iOS與其他非iOS設(shè)備進(jìn)行數(shù)據(jù)通信喝峦,使用coreBluetooth.framework
iOS中藍(lán)牙的實(shí)現(xiàn)方案
iOS中提供了4個(gè)框架用于實(shí)現(xiàn)藍(lán)牙連接
GameKit.framework(用法簡單)
只能用于iOS設(shè)備之間的連接申屹,多用于游戲(比如五子棋對戰(zhàn))凤价,從iOS7開始過期
MultipeerConnectivity.framework
只能用于iOS設(shè)備之間的連接匙睹,從iOS7開始引入桅狠,主要用于文件共享(僅限于沙盒的文件)
ExternalAccessory.framework
可用于第三方藍(lán)牙設(shè)備交互潮瓶,但是藍(lán)牙設(shè)備必須經(jīng)過蘋果MFi認(rèn)證(國內(nèi)較少)
CoreBluetooth.framework(時(shí)下熱門)
可用于第三方藍(lán)牙設(shè)備交互蚕泽,必須要支持藍(lán)牙4.0
硬件至少是4s晌梨,系統(tǒng)至少是iOS6
藍(lán)牙4.0以低功耗著稱,一般也叫BLE(BluetoothLowEnergy)
目前應(yīng)用比較多的案例:運(yùn)動(dòng)手壞须妻、嵌入式設(shè)備仔蝌、智能家居
下面具體介紹使用CoreBluetooth.framework的代碼步驟:
//藍(lán)牙系統(tǒng)庫
#import <CoreBluetooth/CoreBluetooth.h>
//必須要由UUID來唯一標(biāo)示對應(yīng)的service和characteristic
#define kServiceUUID @"5C476471-1109-4EBE-A826-45B4F9D74FB9"
#define kCharacteristicHeartRateUUID @"82C7AC0F-6113-4EC9-92D1-5EEF44571398"
#define kCharacteristicBodyLocationUUID @"537B5FD6-1889-4041-9C35-F6949D1CA034"
@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property (nonatomic,strong)CBCentralManager * centralManager;
@property (nonatomic,strong)CBPeripheral * peripheral;
@end
1.創(chuàng)建中心角色
#import <CoreBluetooth/CoreBluetooth.h>
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化藍(lán)牙 central manager
_centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) options:nil];
}
2.掃描外設(shè)
[manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerRestoredStateScanOptionsKey:@(YES)}];
3.連接外設(shè)
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
if([peripheral.name isEqualToString:BLE_SERVICE_NAME]){
[self connect:peripheral];
}
s);
}
-(BOOL)connect:(CBPeripheral *)peripheral{
self.manager.delegate = self;
[self.manager connectPeripheral:peripheral
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}
4.掃描外設(shè)中的服務(wù)和特征
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@", peripheral);
_testPeripheral = peripheral;
[peripheral setDelegate:self]; <br>//查找服務(wù)
[peripheral discoverServices:nil];
}
發(fā)現(xiàn)服務(wù):
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"didDiscoverServices");
if (error)
{
NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)])
[self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil];
return;
}
for (CBService *service in peripheral.services)
{
//發(fā)現(xiàn)服務(wù)
if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]])
{
NSLog(@"Service found with UUID: %@", service.UUID); <br>//查找特征
[peripheral discoverCharacteristics:nil forService:service];
break;
}
}
}
發(fā)現(xiàn)服務(wù)中的特征:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error)
{
NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
[self error];
return;
}
NSLog(@"服務(wù):%@",service.UUID);
for (CBCharacteristic *characteristic in service.characteristics)
{
//發(fā)現(xiàn)特征
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) {
NSLog(@"監(jiān)聽:%@",characteristic);<br>//監(jiān)聽特征
[self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
5.與外設(shè)進(jìn)行數(shù)據(jù)交互
讀取數(shù)據(jù):
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error)
{
NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]);
self.error_b = BluetoothError_System;
[self error];
return;
}
// NSLog(@"收到的數(shù)據(jù):%@",characteristic.value);
[self decodeData:characteristic.value];
}
寫數(shù)據(jù):
NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"];
[self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];