visit
First, you need to have a prototype board with the required hardware (at least a lightning connector). Then the goal is just to do a simple project, that can light a LED for instance, or compute an addition.
Apple provides a great in Objective-C, don’t forget to turn on Wireless Accessory Configuration in the capabilities of your target. This project demonstrates the basic use of ExternalAccessory.framework
. The protocol used to communicate with your accessory is iAP2 (iPod Accessory Protocol 2). It’s the same protocol on Bluetooth.
EAAccessoryManager
: responsible to give you the list of accessories (plugged on the lightning port, via bluetooth etc..) your app can connect to.EAAccessory
: an accessory connected to your iPhone ( not necessarily connected to your app). It has property like name, manufacturer, serial number etc..EASession
: Responsible for the connection and exchanging data with the accessory
The outputStream
and inputStream
inherit from [NS]Stream
. Basically you will write data on the outputStream
which your hardware device can read and it can send you data on the inputStream
, which will be read by your app.
Note: The output & input streams are scheduled on the main thread. We tried to scheduled them on a background queue, it may work for a simple project, but in the end we didn’t received / send all the data. I recommend to schedule them on the main thread and dispatch the parsing and serialization on background queues.
Data exchange between your app and your device This is how you write and read bytes:
At this point it may not work on the first time, you are sending data, don’t really know if your hardware received them. Your app is supposed to get some data, but nothing on the input stream… You need to investigate, to understand why your data are not sent or retrieved. Depending on your project you may need a and an ATS to analyse the bytes transmitted between the iPhone and your hardware device in real time. Basically you will have access to all the bytes that are going from and to the lighting port in real time. I found really useful to read, parse (with regex) and understand all the data sent and received. If your cables between the ATS, the beagle, your mac, iPhone and the hardware board are too long or of poor quality, it may not work (seriously, we had this problem with too long cables).
Sending and receiving bytes can become a nightmare if you don’t have a good specification and you stick to it. Organising bytes as frames is really helpful, here is how we organised our data exchange at Prynt:
A packet sent or received on the input/output stream
0x0F
and 0xF0
for the sync bytes, but you can choose any valueHaving bytes organised as a frame is easier to parse, one thing that is helpful for us having our model made of UInt8
(= a byte), like for instance, the BatteryLevel
:
And of course, the parsing and serialization of those data should be well tested.