iOS蓝牙总结

2016-12-01  本文已影响61人  xieyinghao

1、#import<CoreBluetooth/CoreBlutooth.h>

2、@interface ViewController : UIViewController{

CBCentralManager *theManager;

CBPeripheral *thePerpher;

CBCharacteristic *theSakeCC;

}

3、开始连接:

if (theManager.state==CBCentralManagerStatePoweredOn) {

NSLog(@"主设备蓝牙状态正常,开始扫描外设...");

//nil表示扫描所有设备

[theManager scanForPeripheralsWithServices:nil options:nil];

}

4、#pragma mark -当前蓝牙主设备状态

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{

if (central.state==CBCentralManagerStatePoweredOn) {

self.title = @"蓝牙已就绪";

self.connectBtn.enabled = YES;

[central scanForPeripheralsWithServices:nil options:nil];

}else

{

self.title = @"蓝牙未准备好";

[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、//扫描到设备会进入方法

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{

NSLog(@"扫描连接外设:%@ %@",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 = @"发现手环,开始连接...";

self.resultTextV.text = [NSString stringWithFormat:@"发现手环:%@\n名称:%@\n",peripheral.identifier.UUIDString,peripheral.name];

}}

6、#pragma mark 设备扫描与连接的代理

//连接到Peripherals-成功

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

{

self.title = @"连接成功,扫描信息...";

NSLog(@"连接外设成功!%@",peripheral.name);

self.peripheral = peripheral;

[peripheral setDelegate:self];

[peripheral discoverServices:nil];

NSLog(@"开始扫描外设服务 %@...",peripheral.name);

}

7、//扫描到服务

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

if (error)

{

NSLog(@"扫描外设服务出错:%@-> %@", peripheral.name, [error localizedDescription]);

self.title = @"find services error.";

[self.activeID stopAnimating];

self.connectBtn.enabled = YES;

return;

}

NSLog(@"扫描到外设服务:%@ -> %@",peripheral.name,peripheral.services);

for (CBService *service in peripheral.services) {

if ([service.UUID.UUIDString isEqualToString:ServicesUUID]) {

[peripheral discoverCharacteristics:nil forService:service];

}

}

NSLog(@"开始扫描外设服务的特征 %@...",peripheral.name);

}

8、//扫描到特征

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

if (error)

{

NSLog(@"扫描外设的特征失败!%@->%@-> %@",peripheral.name,service.UUID, [error localizedDescription]);

self.title = @"find characteristics error.";

[self.activeID stopAnimating];

self.connectBtn.enabled = YES;

return;

}

NSLog(@"扫描到外设服务特征有:%@->%@->%@",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;

}

//设备信息

else if ([characteristic.UUID.UUIDString isEqualToString:DEVICE])

{

[peripheral readValueForCharacteristic:characteristic];

}

}

}

}

#pragma mark 设备信息处理

//扫描到具体的值

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error

{

if (error) {

NSLog(@"扫描外设的特征失败!%@-> %@",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(@"步数:%d====%@=======",steps,characteristic.value);

self.resultTextV.text = [NSString stringWithFormat:@"%@步数:%d\n",self.resultTextV.text,steps];

UIAlertView *ali = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@步数:%d\n",self.resultTextV.text,steps] message:[NSString stringWithFormat:@"%@步数:%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得到设备信息

}

[self.activeID stopAnimating];

self.connectBtn.enabled = YES;

self.title = @"信息扫描完成";

}

上一篇下一篇

猜你喜欢

热点阅读