12月 08

raspberry pi 连接android手机

  之前测试wifi不稳定,网上看了看gprs的开发模块还挺贵。打算将我的htc手机连接到raspberry pi的板子上,看看能不能成功。
  使用命令查看是否已经认到手机。
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 008: ID 0bb4:0cad HTC (High Tech Computer Corp.)
Bus 001 Device 009: ID 0ac8:301b Z-Star Microelectronics Corp. ZC0301 Webcam

  编写文件,内容里面的串是根据lsusb中的值得来的。
$ sudo vim /etc/udev/rules.d/99-android.rules
SUBSYSTEM==”usb”, ATTRS{idVendor}==”0bb4″, ATTRS{idProduct}==”0cad”,MODE=”0666″

$ sudo chmod a+rx /etc/udev/rules.d/99-android.rules
$ sudo service udev restart

下载编译好的adb工具
http://fieldeffect.info/w/Beagleboard_Notes?action=AttachFile&do=view&target=adb
本地下载

传到系统上,使用命令进入android手机
$ chmod a+x adb
$ ./adb shell

  成功了。这样就可以用手机做代理进行操作,如果写个能时时交互数据的服务,很多传感器的钱都剩下了。

http://huulivoide.pp.fi/Arch/arm/也有编译好的adb工具,我没有测试

12月 05

go语言跑在Raspberry pi 上

  之前go语言在HTC的android手机上运行正常,所以在raspberry pi上也应该没有问题。所以测试一下,如果可以又多了一种开发方法。下载go1.0.3.

安装需要的包
$ sudo apt-get install mercurial bison gcc libc6-dev ed gawk make git

配置环境变量,将以下配置放在profile文件的最底部,为了方便直接用go源码编译,直接放在家目录里。
$ sudo vi /etc/profile
export GOARM=5
export GOROOT=/home/pi/go
export GOARCH=arm
export GOOS=linux
export PATH=$PATH;/home/pi/go/bin/

开始编译
$ cd go/src/
$ time ./all.bash

经过漫长的等待终于编译完了,耗时如下。
real 49m48.591s
user 44m33.760s
sys 2m7.540s

直接用之前的代码测试http://www.simonzhang.net/?p=1346。运行成功。

4月 13

nginx 区分移动设备配置

  当期需求,要将来访请求区分开,pc使用页面展示,android和iphone转向相应的页面,配置如下,文中连接只是为了演示:
server {
listen 80;
access_log off;
server_name simonzhang.net www.simonzhang.net ;
modern_browser unlisted;
set $ismob 0;
if ( $http_user_agent ~ “^((.*iPhone.*)|(.*iOS.*)|(.*Safari.*))$”) {
set $ismob 1;
rewrite ^(.*) http://www.simonzhang.net/?page_id=782 break;
}
if ( $http_user_agent ~ “^((.*Android.*))$”) {
set $ismob 1;
rewrite ^(.*) http://www.pixshow.net/robots.txt break;
}
if ( $http_user_agent ~ “^((.*Windows NT.*)|(.*Intel Mac OS.*))$”) {
set $ismob 1;
rewrite ^(.*) http://www.simonzhang.net/ break;
}
rewrite ^(.*) http://www.pixshow.net/ break;
}

注:此类配置并不能百分之百区分出设备,毕竟有写设备还是不守规矩。

4月 07

android的monkey测试简单记录

  需要对android的一个软软件做个疲劳测试,使用android SDK自带的工具最为方便。所就开始做monkey测试,调用和回播的脚本是从网上搜集而来,自己做了小修改。原始脚本的网页url忘记记录,所以不能提供参考网连接。下面开始对我所做的简单测试做个记录。
  首先是在系统上安装java运行环境,这个比较简单。下载android SDK包,直接解压后就可以使用非常方便。
  进入android-sdk-windows/tools目录,在其下面写两个脚本。
录脚本的启动脚本名monkey_recorder.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder

device = mr.waitForConnection()
recorder.start(device)

回放脚本名为monkey_playback.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from com.android.monkeyrunner import MonkeyRunner

# The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command.  The line is split into 2
# parts with a | character.  Text to the left of the pipe denotes
# which command to run.  The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command.  In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command. 

# Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
    'TOUCH': lambda dev, arg: dev.touch(**arg),
    'DRAG': lambda dev, arg: dev.drag(**arg),
    'PRESS': lambda dev, arg: dev.press(**arg),
    'TYPE': lambda dev, arg: dev.type(**arg),
    'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
    }

# Process a single file for the specified device.
def process_file(fp, device):
    for line in fp:
        (cmd, rest) = line.split('|')
        try:
            # Parse the pydict
            rest = eval(rest)
        except:
            print 'unable to parse options'
            continue

        if cmd not in CMD_MAP:
            print 'unknown command: ' + cmd
            continue
        CMD_MAP[cmd](device, rest)
        
def go(file,num):
    n = num
    device = MonkeyRunner.waitForConnection()
    for i in range(int(n)):
        print 'now %s'%i
        fp = open(file, 'r')
        process_file(fp, device)
        fp.close();

def main():
    file = sys.argv[1]
    try :
        number = sys.argv[2]
    except :
        number = 1
    go(file,number)
 
if __name__ == '__main__':
    main()

  到此环境和脚本就算部署完了。下面开始记录脚本使用。
  首先将手机和电脑用usb线连好,使用“cmd”调出命令行,然后进入“android-sdk-windows/tools”目录。输入命令:
monkeyrunner.bat monkey_recorder.py
如果没有报错,并且出现以下界面,就可以开始录制脚本了。

  “wait”为每步操作间的等待时间。“Press a Button“发送,MENU,HOME,or SEARCH 按钮。“Type Something”为输入文字。“Fling”为模拟滑动操作。“Export Actions”为保存刚才录制的脚本。最后一个为刷新。   
  保存的脚本可以直接用文本编辑器手动编辑。假如我们录制的脚本为text.txt。下一步就是回放脚本,使用命令为:
monkeyrunner.bat monkey_playback.py test.txt 100
  最后的数字是将这个脚本循环100次。
  我只需要到此即可了。如果需要抓图片\照相\判断等高级功能,将来需要时再学习。