看了幾天,發現ArduinoFrimata 這個project 使用的 AndroidSerialDriver 是2年前的版本
爬過最新版的AndroidSerialDriver 之後,把ArduinoFrimata 修改如下:
private UsbSerialDriver usb_driver; // 改宣告以下變數
private UsbManager usb_manager;
private UsbSerialPort usb_port;
public ArduinoFirmata(android.app.Activity context){
//this.context = context;
//this.usb = UsbSerialProber.acquire(manager); //原本是這樣寫
this.usb_manager = (UsbManager) context.getSystemService(Context.USB_SERVICE); // 以下是這次修改的
List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(this.usb_manager);
if (availableDrivers.isEmpty()) {
return;
}
// Open a connection to the first available driver.
this.usb_driver = availableDrivers.get(0);
}
public void connect() throws IOException, InterruptedException{
if(this.usb_driver == null) throw new IOException("device not found"); // 從這邊開始修改
// Find all available drivers from attached devices.
UsbDeviceConnection connection = this.usb_manager.openDevice(this.usb_driver.getDevice());
if (connection == null) {
// You probably need to call UsbManager.requestPermission(driver.getDevice(), ..)
return;
}
// Read some data! Most have just one port (port 0).
this.usb_port = this.usb_driver.getPorts().get(0); //其他this.usb 也都更正為 usb_port 格式
try{
this.usb_port.open(connection);
this.usb_port.setBaudRate(9600);
Thread.sleep(3000);
}
catch(InterruptedException e){
throw e;
}
catch(IOException e){
throw e;
}
if(this.th_receive == null){
this.th_receive = new Thread(new Runnable(){
public void run(){
while(isOpen()){
try{
byte buf[] = new byte[4096];
int size = usb_port.read(buf, 100);
SetLog("Read:"+HexDump.dumpHexString(buf));
if(size > 0){
for(int i = 0; i < size; i++){
processInput(buf[i]);
}
}
Thread.sleep(10);
}
catch(IOException e){
close();
if(handler!=null) handler.onClose();
}
catch(InterruptedException e){
if(handler!=null) handler.onError(e.toString());
}
}
}
});
this.th_receive.start();
}
for (byte i = 0; i < 6; i++) {
byte[] writeData = {(byte)(REPORT_ANALOG | i), 1};
write(writeData);
}
for (byte i = 0; i < 2; i++) {
byte[] writeData = {(byte)(REPORT_DIGITAL | i), 1};
write(writeData);
}
}
總之,還有原本的版本是引用 google code 作為 submodule 我試著換成目前 github 的版本
應該會持續丟在這邊更新:https://github.com/jyyan/ArduinoFirmata-Android
我們明天見