简体   繁体   English

iOS:将命令发送到外围设备并从中接收响应

[英]iOS:Send Command To peripheral and Receive response From it

I am developing core bluetooth app in iOS. 我正在开发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. 作为iOS开发的新目标,我的要求是我必须向ble设备发出一些请求,以便它能够响应,并且应该在iOS应用程序中得到该响应。 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. 您应该检查CBCharacteristicproperties属性-它会告诉您可以使用该特性做什么。 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 ). 从此功能不支持您正在阅读的消息-要么只能写它,或者如果它是向中央发送信息,则可能必须使用通知或指示( CBCharacteristicPropertyNotify和/或在properties设置的CBCharacteristicPropertyIndicate )。

If this is the case then you need to use [peripheral setNotifyValue:YES forCharacteristic:characteristic]; 如果是这种情况,则需要使用[peripheral setNotifyValue:YES forCharacteristic:characteristic]; When the device has new data for this characteristic it will call the peripheral:didUpdateNotificationStateForCharacteristic:error: delegate method. 当设备具有针对该特性的新数据时,它将调用peripheral:didUpdateNotificationStateForCharacteristic:error:委托方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IOS:将命令值写入核心蓝牙外围设备以获取响应 - IOS:Write command values to corebluetooth peripheral device to get the response iOS CoreBluetooth:从中央发送数据并从外围设备获取响应 - iOS CoreBluetooth:Sending data from central and getting response from peripheral iBeacon iOS 10:设备可以扫描并从其他外围设备接收广告吗? - iBeacon iOS 10: Can device scan for and receive advertisements from other peripheral devices? iOS BLE 中央如何在通知模式下从外围设备接收超过 182 个字节? - How can an iOS BLE central receive more than 182 bytes from a peripheral in notify mode? iOS收到来自服务器的udp响应 - iOS receive udp response from server iOS:未收到从中央到外围设备的写入请求 - iOS: Not receiving the write requests from Central to Peripheral 使用iOS SDK中的BLE从外部设备发送和接收数据 - Send and Receive data from external device using BLE in iOS SDK iOS-应用程序可以向服务器发送消息或从服务器接收消息吗? - iOS - Can application send and receive messages to/from server? 在iOS中发送和接收推送通知? - Push Notification send and receive in iOS? 蓝牙LE无法从中央向外围设备发送数据 - Bluetooth LE can't send data from central to peripheral
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM