iOS藍牙4.0開發(fā)

1建立中心角色

#import <CoreBluetooth/CoreBluetooth.h>

CBCentralManager *manager;

manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

2掃描外設(discover)

[manager scanForPeripheralsWithServices:nil options:options];

3連接外設(connect)

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

{

if([peripheral.name? isEqualToString:BLE_SERVICE_NAME]){

[self connect:peripheral];

}

s);

}

-(BOOL)connect:(CBPeripheral *)peripheral{

self.manager.delegate = self;

[self.manager connectPeripheral:peripheral

options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];

}

4掃描外設中的服務和特征(discover)

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {? ? ? ? ? ? NSLog(@"Did connect to peripheral: %@", peripheral);? ? _testPeripheral = peripheral;? ? ? ? ? ? [peripheral setDelegate:self];

//查找服務? ? [peripheral discoverServices:nil];? ? ? ? ? ? ? }

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {? ? ? ? ? ? ? NSLog(@"didDiscoverServices");? ? ? ? ? ? if (error)? ? {? ? ? ? NSLog(@"Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);? ? ? ? ? ? ? ? ? ? if ([self.delegate respondsToSelector:@selector(DidNotifyFailConnectService:withPeripheral:error:)])? ? ? ? ? ? [self.delegate DidNotifyFailConnectService:nil withPeripheral:nil error:nil];? ? ? ? ? ? ? ? ? ? return;? ? }? ? ? ? ? ? ? for (CBService *service in peripheral.services)? ? {? ? ? ? ? //發(fā)現服務? ? ? ? if ([service.UUID isEqual:[CBUUID UUIDWithString:UUIDSTR_ISSC_PROPRIETARY_SERVICE]])? ? ? ? {? ? ? ? ? ? NSLog(@"Service found with UUID: %@", service.UUID);//查找特征 ? ? ? ? ?[peripheral discoverCharacteristics:nil forService:service];? ? ? ? ? ? break;? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ? } }12345678910111213141516171819202122- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{? ? ? ? if (error)? ? {? ? ? ? NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error localizedDescription]);? ? ? ? ? ? ? ? [self error];? ? ? ? return;? ? }? ? ? ? NSLog(@"服務:%@",service.UUID);? ? for (CBCharacteristic *characteristic in service.characteristics)? ? { ? ? //發(fā)現特征 ? ? ? ? ? ?if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"xxxxxxx"]]) {? ? ? ? ? ? ? ? NSLog(@"監(jiān)聽:%@",characteristic);//監(jiān)聽特征 ? ? ? ? ? ? ? [self.peripheral setNotifyValue:YES forCharacteristic:characteristic];? ? ? ? ? ? }? ? ? ? ? ? }}

5與外設做數據交互(讀 與 寫)

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{? ? if (error)? ? {? ? ? ? NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID, [error localizedDescription]);? ? ? ? self.error_b = BluetoothError_System;? ? ? ? [self error];? ? ? ? return;? ? }? ? //? ? NSLog(@"收到的數據:%@",characteristic.value);? ? [self decodeData:characteristic.value];}

12NSData *d2 = [[PBABluetoothDecode sharedManager] HexStringToNSData:@"0x02"];? ? ? ? ? ? ? ? [self.peripheral writeValue:d2 forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容