
vcHL1eK49nBlcmlwaGVyYWyjrL3T18XE473Qy/y747Goy/zM4bmptcS3/s7xo6zV4rj2cGVyaXBoZXJhbLvhu+OxqMv509C3/s7xuPjE46GjscjI59DEzPjSx8b3zOG5qcG91tZzZXJ2aWNlo6zJ6LG40MXPonNlcnZpY2XT68r9vt278cihc2VydmljZS7V4tH5xOO+zdaqtcDT0MTEvLjW1rf+zvHBy6GjCjMuyLu688Tjv8nS1LbUxOO40NDLyKS1xLf+zvG9+NDQstnX96GjscjI587Sw8e21Mr9vt278cihtcS3/s7xuNDQy8iko6zO0r7NttTV4rj2u/HIobXEt/7O8cu1o6y4+M7SxOO1xMv509C1xGNoYXJhY3RlcmlzY3Msy/nT0LXEzNjV96GjvNnJ6NXiyrG68sv8t7W72MG9uPbM2NX3o6zSu7j2ysfQxMz4yv2+3bXEY2hhcmFjdGVyaXN0aWMswe3N4tK7uPbKx9DEzPjRucGmyv2+3aOoz7mx4LXEo6mho8Tjvs2/ydLUttTG5MzY1fe9+NDQtsHQtLLZ1/ejrNXiwO+78cihstnX96Osvs3Kx7bBstnX96Gju/HIobW9ttTTpsr9vt3UtM23Cjxicj4KCr+00rvPwsC019S52c34tcS94bm5zbwKPGltZyBzcmM9"/uploadfile/Collfiles/20141201/2014120109200053.png" alt="\">#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:方法
當你訂閱成功後,如果數據有更新,則回調該方法。 你就可以去讀取你想要的數據了。