mergAccessory

mergAccessory is an iOS external for connecting to and communicationg with accessory hardware using the External Accessory framework.

Each device in the MFi program has a name and one or more protocols it supports. For your app to see the device it needs to state what protocols it supports. To do this you need to edit the plist file wich is found at /Applications/LiveCode 5.5.app/Contents/Tools/Runtime/iOS/Device-5_1/Settings.plist. Add the protocols your application supports under the UISupportedExternalAccessoryProtocols key. For example to connect to the BlueBamboo p25i add:

UISupportedExternalAccessoryProtocols
 
   com.bluebamboo.p25i

Your application will also need to be registered with the manufacturer and Apple.

mergAccessory Syntax

function mergAccessoryNames

Returns the names of any devices connected to the device with protocols your supports.

function mergAccessoryProtocols pName

Syntax example:

if "p25i" is among the lines of mergAccessoryNames() then
   if "com.bluebamboo.p25i" is among the lines of mergAccessoryProtocols("p25i") then
      printDocket
   else
      answer "Printer not connected"
   end if
end if

Returns a list of protocols that both the accessory and your app support

command mergAccessoryOpenSession pNameAndProtocol,pCallbackHandler

Sends a message back to the calling object when the session is open. The message has a single parameter which is the same as pNameAndProtocol.

Parameters:

  • pNameAndProtocol - The name as returned by mergAccessoryNames()|the protocol as returned by mergAccesoryProtocols(). For example, "p25i|com.bluebamboo.p25i"
  • pCallbackHandler - A handler name.

command mergAccessoryWrite pNameAndProtocol,pData,pCallbackHandler

Sends a message back to the calling object when the write is completed. The message has a single parameter which is the same as pNameAndProtocol.

Parameters:

  • pNameAndProtocol - The name as returned by mergAccessoryNames()|the protocol as returned by mergAccesoryProtocols(). For example, "p25i|com.bluebamboo.p25i"
  • pData - The data to write to the accessory. This obviously needs to conform to the protocol and you should be able to get the details of this from the manufacturer.
  • pCallbackHandler - A handler name.

command mergAccessoryRead pNameAndProtocol,pLength,pCallbackHandler

Sends a message back to the calling object when the write is completed. The message has two parameters. The first is the same as pNameAndProtocol. The second is the data that was read from the accessory.

Parameters:

  • pNameAndProtocol - The name as returned by mergAccessoryNames()|the protocol as returned by mergAccesoryProtocols(). For example, "p25i|com.bluebamboo.p25i"
  • pLength - The number of bytes to read (integer). If you specify 0 here then any available data will be read. The callback won't be sent until the length of data specified is reached.
  • pCallbackHandler - A handler name.

Syntax example:

on printDocket
   mergAccessoryOpenSession "p25i|com.bluebamboo.p25i","sessionOpen"
end printDocket
 
on sessionOpen pNameAndProtocol
   mergAccessoryWrite pNameAndProtocol,"UfwàD"&fld "write","writeCompleted"
end sessionOpen
 
on writeCompleted pNameAndProtocol
   mergAccessoryRead pNameAndProtocol,0,"readCompleted"
end writeCompleted
 
on readCompleted pNameAndProtocol,pData
   if byte 5 of pData = numToByte(3) and byte 6 of pData = numToByte(0) then
      answer "print complete"
      mergAccessoryCloseSession pName
   end if
end readCompleted

Notifications

command mergAccessoryRegisterForNotifications

Registers the calling control to receive the mergAccessoryConnected and mergAccessoryDisconnected messages. Only one control in your stack can do this.

message mergAccessoryConnected pName

Sent to the control that registered for notifications when an accessory with a supported protocol connects to the device.

message mergAccessoryDisonnected pName

Sent to the control that registered for notifications when an accessory with a supported protocol connects to the device.

Syntax example:

on openCard
   if the environment = "mobile" then
      mergAccessoryRegisterForNotifications
   end if
end openCard
 
on mergAccessoryConnected pName
   if pName = "p25i" then
      show button "printDocket"
   end if
end mergAccessoryConnected
 
on mergAccessoryDisconnected pName
   if pName = "p25i" then
      hide button "printDocket"
   end if
end mergAccessoryDisconnected