Wednesday, May 27, 2009

few notes about android bluetooth

bluez version3.36 source code in Android git
git://android.git.kernel.org/platform/external/bluez.git

start bluetooth manually
Pull high in BT power

#echo 1 > /sys/class/rfkill/rfkill0/state


Load BT firmware to hci stack
# hciattach -n -s 115200 /dev/ttyHS0 texas 4000000 flow

Texas module LMP version : 0X3
Texas module LMP sub-version : 0X1126
internal version freeze: 38
software version: 2
chip: brf6300 (4)
Setting speed to 4000000
Texas speed changed to 4000000.
Loading firmware from /etc/firmware/brf6300.bin...
Firmware load successful.

Start HCI device
# hciconfig hci0 up


Start HCI daemon
# hcid -s -n -f /etc/bluez/hcid.conf

hcid[307]: Bluetooth HCI daemon
hcid[307]: Device hci0 has been activated

Gettingthe current values from dbus:

# dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter.GetMode
# dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter.GetDiscoverableTimeout

Changing to discoverable mode and setting the timeout to 60 seconds

# dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter.SetMode string:discoverable
# dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter.SetDiscoverableTimeout uint32:60


check bluetooth is On or Off
# ps | grep hci

325 1002 744 S /system/bin/hciattach -n -s 115200 /dev/ttyHS0 texas
326 1002 1608 S /system/bin/hcid -s -n -f /etc/bluez/hcid.conf

bluetooth configuration file:
/etc/bluez/hcid.conf

hcid.conf manual
http://linux.die.net/man/5/hcid.conf

check bluetooth device in detail
# hciconfig -a

hci0: Type: UART
BD Address: 00:22:A5:B8:AD:65 ACL MTU: 1021:4 SCO MTU: 180:4
UP RUNNING PSCAN
RX bytes:2271 acl:18 sco:0 events:66 errors:0
TX bytes:1156 acl:16 sco:0 commands:39 errors:0
Features: 0xff 0xff 0x2d 0xfe 0x9b 0xf9 0x00 0x80
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH HOLD SNIFF PARK
Link mode: SLAVE ACCEPT
Name: 'g1mokoko'
Class: 0x48010c
Service Classes: Capturing, Telephony
Device Class: Computer, Laptop
HCI Ver: 2.0 (0x3) HCI Rev: 0x0 LMP Ver: 2.0 (0x3) LMP Subver: 0x1126
Manufacturer: Texas Instruments Inc. (13)

# dmesg | grep Blue

[ 5.751220] Bluetooth: HCI UART driver ver 2.2
[ 5.751525] Bluetooth: HCI H4 protocol initialized
[ 5.751831] Bluetooth: HCILL protocol initialized
[ 5.767395] Bluetooth: L2CAP ver 2.11
[ 5.767669] Bluetooth: L2CAP socket layer initialized
[ 5.768005] Bluetooth: SCO (Voice Link) ver 0.6
[ 5.768524] Bluetooth: SCO socket layer initialized
[ 5.782531] Bluetooth: RFCOMM socket layer initialized
[ 5.782928] Bluetooth: RFCOMM TTY layer initialized
[ 5.783599] Bluetooth: RFCOMM ver 1.10
[ 5.783905] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[ 5.784210] Bluetooth: HIDP (Human Interface Emulation) ver 1.2

Scan nearby bluetooth devices
# hcitool scan

Scanning ...
00:22:43:C6:5B:B9 daydreamer-0
00:18:C5:42:18:78 Erin-Nokia N73
00:1E:52:6D:E8:E9 erin's keyboard
00:0F:79:08:D4:05 GgBlu
00:07:61:4B:20:3B Bluetooth Travel Mouse

Check what service it has: AVRCP,A2DP,HandsFree, Headset
# sdptool browse local

Browsing FF:FF:FF:00:00:00 ...
Service Name: Audio Source
Service RecHandle: 0x10000
Service Class ID List:
"Audio Source" (0x110a)
Protocol Descriptor List:
"L2CAP" (0x0100)
PSM: 25
"AVDTP" (0x0019)
uint16: 0x100
Profile Descriptor List:
"Advanced Audio" (0x110d)
Version: 0x0100

Service Name: AVRCP TG
Service RecHandle: 0x10001
Service Class ID List:
"AV Remote Target" (0x110c)
Protocol Descriptor List:
"L2CAP" (0x0100)
PSM: 23
"AVCTP" (0x0017)
uint16: 0x100
Profile Descriptor List:
"AV Remote" (0x110e)
Version: 0x0100

Service Name: Voice Gateway
Service RecHandle: 0x10002
Service Class ID List:
"Headset Audio Gateway" (0x1112)
"Generic Audio" (0x1203)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 11
Profile Descriptor List:
"Headset" (0x1108)
Version: 0x0100

Service Name: Voice Gateway
Service RecHandle: 0x10003
Service Class ID List:
"Handsfree Audio Gateway" (0x111f)
"Generic Audio" (0x1203)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 10
Profile Descriptor List:
"Handsfree" (0x111e)
Version: 0x0105

Connect to bluetooth keyboard
add these lines to /etc/bluez/hcid.conf
device 00:1E:52:6D:E8:E9 {
name “Bluetooth Keyboard”;
auth enable;
encrypt enable;
}
# hidd --connect 00:1E:52:6D:E8:E9


Connect to bluetooth mice
add these lines to /etc/bluez/hcid.conf
device 00:07:61:4B:20:3B {
name “Bluetooth Mouse”;
}
# hidd --connect 00:07:61:4B:20:3B


Confirm the connections
hidd --show

00:1E:52:6D:E8:E9 Bluetooth HID Boot Protocol Device [0000:0000] connected [boot-protocol]
00:07:61:4B:20:3B Bluetooth HID Boot Protocol Device [0000:0000] connected [boot-protocol]

Tuesday, May 26, 2009

YouTube on Android

From Android supported media formats, we know they only support 3gp and mp4 formats now. There is a particular YouTube application on Android, so I needn't open Android Browser to watch YouTube video. Why I can watch YouTube video without any problem? The reason is from gdata youtube API. When we make a search in YouTube, they can only display mp4 videos files for searching result. I can tell it from android's logs. Below texts are from ddms logcat:


05-27 11:24:06.604: DEBUG/youtube(1603): VideoQuery.run url: /feeds/api/videos?vq=blackandwhite+ep12&format=2&restriction=tw this: com.google.android.youtube.YouTubeActivity$SearchVideoQuery@4364bb78

05-27 11:24:06.604: DEBUG/youtube(1603): YouTubeQueryWorker url:http://gdata.youtube.com/feeds/api/videos?vq=blackandwhite+ep12&format=2&restriction=tw&start-index=1&max-results=9 mStartingIndex: 0

We can find it made a search from below URL and it has some paramaters. The detailed reference guide is here.

q: The q parameter specifies a search query term
format: The format parameter specifies that videos must be available in a particular video format.
restriction: The restriction parameter identifies the IP address that should be used to filter videos that can only be played in specific countries.
start-index: The start-index parameter specifies the index of the first matching result that should be included in the result set.
max-results: The max-results parameter specifies the maximum number of results that should be included in the result set.

Let's verify it in Android Dev Phone!

1. search video from YouTube application: (play successfully!)



2. search video from Android Browser application: (cannot play!)




PS. there is another interesting trick! Based on youtube API, the valid formats are 1, 5, 6. If I click that link from my laptop Firefox browser, I will see nothing. Unless, I change the format to 1, 5, or 6.

Friday, May 22, 2009

playing with Android Dev Phone

Finally I got an Android Dev phone this week and start to have fun with it. The first thing is I do is to flash it with the newest android 1.5 system images. :)

* Flash the radio and system images
Official images from HTC: just follow their introduction, step by step, then all done. Or, build yourself! Yup, we can build the image from android source code too. Here is a document from android site. It show
s how to build the Android source and deploy it to Dev Phone. If you follow it, you will meet few problems. I solved it by this blog article.

* Busybox
We all know the toolbox in Android environment is too limited. Therefore, I download a busybox for Android from this site. Then, I can use a lot of BASIC commands in adb shell now.

* Bluetooth
Android is based on Bluez 3.36 version, but I found it doesn't have some useful bluez utils in official image. Therefore, I make bluez library and utility myself. Android provides their Bluetooth introduction and a good Bluetooth diagram here. I added "BOARD_HAVE_BLUETOOTH := true" one line in BoardConfig.mk file and typed 'mm' in 'mydroid/external/bluez/' folder. Then it would build bluez related library and utilities. I use 'adb push' to Dev Phone. If you find the permission problem, you can use 'mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system', it can make 'system' partition be writable. Or you should use 'chmod' to modify system folder permission.



* Connect to Bluetooth stereo headphone

1. make this BT device be discoverable (check its manu
al)
2. on Dev phone, Wireless controls, turn Bluetooth 'On'
3. on Dev phone, Bluetooth settings, choose 'Scan for devices'
4. see a headset icon 'GgBlu' and click to Pair with it
5. Done! it shows 'Connected to phone and media audio'
6. Play music and make an incoming call
7. it can stop the music and display incoming call screen
8. after finished the phone call, it can continue to play music successfully



* Connect to the bluetooth mice and keyboard

Just follow this discussion and you can connect with BT mice and keyboard! Ha, I can write long email and blog article via my apple BT keyboard now.




Tuesday, May 5, 2009

Hello 0xLab android application

I created an android application 'Hello 0xLab' to display all 0xLab team member information. It looks like a business card, but it is an android apk file. User can download it and install it to Android cell phone. It collects 8 people photo images, phone number, email address and blog web URL.

For me, it's my first android application. I hope it could be fun and also be meaningful for 0xLab. I read HelloAndroid book source code and Android development guides. I choose two topics 'WebView' and 'Intent' to practice.

WebView
Android Web Browser is based on Webkit engine, like Qt4 Web Browser. There is a WebView widget in Android API and we can use it as a Web Browser. Therefore, I put a half screen page as a Web browser container. The WebView can load the content from local files stored in 'assets' folder and also can load any web page from the Web. You can write Java script in your html file and interact it with your application too. I modified my application based on a sample code from an Android blog article 'WebViewDemo'. In each person name card, you can see a 0xLab logo in upper part. If you click it, it would change to this person's photo.




Intent

From Android Dev guide, three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents. I can use it to trigger Android applications plus information data I add. Therefore, I put three image buttons to interact with other Android phone applications. Click the first one can launch Android Dialer with member's phone number. Click the second one can launch Android Email composer with member's email address. If you click the last one, the WebView would reload to member's blog website.


Are you curious what 0xLab team member look like? Would you like to contact 0xLab directly? Download it and install it to your Android phone! If you are interested how it works, you could download all source code. I created a project 'hello0x1ab' in google code site too.

Friday, May 1, 2009

why we call '0xLab'?


我們這個TEAM的成員 有一段的時間
每週二晚上都會參加TOSSUG在MIX的聚會
話說有天晚上 我們正在票選LAB的名稱
但總選不到響亮又要好記的名字
有人愛中國禪風 有人愛KUSO的阿宅
有人對機器人有莫名的愛戀
又因為我們喜歡做open source的專案
因此我們很希望名字能帶有OPEN的感覺

我開著stardict查單字 想著要有服務社群的概念
從cross十字架開始關聯 想到第1個電腦井字遊戲
那不是就是圈叉圈的標記
腦海中回憶起在倫敦時橋上看見的OXO tower
好吧 那不如取名 OX Lab 吧
一講出口 阿宅們就開始KUSO
OXLab --> 0XLab --> 0xlab --> 0x1ab --> 427
嗯 那就是四月二十七日揭竿起義啦
而我們很可愛的LOGO 是從團員中唯一有美學概念的walkingice
花了他幾個晚上繪畫出來的 外星人 很有我們的FU~