1

In the documentation we have the following commands.

command mergAccessoryRead pNameAndProtocol,pLength,pCallcackHandler
command mergAccessoryWrite pNameAndProtocol,pData,pCallcackHandler

Do they only work with printers or can they be used generally? Examples?

Update 2: I am stuck on mergAccessoryOpenSession. I think no session opened on this case.

Here's my current code.

global pNameAndProtocol
global pData
on preopenstack

   put "xxxx" into pNameAndProtocol
   put "get xxxx" into pData
end preopenstack

command xxxx

   answer pNameAndProtocol
   answer pData   
   printDocket  


end xxxx

on printDocket
   try
      mergAccessoryOpenSession "xxxx","sessionOpen"
      answer "mergAccessoryOpenSession is called..."
   catch someError
      answer "An error on printDocket " &&someError
   end try
end printDocket

on sessionOpen pNameAndProtocol   
   try
      mergAccessoryWrite "xxxx","get xxxx","writeCompleted"
       answer "mergAccessoryWrite is called..."
   catch someError 
      answer "An error on sessionOpen " &&someError
   end try   
end sessionOpen

on writeCompleted pNameAndProtocol   
   try
      mergAccessoryRead pNameAndProtocol,0,"readCompleted"
      answer "mergAccessoryRead is called..."
   catch someError 
      answer "An error on writeCompleted " &&someError
   end try

end writeCompleted

on readCompleted pNameAndProtocol,pData   
      answer "Completed..."   
end readCompleted
JunM
  • 7,040
  • 7
  • 37
  • 58

1 Answers1

1

They work with any Bluetooth or attached hardware accessory. You just need to get the protocol documentation from the manufacturer. The demo prints to a mini Bluetooth printer.

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

EDIT

After a fairly lengthy support incident I worked out the problem here. It seems that callbacks from externals are not queued when there's an answer dialog open and they just get lost never to be executed. So don't use answer dialogs when you're expecting callbacks from an external folks. I have opened this thread on the engine forum to see if there is a solution: http://forums.runrev.com/viewtopic.php?f=66&t=15602

Monte Goulding
  • 2,380
  • 1
  • 21
  • 25
  • If we will use this command mergAccessoryWrite pNameAndProtocol,pData,pCallcackHandler - is there a way we can insert a command from the manufacturer of our device? – JunM Jun 06 '13 at 04:08
  • Yes... pData is what you write to the device... so if your device needs a "HELLO" command then that's what you use foe pData in the command. – Monte Goulding Jun 06 '13 at 05:39
  • To make it simpler, I have a command from the manufacturer of the device. Let's say "xxxx", how do I send this "xxxx" command to my device and read its result? – JunM Jun 06 '13 at 05:39
  • I tried your suggestion and I got an error "invalid number parameters" using try catch. I have only three parameters. Here's my code: `try` `mergAccessoryWrite pNameAndProtocol,"get xxxx","writeCompleted"` `catch someError` `answer "An error on sessionOpen " &&someError` `end try` – JunM Jun 06 '13 at 06:19
  • Hmm... it looks like the correct number of parameters to me... Does your code look like the demo code I've included above? – Monte Goulding Jun 06 '13 at 10:21
  • I've managed to get rid of the error "invalid parameter", my mistake, I was not able to add "0" as one of the parameters of mergAccessoryRead. But another problem I got is, it seems it didn't reached on readCompleted codes. I have this code `on readCompleted pNameAndProtocol,pData` `answer "Completed..."` `end readCompleted`. I haven't received any prompt message "Completed..." – JunM Jun 07 '13 at 02:38
  • I Updated my post, added my current code. I am stuck on mergAccessoryWrite that's why it does not go to readCompleted. Thanks for the help. – JunM Jun 07 '13 at 02:59
  • Should I still need to call sessionOpen, writeCompleted and readcompleted? Or I can only call printDocket? Sorry for asking a lot of questions. Thanks. – JunM Jun 07 '13 at 03:34
  • Delete everything after printDocket in the xxxx command... sessionOpen,writeCompleted,readCompleted are asynchronous callbacks. I'm assuming here that you have used xxxx where you don't want the world to see what device you are working with... Also assuming you've added the appropriate UISupportedExternalAccessoryProtocols key to the plist. – Monte Goulding Jun 07 '13 at 04:53
  • Yes, I just used "xxxx" for an example only. I have tried your solution and it calls `mergAccessoryOpenSession` only and stop there. It does not go to "on sessionOpen pNameAndProtocol" block. I tried calling `mergAccessoryOpenSessions` to display the device name and protocol and it has. Regard ing `UISupportedExternalAccessoryProtocols`, I have it set up because I can get the device name and protocol name. In this case, no sesssion was opened? Can't think why it does not work. Thanks. – JunM Jun 07 '13 at 05:09
  • No errors thrown by `mergAccessoryOpensession`. It just stopped there. I tried adding pop up message inside on sessionOpen before calling `mergAccessoryWrite` but the message doesn't show up. – JunM Jun 07 '13 at 08:27
  • I think you're going to need to send me your stack, plist file and accessory documentation if I'm going to be able to help you any further... I'll also need to know the value returned by mergAccessoryNames() and mergAccessoryProtocols(). – Monte Goulding Jun 08 '13 at 07:15
  • Thanks for the support on this one, accepted/voted your response. – JunM Jun 26 '13 at 01:33