iOS藍牙開發(fā)數(shù)據實時傳輸
更新時間:2019年12月25日 14:23:52 作者:畫個大餅
這篇文章主要為大家詳細介紹了iOS藍牙開發(fā)數(shù)據實時傳輸,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
隨著iOS項目開發(fā) 很多app需要通過藍牙與設備連接
藍牙開發(fā)注意:
先定義中心設備和外圍設備以及遵守藍牙協(xié)議
@interface ViewController()<CBCentralManagerDelegate,CBPeripheralDelegate> @property (strong, nonatomic) CBCentralManager *manager; @property (nonatomic, strong) CBPeripheral *peripheral; @property (nonatomic, weak)NSTimer * connentTimer; @end
再實現(xiàn)delegate方法
1.判斷藍牙狀態(tài),如成功則掃描指定UUID設備(如不指定UUID,則無法后臺持續(xù)連接)
2.當發(fā)現(xiàn)指定設備后,連接該設備
3.當連接指定外圍設備成功,編寫定時器,每秒讀取1次RSSI
4.當監(jiān)聽到失去和外圍設備連接,重新建立連接
5.當讀取到RSSI值,打印出它的值
//藍牙狀態(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;
//應用程序沒有被授權使用藍牙
case CBCentralManagerStateUnauthorized:
state = @"The app is not authorized to use Bluetooth Low Energy.";
break;
//尚未打開藍牙
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);
}
//查找設備
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//每個藍牙設備有自己唯一的標識符,根據標識符確認自己要連接的設備
if ([peripheral.identifier isEqual:self.peripheral.identifier])
{
self.peripheral = peripheral;
//數(shù)據連接定時器
self.connentTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(connentPeripheral) userInfo:@"timer" repeats:YES];
[self.connentTimer fire];
}
}
- (void)connentPeripheral {
//連接外設
self.manager.delegate = self;
[self.manager connectPeripheral:_peripheral options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}
//連接成功后調用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@,%@", peripheral,peripheral.name);
[peripheral setDelegate:self]; //查找服務
[peripheral discoverServices:nil];
[self.connentTimer invalidate];
//監(jiān)測設備是否斷開了
// [self createWorkDataSourceWithTimeInterval:1];
}
//當監(jiān)聽到失去和外圍設備連接,重新建立連接
//這個方法是必須實現(xiàn)的,因為藍牙會中斷連接,正好觸發(fā)這個方法重建連接。重建連接可能造成數(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);
}
//返回的藍牙服務通知通過代理實現(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)服務
if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180D"]])//heart rate
{
//在一個服務中尋找特征值
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
//返回的藍牙特征值通知通過代理實現(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];
}
}
}
//處理藍牙發(fā)過來的數(shù)據
- (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
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS自定義collectionView實現(xiàn)毛玻璃效果
不知道大家發(fā)現(xiàn)沒有蘋果在iOS7.0之后,很多系統(tǒng)界面都使用了毛玻璃效果,增加了界面的美觀性,所以這篇文章跟大家分享個iOS自定義collectionView實現(xiàn)毛玻璃效果的方法,有需要的可以參考借鑒,下面來一起看看。2016-09-09
IOS 波紋進度(waveProgress)動畫實現(xiàn)
這篇文章主要介紹了IOS 紋進度(waveProgress)動畫實現(xiàn)的相關資料,需要的朋友可以參考下2016-09-09

