#import@interface ViewController () @property (nonatomic, retain) CBCentralManager *centralManager; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; }
/* Invoked whenever the central manager's state is updated. */ - (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: state = @"work"; break; case CBCentralManagerStateUnknown: default: ; } NSLog(@"Central manager state: %@", state); }上面的代碼如果這個時候如果是CBCentralManagerStatePoweredOn,代表藍牙可用。一定要在該方法回調後去開啟掃描外設,否則無反應.
- (IBAction)scan:(id)sender { [self.centralManager scanForPeripheralsWithServices:nil options:nil]; }上面代碼我創建了一個按鈕來掃描,就不上對應的xib圖片了.
- (IBAction)scan:(id)sender { [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FFE0"]] options:nil]; }如果你只想搜索有提供對應的服務號的外設(去peripharal 的advertising packet裡匹配服務號,傳入一個數組進入,對象為CBUUID,也就是Service的唯一標識符。一般我們搜索過一個nil的就會知道我們要的服務好是什麼樣子的了,之後編程就可以直接使用這個已知的服務好。除非你的藍牙設備的廠家更改服務號,不過幾乎不可能。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { static int i = 0; NSString *str = [NSString stringWithFormat:@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData]; NSLog(@"%@",str); [self.discoverdPeriparals addObject:peripheral]; }當你發現了一個設備,該方法會回調。peripheral代表你發現的設備,advertisementData時廣告數據,rssi代表著信號強度.
- (IBAction)connect:(id)sender { [self.centralManager connectPeripheral:[self.discoverdPeriparals firstObject] options:nil]; }
/* Invoked whenever a connection is succesfully created with the peripheral. Discover available services on the peripheral */ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Did connect to peripheral: %@", peripheral); peripheral.delegate = self; [central stopScan]; [peripheral discoverServices:nil]; }當連接成功後調用該方法。這個時候我們設置該peripheral的代理我們自己,讓peripheral給我們返回所有服務。
[peripheral discoverServices:@[[CBUUID UUIDWithString:@"FFE0"]]];這個方法也是傳入nil返回所有服務,如果是傳入特定的服務id,只返回該服務 這裡我們傳入nil來返回所有服務
- (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); if ([service.UUID isEqual:[CBUUID UUIDWithString:@"FFE0"]]) { [peripheral discoverCharacteristics:nil forService:service]; } } }
[peripheral discoverCharacteristics:nil forService:service];
- (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) { DLog(@"%@",characteristic); if( [characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFE1"]]) { self.p = peripheral; self.c = characteristic; //read //[testPeripheral readValueForCharacteristic:characteristic]; NSLog(@"Found a Device Manufacturer Name Characteristic - Read manufacturer name"); } } }上面的方法是找到FEE0服務的所有特征,這裡的只有一個,也就藍牙小票機FFE0寫服務的寫特征. 獲取到該特征。進行寫服務,具體的log就不寫了
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSString *RStr = @"2764"; NSMutableString *str = [NSMutableString new]; [str appendFormat:@"%c", 28]; [str appendFormat:@"%c", 33]; [str appendFormat:@"%c", 8]; [self.p writeValue:[str dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)] forCharacteristic:self.c type:CBCharacteristicWriteWithResponse]; RStr = @"吳水鳳 你好呀!!!\n\n\n\n\n\n\n\n"; [self.p writeValue:[RStr dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)] forCharacteristic:self.c type:CBCharacteristicWriteWithResponse]; }這裡我對藍牙小票機提供的寫特征進行寫服務。成功.
[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];
該方法回調peripheral:didUpdateValueForCharacteristic:error:方法
當你訂閱成功後,如果數據有更新,則回調該方法。 你就可以去讀取你想要的數據了。