简体   繁体   中英

iOS:Send Command To peripheral and Receive response From it

I am developing core bluetooth app in iOS. I have smartwatch device and is connecting to the app I have developed and aim able to discover the service and characteristics from it and when I am trying to read the information from the watch aim getting errors as below

 Bluetooth_iph[2688:614669] Did discover peripheral. peripheral: <CBPeripheral: 0x17daf4d0,   
  identifier = D675EA0B-1342-0C74-9D3D-98CAFA478985, name = BLEDEVICE, state = connecting> 
   rssi: -51, UUID: <CFUUID 0x17d96e80> D675EA0B-1342-0C74-9D3D-98CAFA478985     
  advertisementData: {
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = "BLEDEVICE";
kCBAdvDataServiceUUIDs =     (
    8880
);
kCBAdvDataTxPowerLevel = 7;

  } 
 2014-10-17 10:06:13.695 Bluetooth_iph[2688:614669] WARNING: No service found
 2014-10-17 10:06:18.353 Bluetooth_iph[2688:614669] Peripheral Connected
 2014-10-17 10:06:18.354 Bluetooth_iph[2688:614669]  started  time is 10:06:10 17-10-14
 2014-10-17 10:06:18.354 Bluetooth_iph[2688:614669] Scanning stopped
 2014-10-17 10:06:18.485 Bluetooth_iph[2688:614669] Found a Device Manufacturer Name 
 2014-10-17 10:06:18.541 Bluetooth_iph[2688:614669] Error updating value for characteristic   
 8881 error: Reading is not permitted.

 2014-10-17 10:06:18.601 Bluetooth_iph[2688:614669] Error updating value for characteristic 
 8881 error: Reading is not permitted.

 2014-10-17 10:06:18.662 Bluetooth_iph[2688:614669] Error updating value for characteristic 
 8881 error: Reading is not permitted.

And for discovering the characteristics for the service I have written as below.

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

  {
  if (error)
  {
    NSLog(@"Discovered characteristics for %@ with error: %@", service.UUID, [error 
     localizedDescription]);
    return;
  }


   if([service.UUID isEqual:[CBUUID UUIDWithString:@"8880"]])
  {
    for (CBCharacteristic * characteristic in service.characteristics)
    {
        /* Read manufacturer name */
        if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"8881"]])
        {
            [_discoveredPeripheral readValueForCharacteristic:characteristic];
            NSLog(@"Found a Device Manufacturer Name Characteristic - Read manufacturer 
      name");
        }
    }
  }

   if ( [service.UUID isEqual:[CBUUID UUIDWithString:CBUUIDGenericAccessProfileString]] )
    {
    for (CBCharacteristic *characteristic in service.characteristics)
    {
        /* Read device name */
        if([characteristic.UUID isEqual:[CBUUID UUIDWithString:CBUUIDDeviceNameString]])
        {
            [_discoveredPeripheral readValueForCharacteristic:characteristic];
            NSLog(@"Found a Device Name Characteristic - Read device name");
        }
     }
  }
  }

And for update value for characteristic

 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:  
  (CBCharacteristic *)characteristic error:(NSError *)error
 {
 if (error)
  {
     NSLog(@"Error updating value for characteristic %@ error: %@", characteristic.UUID,   
   [error localizedDescription]);
    return;
  }
}

As aim new to iOS development my requirement is I have to give some request to ble device so it should respond and that response I should get in the iOS app. Above code is on behalf of my knowledge. please help me if possible by code or any other examples.

You should check the properties property of the CBCharacteristic - It will tell you what you can do with the characteristic. From the message you are getting reading is not supported on this characteristic - Either you can only write to it or, if it is meant to send information to your central, then you probably have to do it using notification or indication ( CBCharacteristicPropertyNotify and/or CBCharacteristicPropertyIndicate set in properties ).

If this is the case then you need to use [peripheral setNotifyValue:YES forCharacteristic:characteristic]; When the device has new data for this characteristic it will call the peripheral:didUpdateNotificationStateForCharacteristic:error: delegate method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM