我正在尝试使用设备中的CoreBluetooth框架读取所有可用服务及其特征值。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Received peripheral : \n%@", peripheral);
NSLog(@"Adv data : %@", advertisementData);
self.activeperipheral=peripheral;
[self.myCentralManager connectPeripheral:peripheral options:nil];
if(peripheral.state==CBPeripheralStateConnected){
peripheral.delegate=self;
NSLog(@"Connected");
}
else
NSLog(@"Not Connected");
NSArray *serviceUUIDs=[advertisementData objectForKey:CBAdvertisementDataServiceUUIDsKey];
for(CBUUID *foundserviceUUIDs in serviceUUIDs){
if([serviceUUIDs containsObject:foundserviceUUIDs]){
NSLog(@"%@",serviceUUIDs);
}
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
for(CBService *service in peripheral.services){
[peripheral discoverCharacteristics:nil forService:service];
NSLog(@"Discover Service:%@",service);
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
for(CBCharacteristic *characteristic in service.characteristics){
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
self.myCharacteristic=characteristic;
NSLog(@"NotifyValue set on %@",characteristic);
//[peripheral readValueForCharacteristic:myCharacteristic];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if(characteristic.isNotifying)
NSLog(@"Notification began on %@",characteristic);
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"%@",characteristic);
// NSLog(@"Characteristic Value Updated");
}
- (int)scanForPeripherals {
NSLog(@"Scanning");
if(self.myCentralManager.state!=CBCentralManagerStatePoweredOn)
NSLog(@"Turn on Bluetooth");
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
NSLog(@"Scanning");
[myCentralManager scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral Connected");
peripheral.delegate=self;
//CBUUID *serviceUUID=[CBUUID UUIDWithString:@"1804"];
[peripheral discoverServices:nil];
//[myCentralManager stopScan];
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"didDisconnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"failed to connect");
}
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
NSLog(@"didReadRSSI");
}
@end
问题是我无法设置NotifyValue:是的,除了BatteryLevel特性之外的其他特性,我也只收到相同的通知。
这是我的输出。 知道我在这里做错了什么吗? 感谢
答案 0 :(得分:0)
因为您在gatt文件(您的蓝牙项目)中的特性设置未正确设置。请参阅其他特征设置并更新您的gatt文件。 获得特征值的另一种方法是使用计时器来读取你的特征:
-(void)TimerFunction
{
[talking_peripher readValueForCharacteristic:_talk_charack];
NSlog("VALUE: %@" _talk_charack);
}
就是这样。但实际上最好做 - 修改gatt文件中的特征设置(蓝牙项目)。