1祟峦、#import<CoreBluetooth/CoreBlutooth.h>
2危纫、@interface ViewController : UIViewController{
CBCentralManager *theManager;
CBPeripheral *thePerpher;
CBCharacteristic *theSakeCC;
}
3、開始連接:
if (theManager.state==CBCentralManagerStatePoweredOn) {
NSLog(@"主設(shè)備藍牙狀態(tài)正常猛拴,開始掃描外設(shè)...");
//nil表示掃描所有設(shè)備
[theManager scanForPeripheralsWithServices:nil options:nil];
}
4雹姊、#pragma mark -當(dāng)前藍牙主設(shè)備狀態(tài)
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
if (central.state==CBCentralManagerStatePoweredOn) {
self.title = @"藍牙已就緒";
self.connectBtn.enabled = YES;
[central scanForPeripheralsWithServices:nil options:nil];
}else
{
self.title = @"藍牙未準(zhǔn)備好";
[self.activeID stopAnimating];
switch (central.state) {
case CBCentralManagerStateUnknown:
NSLog(@">>>CBCentralManagerStateUnknown");
break;
case CBCentralManagerStateResetting:
NSLog(@">>>CBCentralManagerStateResetting");
break;
case CBCentralManagerStateUnsupported:
NSLog(@">>>CBCentralManagerStateUnsupported");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@">>>CBCentralManagerStateUnauthorized");
break;
case CBCentralManagerStatePoweredOff:
NSLog(@">>>CBCentralManagerStatePoweredOff");
break;
default:
break;
}}}
5、//掃描到設(shè)備會進入方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
NSLog(@"掃描連接外設(shè):%@ %@",peripheral.name,RSSI);
[central connectPeripheral:peripheral options:nil];
NSLog(@"peripheral:%@============advertisementData:%@==============RSSI:%@",peripheral.identifier,advertisementData,RSSI);
if ([peripheral.name hasSuffix:@"SMART"]) {
thePerpher = peripheral;
[central stopScan];
[central connectPeripheral:peripheral options:nil];
self.title = @"發(fā)現(xiàn)手環(huán)验游,開始連接...";
self.resultTextV.text = [NSString stringWithFormat:@"發(fā)現(xiàn)手環(huán):%@\n名稱:%@\n",peripheral.identifier.UUIDString,peripheral.name];
}}
6充岛、#pragma mark 設(shè)備掃描與連接的代理
//連接到Peripherals-成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
self.title = @"連接成功,掃描信息...";
NSLog(@"連接外設(shè)成功耕蝉!%@",peripheral.name);
self.peripheral = peripheral;
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
NSLog(@"開始掃描外設(shè)服務(wù) %@...",peripheral.name);
}
7崔梗、//掃描到服務(wù)
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
if (error)
{
NSLog(@"掃描外設(shè)服務(wù)出錯:%@-> %@", peripheral.name, [error localizedDescription]);
self.title = @"find services error.";
[self.activeID stopAnimating];
self.connectBtn.enabled = YES;
return;
}
NSLog(@"掃描到外設(shè)服務(wù):%@ -> %@",peripheral.name,peripheral.services);
for (CBService *service in peripheral.services) {
if ([service.UUID.UUIDString isEqualToString:ServicesUUID]) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
NSLog(@"開始掃描外設(shè)服務(wù)的特征 %@...",peripheral.name);
}
8、//掃描到特征
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error)
{
NSLog(@"掃描外設(shè)的特征失斃菰凇蒜魄!%@->%@-> %@",peripheral.name,service.UUID, [error localizedDescription]);
self.title = @"find characteristics error.";
[self.activeID stopAnimating];
self.connectBtn.enabled = YES;
return;
}
NSLog(@"掃描到外設(shè)服務(wù)特征有:%@->%@->%@",peripheral.name,service.UUID,service.characteristics);
//獲取Characteristic的值
for (CBCharacteristic *characteristic in service.characteristics){
{
self.characteristic = characteristic;
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
[self.peripheral setNotifyValue:YES forCharacteristic:characteristic];
if ([characteristic.UUID.UUIDString isEqualToString:NotifyUUID])
{
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
//讀取時間
else if ([characteristic.UUID.UUIDString isEqualToString:WriteUUID])
{
// 讀取時間:
Byte byte[3];
byte[0] = 0X89;
byte[1] = 0x00;
byte[2] = 0x00;
NSData *data = [NSData dataWithBytes:byte length:3];
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
// 讀取時間:
Byte byte1[4];
byte1[0] = 0xC6;
byte1[1] = 0x01;
byte1[2] = 0x08;
byte1[3] = 0x08;
NSData *data1 = [NSData dataWithBytes:byte1 length:4];
[peripheral writeValue:data1 forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
else if ([characteristic.UUID.UUIDString isEqualToString:SHAKE])
{
//震動
theSakeCC = characteristic;
}
//設(shè)備信息
else if ([characteristic.UUID.UUIDString isEqualToString:DEVICE])
{
[peripheral readValueForCharacteristic:characteristic];
}
}
}
}
#pragma mark 設(shè)備信息處理
//掃描到具體的值
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error
{
if (error) {
NSLog(@"掃描外設(shè)的特征失敗场躯!%@-> %@",peripheral.name, [error localizedDescription]);
self.title = @"find value error.";
return;
}
NSLog(@"==characteristic.UUID.UUIDString:%@===characteristic.value:%@",characteristic.UUID.UUIDString,characteristic.value);
if ([characteristic.UUID.UUIDString isEqualToString:[NSString stringWithFormat:NotifyUUID]]) {
Byte *steBytes = (Byte *)characteristic.value.bytes;
int steps = TCcbytesValueToInt(steBytes);
NSLog(@"步數(shù):%d====%@=======",steps,characteristic.value);
self.resultTextV.text = [NSString stringWithFormat:@"%@步數(shù):%d\n",self.resultTextV.text,steps];
UIAlertView *ali = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@步數(shù):%d\n",self.resultTextV.text,steps] message:[NSString stringWithFormat:@"%@步數(shù):%d\n",self.resultTextV.text,steps] delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[ali show];
}
else if ([characteristic.UUID.UUIDString isEqualToString:@"FFF1"])
{
Byte *bufferBytes = (Byte *)characteristic.value.bytes;
int buterys = TCcbytesValueToInt(bufferBytes)&0xff;
NSLog(@"電池:%d%%",buterys);
self.resultTextV.text = [NSString stringWithFormat:@"%@電量:%d%%\n",self.resultTextV.text,buterys];
}
else if ([characteristic.UUID.UUIDString isEqualToString:DEVICE])
{
Byte *infoByts = (Byte *)characteristic.value.bytes;
//這里解析infoByts得到設(shè)備信息
}
[self.activeID stopAnimating];
self.connectBtn.enabled = YES;
self.title = @"信息掃描完成";
}