项目作者: OmarAflak

项目描述 :
A lightweight Android library to communicate with Arduino through usb.
高级语言: Java
项目地址: git://github.com/OmarAflak/Arduino-Library.git
创建时间: 2017-05-21T13:19:55Z
项目社区:https://github.com/OmarAflak/Arduino-Library

开源协议:

下载


Arduino

A lightweight Android library to communicate with Arduino through USB.

Usage

  1. Arduino arduino = new Arduino(Context);
  2. @Override
  3. protected void onStart() {
  4. super.onStart();
  5. arduino.setArduinoListener(new ArduinoListener() {
  6. @Override
  7. public void onArduinoAttached(UsbDevice device) {
  8. arduino.open(device);
  9. }
  10. @Override
  11. public void onArduinoDetached() {
  12. // arduino detached from phone
  13. }
  14. @Override
  15. public void onArduinoMessage(byte[] bytes) {
  16. String message = new String(bytes);
  17. // new message received from arduino
  18. }
  19. @Override
  20. public void onArduinoOpened() {
  21. // you can start the communication
  22. String str = "Hello Arduino !";
  23. arduino.send(str.getBytes());
  24. }
  25. @Override
  26. public void onUsbPermissionDenied() {
  27. // Permission denied, display popup then
  28. arduino.reopen();
  29. }
  30. });
  31. }
  1. @Override
  2. protected void onDestroy() {
  3. super.onDestroy();
  4. arduino.unsetArduinoListener();
  5. arduino.close();
  6. }

Custom vendor id

The library currently filters the vendor id 9025, but you can add your own filter by calling :

  1. Arduino arduino = new Arduino(Context);
  2. arduino.addVendorId(1234);

Custom delimiter while reading

You can set a custom delimiter byte (default is \n) :

  1. Arduino arduino = new Arduino(Context);
  2. arduino.setDelimiter(';');

Arduino Side

Example code which sends back every character received

  1. void setup() {
  2. Serial.begin(9600);
  3. }
  4. void loop() {
  5. if(Serial.available()){
  6. char c = Serial.read();
  7. Serial.print(c);
  8. }
  9. }

Sample Code

See MainActivity.java

Special thanks

This code is using UsbSerial library : https://github.com/felHR85/UsbSerial

License

  1. MIT License
  2. Copyright (c) 2017 Michel Omar Aflak
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.