iOS藍(lán)牙開發(fā)數(shù)據(jù)實(shí)時(shí)傳輸
隨著iOS項(xiàng)目開發(fā) 很多app需要通過藍(lán)牙與設(shè)備連接
藍(lán)牙開發(fā)注意:
先定義中心設(shè)備和外圍設(shè)備以及遵守藍(lán)牙協(xié)議
@interface ViewController()<CBCentralManagerDelegate,CBPeripheralDelegate> @property (strong, nonatomic) CBCentralManager *manager; @property (nonatomic, strong) CBPeripheral *peripheral; @property (nonatomic, weak)NSTimer * connentTimer; @end
再實(shí)現(xiàn)delegate方法
1.判斷藍(lán)牙狀態(tài),如成功則掃描指定UUID設(shè)備(如不指定UUID,則無法后臺(tái)持續(xù)連接)
2.當(dāng)發(fā)現(xiàn)指定設(shè)備后,連接該設(shè)備
3.當(dāng)連接指定外圍設(shè)備成功,編寫定時(shí)器,每秒讀取1次RSSI
4.當(dāng)監(jiān)聽到失去和外圍設(shè)備連接,重新建立連接
5.當(dāng)讀取到RSSI值,打印出它的值
//藍(lán)牙狀態(tài)
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString * state = nil;
switch ([central state])
{
case CBCentralManagerStateUnsupported:
state = @"The platform/hardware doesn't support Bluetooth Low Energy.";
break;
//應(yīng)用程序沒有被授權(quán)使用藍(lán)牙
case CBCentralManagerStateUnauthorized:
state = @"The app is not authorized to use Bluetooth Low Energy.";
break;
//尚未打開藍(lán)牙
case CBCentralManagerStatePoweredOff:
state = @"Bluetooth is currently powered off.";
break;
//連接成功
case CBCentralManagerStatePoweredOn:
[self.manager scanForPeripheralsWithServices:nil options:nil];
state = @"work";
break;
case CBCentralManagerStateUnknown:
default:
;
}
NSLog(@"Central manager state: %@", state);
}
//查找設(shè)備
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//每個(gè)藍(lán)牙設(shè)備有自己唯一的標(biāo)識(shí)符,根據(jù)標(biāo)識(shí)符確認(rèn)自己要連接的設(shè)備
if ([peripheral.identifier isEqual:self.peripheral.identifier])
{
self.peripheral = peripheral;
//數(shù)據(jù)連接定時(shí)器
self.connentTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(connentPeripheral) userInfo:@"timer" repeats:YES];
[self.connentTimer fire];
}
}
- (void)connentPeripheral {
//連接外設(shè)
self.manager.delegate = self;
[self.manager connectPeripheral:_peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}
//連接成功后調(diào)用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@,%@", peripheral,peripheral.name);
[peripheral setDelegate:self]; //查找服務(wù)
[peripheral discoverServices:nil];
[self.connentTimer invalidate];
//監(jiān)測(cè)設(shè)備是否斷開了
// [self createWorkDataSourceWithTimeInterval:1];
}
//當(dāng)監(jiān)聽到失去和外圍設(shè)備連接,重新建立連接
//這個(gè)方法是必須實(shí)現(xiàn)的,因?yàn)樗{(lán)牙會(huì)中斷連接,正好觸發(fā)這個(gè)方法重建連接。重建連接可能造成數(shù)秒后才能讀取到RSSI。
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
[self.manager connectPeripheral:peripheral options:nil];
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"%@",error.description);
}
//返回的藍(lán)牙服務(wù)通知通過代理實(shí)現(xiàn)
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error)
{
NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);
return;
}
for (CBService *service in peripheral.services)
{
// NSLog(@"Service found with UUID: %@", service.UUID.UUIDString);
//發(fā)現(xiàn)服務(wù)
if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180D"]])//heart rate
{
//在一個(gè)服務(wù)中尋找特征值
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
//返回的藍(lán)牙特征值通知通過代理實(shí)現(xiàn)
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error)
{
NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);
return;
}
for (CBCharacteristic * characteristic in service.characteristics)
{
NSLog(@"characteristic:%@",characteristic);
if( [characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A37"]])
{
[self notification:service.UUID characteristicUUID:characteristic.UUID peripheral:peripheral on:YES];
// [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
//處理藍(lán)牙發(fā)過來的數(shù)據(jù)
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
}
-(void) notification:(CBUUID *) serviceUUID characteristicUUID:(CBUUID *)characteristicUUID peripheral:(CBPeripheral *)p on:(BOOL)on
{
CBService *service = [self getServiceFromUUID:serviceUUID p:p];
if (!service)
{
// if (p.UUID == NULL) return; // zach ios6 addedche
// NSLog(@"Could not find service with UUID on peripheral with UUID \n");
return;
}
CBCharacteristic *characteristic = [self getCharacteristicFromUUID:characteristicUUID service:service];
if (!characteristic)
{
// if (p.UUID == NULL) return; // zach ios6 added
// NSLog(@"Could not find characteristic with UUID on service with UUID on peripheral with UUID\n");
return;
}
[p setNotifyValue:on forCharacteristic:characteristic];
}
-(CBService *) getServiceFromUUID:(CBUUID *)UUID p:(CBPeripheral *)p
{
for (CBService* s in p.services)
{
if ([s.UUID isEqual:UUID]) return s;
}
return nil; //Service not found on this peripheral
}
-(CBCharacteristic *) getCharacteristicFromUUID:(CBUUID *)UUID service:(CBService*)service {
for (CBCharacteristic* c in service.characteristics)
{
if ([c.UUID isEqual:UUID]) return c;
}
return nil; //Characteristic not found on this service
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS微信端confirm以及alert去掉網(wǎng)址的實(shí)例代碼
下面小編就為大家分享一篇IOS微信端confirm以及alert去掉網(wǎng)址的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果
不知道大家發(fā)現(xiàn)沒有蘋果在iOS7.0之后,很多系統(tǒng)界面都使用了毛玻璃效果,增加了界面的美觀性,所以這篇文章跟大家分享個(gè)iOS自定義collectionView實(shí)現(xiàn)毛玻璃效果的方法,有需要的可以參考借鑒,下面來一起看看。2016-09-09
iOS仿微信搖一搖動(dòng)畫效果加震動(dòng)音效實(shí)例
這篇文章主要介紹了iOS仿微信搖一搖動(dòng)畫效果加震動(dòng)音效實(shí)例,詳細(xì)介紹了微信搖一搖功能的實(shí)現(xiàn)原理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03
IOS 波紋進(jìn)度(waveProgress)動(dòng)畫實(shí)現(xiàn)
這篇文章主要介紹了IOS 紋進(jìn)度(waveProgress)動(dòng)畫實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-09-09

