Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
External Accessory Framework?
-
The following code does not work
e = ObjCClass('EAAccessoryManager')
The error message is
ValueError: no Objective-C class named 'EAAccessoryManger' found
. Is it because the framework is not linked to Pythonista? Is there any other way that I can make it work besides asking @omz to link the framework?I am trying to talk to a Lego Mindstorms brick using the bluetooth connection. The brick uses bluetooth 2.1 and is a MFi device. I did some research and it seems that EA framework is the way to go.
-
I'm not sure if what you're trying to achieve is possible at all, or if the app would need special entitlements to talk to an MFi device.
However to get the class name to resolve, all you need to do is load the framework, like this:
from objc_util import * NSBundle = ObjCClass('NSBundle') ea_framework = NSBundle.bundleWithPath_('/System/Library/Frameworks/ExternalAccessory.framework') ea_framework.load() # This should work now: EAAccessoryManager = ObjCClass('EAAccessoryManager') print EAAccessoryManager
-
Thanks @omz !
The above code works. I was able to chain two more method calls as follows:
print EAAccessoryManager.sharedAccessoryManager().connectedAccessories()
It returns an empty tuple as I don't currently have the Lego brick around. I'll do some more tests later.
As I understand it, in order to talk to the Lego brick, following key/value settings are needed in the app's
info.plist
file.<key>UISupportedExternalAccessoryProtocols</key> <array> <string>COM.LEGO.MINDSTORMS.EV3</string> </array>
I thhink it can probably be set programmatically with following objc code?
session = [[EASession alloc] initWithAccessory:accessory forProtocol:protocolString];
which is something can be translated using
objc_util
.It is just my guess though. I'll give it a try later and report the results.
-
As I understand it, in order to talk to the Lego brick, following key/value settings are needed in the app's info.plist file.
That's what I suspected. The thing is, there is absolutely no way for you to change what's in the app's Info.plist.
-
@omz Are you saying the protocol string MUST be declared in the plist file. Otherwise it is impossible to set the it programmatically within the app?
-
have youe tried the
cb
module? -
Are you saying the protocol string MUST be declared in the plist file. Otherwise it is impossible to set the it programmatically within the app?
Yes, that's one of the security mechanisms of iOS. Certain things need to be declared in the Info.plist file and can't be changed at runtime. This is also used for things like location access, background audio, push notifications, etc.
-
@JonB Using the
cb
module is unfortunately not an option for a Bluetooth 2.1 MFi device. It only works with Bluetooth 4 (BLE). -
@JonB said:
have youe tried the
cb
module?cb
module only works with Bluetooth LE devices (4.0). But the Lego brick is on Bluetooth v2.1 ... -
I tried a bit more with the brick connected to the iPad. I was able to see the brick. But the session creation failed silently by return
None
. The code is as follows (after your sample code):accessories = EAAccessoryManager.sharedAccessoryManager().connectedAccessories() accessory = accessories[0] print accessory EASession = ObjCClass('EASession') session = EASession.alloc().initWithAccessory_forProtocol_(accessory, 'COM.LEGO.MINDSTORMS.EV3') print session
The output is
<EAAccessory: 0x17020a210> { connected: YES connectionID: 27234188 name: MFI Accessory manufacturer: LEGO modelNumber: DM240411 serialNumber: firewareRevision: 1.0.0 harewareRevision: 1.0.0 protocols: ( "COM.LEGO.MINDSTORMS.EV3" ) delegate: (null) } None
Notice that the last
None
is the printing result ofsession
. Is this because that the external framework setting is not in Pythonista'sinfo.plist
? -
@omz Is there a place I can find the
cb
module reference? It doesn't seem to be included in the latest beta's in-app docs. -
@Gerzer Sorry, I didn't notice that I accidentally removed that while moving some things around in the documentation – will be back in the next build.
-
@ywangd if you do get pythonista to talk to a Mindstorms brick, make sure to post the code, I'd love to see what I can do with this! I haven't touched my brick in about a year, but I wonder if I could get pythonista to control the brick's motors, or even monitor sensor input! I'd be really cool to read something like the ultrasonic sensor input on iOS.
-
This guy seems to have it working http://youtu.be/tZn9DQtC0So it looks like he has code running on a Mac that talks to EV3
-
@Webmaster4o said:
This guy seems to have it working http://youtu.be/tZn9DQtC0So it looks like he has code running on a Mac that talks to EV3
This is different from what I'd like to achieve. It used a Mac as a bridge between iOS and EV3 so that iOS was not directly talking to the EV3. I have done similar things as well.
On the iOS side I created a socket client in Pythonista and talk to a server running on Mac that forwards commands from iOS to EV3. A few libraries are available to control an EV3 from a PC. The one I used is called MonoBrick.
What I'd like to do now is to avoid the PC bridge and let iOS directly control the EV3. However, it does NOT seem to be possible from within Pythonista due to the
info.plist
setting. A communication session could NOT be created (though Pythonista was able to discover the existence of EV3). It seems that a dedicated app is needed to achieve the goal. -
That's a shame.
-
@omz Great, thanks!
-
Using my raspberry pi as a bridge to EV3 now.
-
Do you guys have a repo of code for connecting Pythonista --> Raspberry Pi --> Lego EV3? This would be interesting to collaborate on.
-
No, I'm using https://github.com/topikachu/python-ev3 with the ev3dev software on the ev3.