Unable to read the data from Third party Device with ExternalAccessory communication in iOS swift










0















I have a specific requirement for communication between iOS devices and third-party Bluetooth devices.



I have successfully established a connection with the device(MFI enabled device ) using EAAccessory Framework using the following code.



let accessory = EAAccessoryManager.shared().connectedAccessories.first!
session = EASession(accessory: accessory, forProtocol: “protocol string”)
session?.outputStream?.delegate = self
session?.outputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
session?.outputStream?.open()
session?.inputStream?.delegate = self
session?.inputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
session?.inputStream?.open()
if availableAccessories.count>0
print("Detected Device")

else
print("Not detected")



Now we need to make the iOS device to the listerning state to read the transmitted data by health Bluetooth device.



How can we change the iOS device to the listening state? We used the following code.



func stream(_ aStream: Stream, handle eventCode: Stream.Event) 
switch eventCode
case Stream.Event.hasBytesAvailable:
let inStream = aStream as? InputStream
_readData(aStream: inStream!)
var buffer = [UInt8](repeating: 0, count: 2048)
var data = Data()
while ((inStream?.hasBytesAvailable)!)
let bytesRead: Int = inStream!.read(&buffer, maxLength: buffer.count)
if bytesRead >= 0
data.append(buffer, count: bytesRead)



print("data.hexEncodedString()")
print(data.hexEncodedString())

case Stream.Event.hasSpaceAvailable:
print("Send the next queued command")
default:
print("Default")





var readData : NSMutableData!



public func _readData(aStream:InputStream)
print("Trying to read data")
let INPUT_BUFFER_SIZE = 65536
let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: INPUT_BUFFER_SIZE)
while(aStream.hasBytesAvailable)
let bytesRead = aStream.read(buf, maxLength: INPUT_BUFFER_SIZE)
if readData == nil
readData = NSMutableData()

readData.append(buf, length: bytesRead)

if readData != nil
let data: NSData = readData
let count = data.length / MemoryLayout.size(ofValue: UInt8())
var array = [UInt8](repeating: 0, count: count)
data.getBytes(&array, length:count * MemoryLayout<UInt8>.size)
print("Data Received :: (readData)")
let dataString = String(data: data as Data, encoding: .utf8)!
print("Data Received dataString:: (dataString)")




I am getting the result as




"CALL 60:a3:7d:38:ff:b3 1234 RFCOMM"




So we are unable to read actual data from the device. Are we going in the right direction? Do require any other scenario or further steps to achieve this?










share|improve this question




























    0















    I have a specific requirement for communication between iOS devices and third-party Bluetooth devices.



    I have successfully established a connection with the device(MFI enabled device ) using EAAccessory Framework using the following code.



    let accessory = EAAccessoryManager.shared().connectedAccessories.first!
    session = EASession(accessory: accessory, forProtocol: “protocol string”)
    session?.outputStream?.delegate = self
    session?.outputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
    session?.outputStream?.open()
    session?.inputStream?.delegate = self
    session?.inputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
    session?.inputStream?.open()
    if availableAccessories.count>0
    print("Detected Device")

    else
    print("Not detected")



    Now we need to make the iOS device to the listerning state to read the transmitted data by health Bluetooth device.



    How can we change the iOS device to the listening state? We used the following code.



    func stream(_ aStream: Stream, handle eventCode: Stream.Event) 
    switch eventCode
    case Stream.Event.hasBytesAvailable:
    let inStream = aStream as? InputStream
    _readData(aStream: inStream!)
    var buffer = [UInt8](repeating: 0, count: 2048)
    var data = Data()
    while ((inStream?.hasBytesAvailable)!)
    let bytesRead: Int = inStream!.read(&buffer, maxLength: buffer.count)
    if bytesRead >= 0
    data.append(buffer, count: bytesRead)



    print("data.hexEncodedString()")
    print(data.hexEncodedString())

    case Stream.Event.hasSpaceAvailable:
    print("Send the next queued command")
    default:
    print("Default")





    var readData : NSMutableData!



    public func _readData(aStream:InputStream)
    print("Trying to read data")
    let INPUT_BUFFER_SIZE = 65536
    let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: INPUT_BUFFER_SIZE)
    while(aStream.hasBytesAvailable)
    let bytesRead = aStream.read(buf, maxLength: INPUT_BUFFER_SIZE)
    if readData == nil
    readData = NSMutableData()

    readData.append(buf, length: bytesRead)

    if readData != nil
    let data: NSData = readData
    let count = data.length / MemoryLayout.size(ofValue: UInt8())
    var array = [UInt8](repeating: 0, count: count)
    data.getBytes(&array, length:count * MemoryLayout<UInt8>.size)
    print("Data Received :: (readData)")
    let dataString = String(data: data as Data, encoding: .utf8)!
    print("Data Received dataString:: (dataString)")




    I am getting the result as




    "CALL 60:a3:7d:38:ff:b3 1234 RFCOMM"




    So we are unable to read actual data from the device. Are we going in the right direction? Do require any other scenario or further steps to achieve this?










    share|improve this question


























      0












      0








      0








      I have a specific requirement for communication between iOS devices and third-party Bluetooth devices.



      I have successfully established a connection with the device(MFI enabled device ) using EAAccessory Framework using the following code.



      let accessory = EAAccessoryManager.shared().connectedAccessories.first!
      session = EASession(accessory: accessory, forProtocol: “protocol string”)
      session?.outputStream?.delegate = self
      session?.outputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
      session?.outputStream?.open()
      session?.inputStream?.delegate = self
      session?.inputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
      session?.inputStream?.open()
      if availableAccessories.count>0
      print("Detected Device")

      else
      print("Not detected")



      Now we need to make the iOS device to the listerning state to read the transmitted data by health Bluetooth device.



      How can we change the iOS device to the listening state? We used the following code.



      func stream(_ aStream: Stream, handle eventCode: Stream.Event) 
      switch eventCode
      case Stream.Event.hasBytesAvailable:
      let inStream = aStream as? InputStream
      _readData(aStream: inStream!)
      var buffer = [UInt8](repeating: 0, count: 2048)
      var data = Data()
      while ((inStream?.hasBytesAvailable)!)
      let bytesRead: Int = inStream!.read(&buffer, maxLength: buffer.count)
      if bytesRead >= 0
      data.append(buffer, count: bytesRead)



      print("data.hexEncodedString()")
      print(data.hexEncodedString())

      case Stream.Event.hasSpaceAvailable:
      print("Send the next queued command")
      default:
      print("Default")





      var readData : NSMutableData!



      public func _readData(aStream:InputStream)
      print("Trying to read data")
      let INPUT_BUFFER_SIZE = 65536
      let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: INPUT_BUFFER_SIZE)
      while(aStream.hasBytesAvailable)
      let bytesRead = aStream.read(buf, maxLength: INPUT_BUFFER_SIZE)
      if readData == nil
      readData = NSMutableData()

      readData.append(buf, length: bytesRead)

      if readData != nil
      let data: NSData = readData
      let count = data.length / MemoryLayout.size(ofValue: UInt8())
      var array = [UInt8](repeating: 0, count: count)
      data.getBytes(&array, length:count * MemoryLayout<UInt8>.size)
      print("Data Received :: (readData)")
      let dataString = String(data: data as Data, encoding: .utf8)!
      print("Data Received dataString:: (dataString)")




      I am getting the result as




      "CALL 60:a3:7d:38:ff:b3 1234 RFCOMM"




      So we are unable to read actual data from the device. Are we going in the right direction? Do require any other scenario or further steps to achieve this?










      share|improve this question
















      I have a specific requirement for communication between iOS devices and third-party Bluetooth devices.



      I have successfully established a connection with the device(MFI enabled device ) using EAAccessory Framework using the following code.



      let accessory = EAAccessoryManager.shared().connectedAccessories.first!
      session = EASession(accessory: accessory, forProtocol: “protocol string”)
      session?.outputStream?.delegate = self
      session?.outputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
      session?.outputStream?.open()
      session?.inputStream?.delegate = self
      session?.inputStream?.schedule(in: RunLoop.main, forMode: RunLoopMode.defaultRunLoopMode)
      session?.inputStream?.open()
      if availableAccessories.count>0
      print("Detected Device")

      else
      print("Not detected")



      Now we need to make the iOS device to the listerning state to read the transmitted data by health Bluetooth device.



      How can we change the iOS device to the listening state? We used the following code.



      func stream(_ aStream: Stream, handle eventCode: Stream.Event) 
      switch eventCode
      case Stream.Event.hasBytesAvailable:
      let inStream = aStream as? InputStream
      _readData(aStream: inStream!)
      var buffer = [UInt8](repeating: 0, count: 2048)
      var data = Data()
      while ((inStream?.hasBytesAvailable)!)
      let bytesRead: Int = inStream!.read(&buffer, maxLength: buffer.count)
      if bytesRead >= 0
      data.append(buffer, count: bytesRead)



      print("data.hexEncodedString()")
      print(data.hexEncodedString())

      case Stream.Event.hasSpaceAvailable:
      print("Send the next queued command")
      default:
      print("Default")





      var readData : NSMutableData!



      public func _readData(aStream:InputStream)
      print("Trying to read data")
      let INPUT_BUFFER_SIZE = 65536
      let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: INPUT_BUFFER_SIZE)
      while(aStream.hasBytesAvailable)
      let bytesRead = aStream.read(buf, maxLength: INPUT_BUFFER_SIZE)
      if readData == nil
      readData = NSMutableData()

      readData.append(buf, length: bytesRead)

      if readData != nil
      let data: NSData = readData
      let count = data.length / MemoryLayout.size(ofValue: UInt8())
      var array = [UInt8](repeating: 0, count: count)
      data.getBytes(&array, length:count * MemoryLayout<UInt8>.size)
      print("Data Received :: (readData)")
      let dataString = String(data: data as Data, encoding: .utf8)!
      print("Data Received dataString:: (dataString)")




      I am getting the result as




      "CALL 60:a3:7d:38:ff:b3 1234 RFCOMM"




      So we are unable to read actual data from the device. Are we going in the right direction? Do require any other scenario or further steps to achieve this?







      ios swift bluetooth eaaccessory






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 16:44









      rmaddy

      239k27311376




      239k27311376










      asked Nov 12 '18 at 11:50









      Chinnu VijayanChinnu Vijayan

      12




      12






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261568%2funable-to-read-the-data-from-third-party-device-with-externalaccessory-communica%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261568%2funable-to-read-the-data-from-third-party-device-with-externalaccessory-communica%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20