基礎知識瞭解git
BLE中的開發要使用CoreBluetooth框架 CoreBluetooth框架的核心實際上是兩個東西,peripheral和central, 能夠理解成外設和中心。對應他們分別有一組相關的API和類藍牙設備的幾種狀態:github
做爲中心模式流程:api
創建中心角色bash
掃描外設(discover)框架
鏈接外設(connect)工具
掃描外設中的服務和特徵(discover)ui
與外設作數據交互(explore and interact)spa
訂閱Characteristic的通知代理
斷開鏈接(disconnect)code
做爲外設模式流程:
具體實施以下
[BLEManager.shareInstance setDelegate:self];
複製代碼
#pragma mark- 開始搜索
- (void)beginScan{
[self.cbCM scanForPeripheralsWithServices:nil options:nil];
}
複製代碼
#pragma mark- 鏈接設備
- (void)connect:(CBPeripheral *)peripheral{
[self.cbCM stopScan];
[self.cbCM connectPeripheral:peripheral options:nil];
}
複製代碼
#pragma mark- 鏈接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
connectPeripheral = peripheral;
peripheral.delegate = self;
[peripheral discoverServices:nil];
if ([self.delegate respondsToSelector:@selector(didConnectPeripheral:)]) {
[self.delegate didConnectPeripheral:peripheral];
}
}
#pragma mark- 鏈接失敗
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
if ([self.delegate respondsToSelector:@selector(didFailToConnectPeripheral:error:)]) {
[self.delegate didFailToConnectPeripheral:peripheral error:error];
}
}
複製代碼
//發現服務
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
for (CBService *s in peripheral.services) {
if ([s.UUID.UUIDString isEqualToString:self->serviceUUIDString]) {
//這裏能夠經過service的UUID屬性來辨識你要的服務
[peripheral discoverCharacteristics:nil forService:s];
}
}
}
//設置特徵屬性值
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->notify]]){
// 訂閱, 實時接收
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->write]]){
self.writeCharacteristic = characteristic;
}
}
}
複製代碼
#pragma mark- 發送數據
- (void)write:(NSData *)data withResponse:(BOOL)withResponse{
if(!_writeCharacteristic){
NSLog(@"writeCharacteristic is nil!");
return;
}
[connectPeripheral writeValue:data forCharacteristic:self.writeCharacteristic type:withResponse ? CBCharacteristicWriteWithResponse : CBCharacteristicWriteWithoutResponse];
}
複製代碼
#pragma mark- 藍牙數據接收
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->serviceUUIDString]]) {
NSData *data = characteristic.value;
if (data) {
if ([self.delegate respondsToSelector:@selector(bleGattServiceDataReceived:)]) {
[self.delegate bleGattServiceDataReceived:data];
}
}
}
}
複製代碼
#pragma mark- 藍牙狀態的改變
- (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
switch (central.state) {
case CBManagerStateUnknown:
//未知狀態
break;
case CBManagerStateResetting:
//藍牙重置中
break;
case CBManagerStateUnsupported:
//藍牙不支持
break;
case CBManagerStateUnauthorized:
//沒有權限
break;
case CBManagerStatePoweredOff:
//藍牙爲開啓
break;
case CBManagerStatePoweredOn:
//藍牙已開啓
break;
default:
break;
}
}
複製代碼