与Arduino和USB主机屏蔽的Android USB附件连接

时间:2015-09-03 08:24:41

标签: android arduino usb

我有以下组件。

设备:

  1. Arduino Uno R3(原创)
  2. USB Host Shield(来自sparkfun.com
  3. ASUS Zenfone 2(Android 5.0版)
  4. 软件:

    1. Arduino IDE(1.6.5)
    2. USB Host Shield(图书馆2.0)
    3. Android Studio(1.3.2)
    4. 我要做的是:

      我正在尝试使用我的Arduino连接Android手机进行简单的数据通信。

      我的问题:

      我不明白什么是错的!我的ADK(来自图书馆)无法通过Arduino USB屏蔽识别我的Android手机。我一直在浪费几个星期:(。 请帮帮我。

      这是我的代码:

      Arduino Sketch:

      #include <adk.h>
      // Satisfy IDE, which only needs to see the include statement in the Uno.
      #ifdef dobogusinclude
      #include <spi4teensy3.h>
      #include <SPI.h>
      #endif
      
      USB Usb;
      ADK adk(&Usb, "DemoTestKit", // Manufacturer Name
        "DemoKit", // Model Name
        "Example sketch for the USB Host Shield",
        "1.0", // Version
        "http://www.example.com", // URL (web page to visit if no installed apps support the accessory)
        "123456789"); // Serial Number (optional)
      uint32_t timer;
      bool connected;
      
      void setup() {
        Serial.begin(115200);
        #if !defined(__MIPSEL__)
          while (!Serial);
          // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
        #endif
        if (Usb.Init() == -1) {
          Serial.print("\r\nOSCOKIRQ failed to assert");
          while (1); // halt
        }
        Serial.print("\r\nArduino Started");
      }
      
      void loop() {
        Usb.Task();
        if (adk.isReady()) {
          Serial.print("\r\nADK Ready");
          if (!connected) {
            connected = true;
            Serial.print(F("\r\nConnected to accessory"));
          }
        } else {
          if (connected) {
            connected = false;
            Serial.print(F("\r\nDisconnected from accessory"));
          }
        }
      }
      

      此处adk.isReady()无效。

      Android代码:-----------

      的AndroidManifest.xml

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.softpider.usb.usbtest" >
        <uses-feature android:name="android.hardware.usb.accessory" />
        <application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" >
          <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
              <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
            </intent-filter>
            <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
              android:resource="@xml/accessory_filter" />
          </activity>
        </application>
      </manifest>
      

      accessory_filter.xml

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
        <usb-accessory model="DemoKit" manufacturer="DemoTestKit" version="1.0"/>
      </resources>
      

      MainActivity.java

      public class MainActivity extends AppCompatActivity {
        public static final boolean D = BuildConfig.DEBUG;
        // This is automatically set when building
        private static final String TAG = "ArduinoBlinkLEDActivity";
        // TAG is used to debug in Android logcat console
        private static final String ACTION_USB_PERMISSION = "com.softpider.usb.usbtest";
        UsbAccessory mAccessory;
        ParcelFileDescriptor mFileDescriptor;
        FileInputStream mInputStream;
        FileOutputStream mOutputStream;
        private UsbManager mUsbManager;
        private PendingIntent mPermissionIntent;
        private boolean mPermissionRequestPending;
        TextView connectionStatus;
        private TextView textView;
        UsbAccessory accessory;
        private UsbDevice device;
      
        private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
          public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
              synchronized (this) {
                accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                  if(accessory != null) {
                    openAccessory(accessory);
                  }
                } else {
                  Log.d(TAG, "permission denied for accessory " + accessory);
                }
                mPermissionRequestPending = false;
              }
            }
            if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) {
              accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
              if (accessory != null) {
                closeAccessory();
              }
            }
          }
        };
      
        @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          connectionStatus = (TextView) findViewById(R.id.connectionStatus);
          textView = (TextView) findViewById(R.id.textView);
          textView.setText("OnCreate");
          mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
          IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
          registerReceiver(mUsbReceiver, filter);
        }
        @Override
        public void onResume() {
          super.onResume();
          // textView.setText("OnResume");
          if (mAccessory != null) {
            setConnectionStatus(true);
            return;
          }
          // Log.i(TAG, "Device List: " + deviceList);
          mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
          UsbAccessory[] accessories = mUsbManager.getAccessoryList();
          // textView.setText(Arrays.toString(accessories));
          HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
          device = deviceList.get("<deviceName>");
          Log.i(TAG, "Accessories List: " + accessories);
          if (accessories != null) {
            if (mUsbManager.hasPermission(accessory)) {
              openAccessory(accessory);
              textView.setText("OnResume IF False");
            } else {
              setConnectionStatus(false);
              textView.setText("OnResume IF Else False");
              synchronized (mUsbReceiver) {
                if (!mPermissionRequestPending) {
                  mUsbManager.requestPermission(device, mPermissionIntent);
                  mPermissionRequestPending = true;
                }
              }
            }
          } else {
            setConnectionStatus(false);
            // textView.setText("OnResume Else False");
            if (D)
              Log.d(TAG, "mAccessory is null");
          }
        }
        private void openAccessory(UsbAccessory accessory) {
          textView.setText("Accessory Open");
        }
        private void setConnectionStatus(boolean connected) {
          connectionStatus.setText(connected ? "Connected" : "Disconnected");
        }
        private void closeAccessory() {
          setConnectionStatus(false);
          textView.setText("Accessory Close");
        }
      }
      

      我无法理解ACTION_USB_PERMISSION字符串是什么?

      private static final String ACTION_USB_PERMISSION = "com.softpider.usb.usbtest";
      

      private static final String ACTION_USB_PERMISSION = "com.softpider.usb.usbtest.USB_PERMISSION";
      

      private static final String ACTION_USB_PERMISSION = "com.softpider.usb.USB_PERMISSION";
      

1 个答案:

答案 0 :(得分:0)

一个古老的问题,但是我现在正在做类似的事情。 因此,我相信您的问题是您没有调用init()方法来初始化ADK。

#include <adk.h>

...

int main(void) {
    init();
    setup();
    for (;;) {
        loop();
    }
}