顯示具有 Android 標籤的文章。 顯示所有文章
顯示具有 Android 標籤的文章。 顯示所有文章

2014年9月18日 星期四

Android Bluetooth Serial Port Profile Bluetooth Device Client


透過簡單的函式使用 spp bluetooth 設備…
    bt_init("HC-05");

將底下程式碼 copy 到 activity 中使用…

    // ======== START Bluetooth functions ======== //
    BluetoothAdapter mmBluetoothAdapter = null;
    BluetoothDevice mmDevice = null;
    BluetoothSocket mmSocket = null;
    OutputStream mmOutputStream = null;
    InputStream mmInputStream = null;
    Thread bt_read = null;

    // try to open a Serial Port Profile Bluetooth Device
    public void bt_init(String device_name) {

        mmBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
       
        if (mmBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth not found", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!mmBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
            Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show();
            return;
        }

        Set paired = mmBluetoothAdapter.getBondedDevices();
       
        if (paired.size() == 0) {
            Toast.makeText(this, "No paired bluetooth devices", Toast.LENGTH_SHORT).show();
            return;
        }
       
        for (BluetoothDevice device : paired) {
            if (device.getName().equals(device_name)) {
                mmDevice = device;
                break;
            }
        }

        if (mmDevice == null) {
            Toast.makeText(this, "Device not found", Toast.LENGTH_SHORT).show();
            return;
        }

        // Serial Port Profile UUID
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
        try {
            mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
            mmSocket.connect();
            mmOutputStream = mmSocket.getOutputStream();
            mmInputStream = mmSocket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Bluetooth connect fail", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO: read data
        bt_read = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        // read a char from bluetooth device
                        if (mmInputStream.available() > 0) {
                            int c = mmInputStream.read();
                            bt_write(c); // ECHO
                        }
                    } catch (IOException ex) {
                        break;
                    }
                }
            }
        });
        bt_read.start();
       
        Toast.makeText(this, "Bluetooth connected", Toast.LENGTH_SHORT).show();
    }

    // TODO: write data
    // write a char to bluetooth device
    void bt_write(int c) {
        try {
            if (mmOutputStream != null)
                mmOutputStream.write(c);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // ======== END Bluetooth functions ======== //


記得 uses-permission 要增加…
    android.permission.BLUETOOTH
    android.permission.BLUETOOTH_ADMIN


2013年9月22日 星期日

自製 Android 遊戲「香蕉檸檬

自製 Android 遊戲「香蕉檸檬」開發完成,日前正式上架,算是我第一個正式上架的遊戲,朋友們有 Android 手機的可以安裝玩看看哦^^~

雖然是殺時間的小小益智遊戲,但也要手腳明快,頭腦清楚才能得高分唷!!!
目前最高分 300 多萬!!!

*記得用 FB 登入 Swarm,才能進入世界排名~
*Google Play 搜尋 「BANANA X LEMON」就可找到~

https://play.google.com/store/apps/details?id=com.longfellow.bananaxlemon&hl=zh-TW

2013年6月29日 星期六

PiPO M3 刷機過程


若沒有要備份原有的 img 可跳到第四點…

一、安裝 android adb tools、android usb driver…

二、下載安裝 Moborobo,安裝 adb Driver


三、下載 romdump
http://android.podtwo.com/romdump/

在 dos command 底下執行下列 scripts

adb push romdump /data/local/
adb shell chmod 04755 /data/local/romdump
adb shell /data/local/romdump


會將 android 裝置 dump 到 sd card 上。

四、下載官方升級工具及韌體…
http://www.pipo.cn/index.php?m=Contact&a=index&id=248&type=3
http://www.pipo.cn/index.php?m=About&a=gujian

五、下載 img 解包打包工具…
http://www.mobile01.com/topicdetail.php?f=605&t=3032201&p=1

六、將官方 img 改名後放到打包工具目錄,執行 RK3066.exe,選擇解開 img

七、作需要的修改

八、再打包

七、使用官方升級工具 update img
 

2013年6月27日 星期四

Android 中如何在自己的 APP 應用程序調用另一個 APP 應用程序

列出手機上安裝的 apk,透過 debug 訊息找到 package name…

                PackageManager pm = getPackageManager();  
                for (ApplicationInfo app : pm.getInstalledApplications(0)) {  
                 Log.d("PackageList", "package: " + app.packageName + ", sourceDir: " + app.sourceDir);  
                }  

使用 Intent 啟動 app…

                PackageManager pm = getPackageManager();  
                Intent intent = pm.getLaunchIntentForPackage("com.example.helloworld");  
                startActivity(intent);  

2013年6月17日 星期一

Google Maps Android API v2 APP 開發步驟

網路上很多 Google Maps Android API v2 相關文章,但有各種不同講法,自己試了 1-2 個禮拜都沒有成功,最後結合網路找的資料跟 android sdk 內附的 maps 範例程式,總算成功試出來了,因此作下記錄。

準備工作…
一、建立 keystore 並取出 SHA1 碼。
 到 jre bin 目錄下 C:\Program Files\Java\jre6\bin\
 建立 keysor…
  keytool -genkey -v -keystore yourkeyname.keystore -alias yourkeyname -keyalg RSA -keysize 2048 -validity 10000
 取出 sha1 碼…
  keytool -list -v -keystore yourkeyname.keystore > save_sha1.txt
  打開 save_sha1.txt 找到 sha1 碼
      SHA1:xx:xx:xx…

二、到 Google apis 申請 API key
 到 https://code.google.com/apis/console/
 Create project
 Services 中 google Maps Android API v2 點一下 off 改成 on
 API Access 中「Create new Android key」
  輸入「sha1碼」+「;」+「your_package_name
  產生出 API key…
   Key for Android apps (with certificates)
   API key:AIzaSyB1yoElxxxxxxxx...

三、打開 Android SDK Manager 安裝  Google Play services,在 extra 底下。
 
四、將  google-play-services_lib import 進 Workspace。
 在 SDK 安裝目錄下 \extras\google\google_play_services\libproject\google-play-services_lib

開發專案…
一、建立新專案。

二、將 google-play-services_lib 函式庫加入專案中。
 project > properties >Android Library , Add。

三、順便將 Android Lint Preferences > Correctness:Messages 中 Missing Translation 改為 Ignore。(忽略多國語言未翻譯的錯誤)

四、修改 AndroidManifest.xml,加入權限及 API key。
 application 內加入 api key…
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
                    android:value="AIzaSyB1yoElxxxxxxxx..."/>

 接著 application 底下加入權限設定…
    <permission
        android:name="your_package_name.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="your_package_name.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>


五、修改 activIty_main.xml,內容改成底下,最簡易 layout,只用 fragment …
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>


 六、MainActivity.java 範例…
public class MainActivity  extends FragmentActivity {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }
    private void setUpMap() {
        mMap.setMyLocationEnabled(true);
    }

} 

註:API LEVEL 8 以上 Android 2.2 以上才能支援 Google Play services。
註:keystore 可以用自己產生的(release用),也可以用 debug 的。(在 C:\Documents and Settings\USER\.android\debug.keystore)

2011年7月3日 星期日

學習 Android/Linux Kernel 編譯

使用 Ubuntu 11.04 (安裝在 VirtualBox 4.08) 在 emulator 上跑 Android,大致記錄過程,待有硬體再來學習實機移植…

一、安裝 git 原始碼管理套件…
sudo apt-get install git-core gnupg

二、安裝 java jre / jdk…
sudo add-apt-repository ppa:ferramroberto/java
sudo apt-get update
sudo apt-get install sun-java6-jre sun-java6-plugin
sudo apt-get install sun-java6-jdk

三、安裝其它需要的套件…
sudo apt-get install flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl valgrind

四、準備下載 Android 原始碼 (不包含Linux核心)
cd ~
wget http://Android.git.kernel.org/repo
chmod 777 repo
sudo cp repo /bin/
mkdir Android
cd Android
repo init -u git://android.git.kernel.org/platform/manifest.git
repo sync        /// 這邊會下載幾小時

五、編譯/執行…
make        /// 這邊會編譯幾小時
export PATH=$PATH:~/Android/out/host/linux-x86/bin
export ANDROID_PRODUCT_OUT=~/Android/out/target/product/generic
emulator

至此是 Android 的編譯,若要進行 Linux Kernel 編譯…
六、下載 Linux Kernel for Android…
mkdir kernel
cd kernel
git clone git://android.git.kernel.org/kernel/common.git        /// 這邊會下載幾小時

七、透過 emulator 截取 config 檔…
cd common
emulator &
adb pull /proc/config.gz
gunzip config.gz
mv config .config

八、修改 Makefile 檔…
nano Makefile
===========================
ARCH=arm
CROSS_COMPILE ?= arm-eabi-
===========================

九、編譯…
make menuconfig
make        /// 這邊會編譯幾小時

十、使用剛編譯出的核心執行 emulator…
cd ~/Android
emulator -kernel ./kernel/common/arch/arm/boot/zImage &

十一、進 Android 後台確認核心版本…
adb shell
root@android:/ # cd /proc
root@android:/proc # cat version
Linux version 2.6.29-gb0d93fb-dirty (fellong@fellong-VirtualBox) (gcc version 4.4.3 (GCC) ) #5 Sun Jul 3 14:31:58 CST 2011

2011年6月28日 星期二

Android Starter

一、下載 JAVA SE JDK,安裝


二、下載 Android SDK,安裝






三、下載 Eclipse Classic,安裝


四、執行 Eclipse,按下列步驟安裝 ADT  Plug-in (摘自 http://developer.android.com/sdk/eclipse-adt.html#installing)

  1. Start Eclipse, then select Help > Install New Software....
  2. Click Add, in the top-right corner.
  3. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location:
    https://dl-ssl.google.com/android/eclipse/
  4. Click OK Note: If you have trouble acquiring the plugin, try using "http" in the Location URL, instead of "https" (https is preferred for security reasons).
  5. In the Available Software dialog, select the checkbox next to Developer Tools and click Next.
  6. In the next window, you'll see a list of the tools to be downloaded. Click Next.
  7. Read and accept the license agreements, then click Finish. Note: If you get a security warning saying that the authenticity or validity of the software can't be established, click OK.
  8. When the installation completes, restart Eclipse. 這邊會開始下載安裝比較久的時間。

五、 執行 Eclipse,到選單 > Windows > Android SDK and ADV Manager 開啟 ADV Manager (或透過直接執行 ADV Manager 亦可)。
此時若出現找不到 ADV Manager 位置的話,則要到選單 > Windows > Preferences > Andriod 頁面進行設定。




六、第一次執行 ADV Manager 要先安裝相關套件,這邊也是會開始下載安裝一段時間




七、新增加一個 ADV (Android模擬器),名稱自己取



八、OK 後就可以在 ECLIPSE 開 Android 專案了,Build Target 記得選剛剛建立好的 ADV 版本。





九、寫個範例程式…(摘自 http://developer.android.com/resources/tutorials/hello-world.html)

package example.HelloAndroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroidActivity extends Activity {
       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           TextView tv = new TextView(this);
           tv.setText("Hello, Android");
           setContentView(tv);
       }
}

十、執行結果…

2010年5月31日 星期一

Android 開發 Google Map API - 如何註冊 API Key

Google Map View 官方教學網站…

http://developer.android.com/resources/tutorials/views/hello-mapview.html

這有詳盡的 Android Google API 開發教學~

萬事起頭難,其中提到,必須有一組 API Key 才能註冊成為合法認證的 Google API,才能在 Android 上執行,才可以開始玩。(其實就是 java 憑證)

建立過程如下…

一、在開發的過程中,可以先建立 debug certificate…

http://code.google.com/intl/zh-TW/android/add-ons/google-apis/mapkey.html#getdebugfingerprint


二、按照上面的說明,先找到應用程式存放 debug keystore 的地方,通常放在…

C:\Documents and Settings\<user>\.android\debug.keystore

或可透過裝了 ADT 的 Eclipse 選單 Windows > Prefs > Android > Build 中找到完整路徑。

三、再下達…

keytool -list -alias androiddebugkey -keystore <path_to_debug_keystore>.keystore -storepass android -keypass android

會發生二個問題…
  • keytool.exe 的位置:在 JAVA JRE 安裝目錄中,例如…

    C:\Program Files\Java\jre6\bin

  • 路徑有可能包含空白字元,導致無法在 DOS COMMAND 視窗下達指令,你可以先把 debug.keystore 拷貝到 C:\ 再下達指令…

    keytool -list -alias androiddebugkey -keystore c:\debug.keystore -storepass android -keypass android

四、如此便會算出 MD5 碼…

androiddebugkey, 2010/5/15, keyEntry,
認證指紋 (MD5): 70:02:A3:1F:85:35:D1:A4:3C:1C:D2:12:34:56:78:90

五、有了 MD5 碼就可以到 Android Maps API Key Signup 網頁註冊新的 API Key…

http://code.google.com/intl/zh-TW/android/maps-api-signup.html

六、完成註冊,得到 API Key…

感謝您申請 Android Maps API 金鑰!
您的金鑰為:
0GNWKtyf-6M2YDIU3XIjq-Qgqh0yn1234567890
此金鑰適合所有使用以下指紋憑證所簽署的應用程式:
70:02:A3:1F:85:35:D1:A4:3C:1C:D2:12:34:56:78:90

有了金鑰就可以拿來填到 main.xml 中了。


p.s. 下達 keytool 指令時可以善用 DOS 輸出導向功能 keytool ooo xxx yyy ... zzz > md5.txt  , 把輸出導向到一個檔案方便拷貝,以免打錯 MD5 碼。

2010年5月23日 星期日

Android EventListener 建立方法

元件常常有很多「event」必預要去「listen」,這時候需要靠 EventListener。

網路上看到的範例大多長的像底下這個樣子…

btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/// TODO
}
});


乍看可能還很難以理解這一小段程式是在作什麼,其實整個意義如同底下這段程式…

btn.setOnClickListener( new clsOnClickListener() );
///---inline class ---///
public class clsOnClickListener implements OnClickListener {
public void onClick(View v) {
/// TODO
}
}



也就是它其實是建立了個新的 class implements 原來的 OnClickListener 來自訂 onClick 事件處理函式。

但難的是在 coding 的時候往往不曉得(或忘了)有哪些事件,有哪些 listener,更別說要寫出上面第一段那樣的程式了。

還好 Eclipse 開發環境很好用,你可以利用它自動產生程式碼的功能,幫你做到這些事。
用說的不好解釋,看段影片說明…

http://www.youtube.com/v/plW4zMxpnBA



這樣就可以減輕腦袋的負荷了~^ ^

Android 動態產生元件

Android 應用程式的開發一般都將 UI 跟 RESOURCES 的部分抽離出來,好方便管理及設計,也因此有一些 XML 檔需要撰寫,主要都是用來描述這些資源。

但若要自己土法練鋼寫動態產生時要怎麼作?


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
///setContentView(R.layout.main); ///不使用 main.xml 資源

LinearLayout layout = new LinearLayout(this);
this.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);

Button btn = new Button(this);
btn.SetText("Button");
layout.addView(btn, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

EditText txt = new EditText(this);
layout.addView(txt, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}


相對照若用 xml 來描述的話像這樣…

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</Button>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>

Android 練習-寶石方塊

直接將自己舊的 java 手機程式改成 Android 版本。

http://www.youtube.com/v/bYORe68RixQ

Android TabLayout 使用心得

Android 2.2 SDK 己經出了…
http://developer.android.com/sdk/index.html
最近也在學習,還好之前有 java/javaME 的基礎,還算順利。

Google 官方有提供教學網頁…
http://developer.android.com/resources/index.html 
很不錯。

不過其中一個範例 TabLayout 的使用…
 http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
這個範例對初學者來講比較複雜。

底下是比較簡單易理解的版本…

HelloTabWidgetNoIcon.java

package com.study.HelloTabWidgetNoIcon;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;

public class HelloTabWidgetNoIcon extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists").setContent(R.id.tab0);
tabHost.addTab(spec);

// Do the same for the other tabs
spec = tabHost.newTabSpec("albums").setIndicator("Albums").setContent(R.id.tab1);
tabHost.addTab(spec);

spec = tabHost.newTabSpec("songs").setIndicator("Songs").setContent(R.id.tab2);
tabHost.addTab(spec);

tabHost.setCurrentTab(0);
}
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >

<TextView
android:text="tab0"
android:id="@+id/tab0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="tab1"
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="tab2"
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</FrameLayout>
</LinearLayout>
</TabHost>


對照一下 HelloTabWidgetNoIcon.java 及 main.xml 就可以了解程式碼跟 layout 間的對應關係了(tab0,tab1,tab2)。

跟官方的範例不同的地方在於,官方的還多加了…
一、Tab 的小圖示( ic_tab_artists.xml, ic_tab_albums.xml, ic_tab_songs.xml 及圖片 )
二、動態的 Tab View ( ArtistsActivity.java, AlbumsActivity.java, SongsActivity.java )
所以才比較複雜並且要多撰寫一些 xml 碼。

不過,若真的要用在手機上,當然還是用圖示比較吸引人嘍。

VirtualBox 空間減肥

sdelete64 -z c: VBoxManage  modifymedium  disk  "/Users/fellow/VirtualBox VMs/Win10/Win10.vdi"  --compact *.vdi 路徑可以在 VirtualBox 儲...