omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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?

    Pythonista
    7
    21
    16532
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ywangd
      ywangd last edited by ywangd

      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.

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by omz

        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
        
        1 Reply Last reply Reply Quote 1
        • ywangd
          ywangd last edited by ywangd

          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.

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • ywangd
              ywangd last edited by

              @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?

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB last edited by

                have youe tried the cb module?

                ywangd 1 Reply Last reply Reply Quote 0
                • omz
                  omz last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • omz
                    omz last edited by omz

                    @JonB Using the cb module is unfortunately not an option for a Bluetooth 2.1 MFi device. It only works with Bluetooth 4 (BLE).

                    1 Reply Last reply Reply Quote 0
                    • ywangd
                      ywangd @JonB last edited by

                      @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 ...

                      1 Reply Last reply Reply Quote 0
                      • ywangd
                        ywangd last edited by

                        @omz

                        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 of session. Is this because that the external framework setting is not in Pythonista's info.plist?

                        1 Reply Last reply Reply Quote 0
                        • Gerzer
                          Gerzer last edited by

                          @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.

                          1 Reply Last reply Reply Quote 0
                          • omz
                            omz last edited by

                            @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.

                            1 Reply Last reply Reply Quote 0
                            • Webmaster4o
                              Webmaster4o last edited by

                              @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.

                              1 Reply Last reply Reply Quote 0
                              • Webmaster4o
                                Webmaster4o last edited by Webmaster4o

                                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

                                ywangd 1 Reply Last reply Reply Quote 0
                                • ywangd
                                  ywangd @Webmaster4o last edited by

                                  @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.

                                  1 Reply Last reply Reply Quote 0
                                  • Webmaster4o
                                    Webmaster4o last edited by

                                    That's a shame.

                                    1 Reply Last reply Reply Quote 0
                                    • Gerzer
                                      Gerzer last edited by

                                      @omz Great, thanks!

                                      1 Reply Last reply Reply Quote 0
                                      • Webmaster4o
                                        Webmaster4o last edited by

                                        Using my raspberry pi as a bridge to EV3 now.

                                        1 Reply Last reply Reply Quote 0
                                        • ccc
                                          ccc last edited by ccc

                                          Do you guys have a repo of code for connecting Pythonista --> Raspberry Pi --> Lego EV3? This would be interesting to collaborate on.

                                          1 Reply Last reply Reply Quote 0
                                          • Webmaster4o
                                            Webmaster4o last edited by

                                            No, I'm using https://github.com/topikachu/python-ev3 with the ev3dev software on the ev3.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors