ผู้ติดตาม

วันอังคารที่ 22 พฤษภาคม พ.ศ. 2555


 :: สรุปการฝึกงาน ::



การใช้งาน Android : Bluetooth & Accelerometer 


Application Source Code

  • BT control
         ควบคุมรถบังคับ ส่งคำสั่งผ่าน Bluetooth

  • BT IPcam
         ส่งภาพจากกล้องที่ตัวรถ มายังเครื่องที่ควบคุม

  • BT ACsensor
         ควบคุมรถบังคับด้วย Accelerometer ส่งคำสั่งผ่าน Bluetooth

วันอังคารที่ 10 เมษายน พ.ศ. 2555


วัน อังคาร   ที่ 10   เมษายน  2555


การติดตั้งกล้องบนตัวรถ  และส่งภาพมายังเครื่องที่ใช้ควบคุม

หลักการทำงาน
      ใช้กล้อง android ติดที่่ตัวรถ โดยใช้แอฟพลิเคชัน ip cam บน android เพื่อส่งภาพที่ได้จากกล้องไปยังเซิร์ฟเวอร์  แล้วสร้างแอฟพลิเคชันเพื่อให้เครื่องที่จะบังคับ ลิ้งไปยังที่อยู่ของเซิร์ฟเวอร์ เพื่อนำภาพที่ได้จากกล้องที่อยู่บนรถมาแสดง  และเพิ่มส่วนของปุ่มที่ใช้ในการบังคับเข้าไปในตัวแอฟพลิเคชัน

=======================================================================

ตัวอย่างโค้ด

ไฟล์ manifest.xml เพิ่มส่วนของโค้ดที่ใช้ขออนุญาตใช้งาน Bluetooth และ intermet ดังนี้

<manifest>
     . . .
     <uses-permission android:name="android.permission.BLUETOOTH" />
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
     <uses-permission android:name="android.permission.INTERNET" />
     . . .
</manifest>

========================================================================

ไฟล์ main.xml เพิ่มส่วนของโค้ด webview เพื่่อใช้แสดงภาพที่ได้จากกล้อง และเพิ่มปุ๋มตามที่ต้องการ สังเกตุว่าจะใช้ Layout แบบ Relativelayout



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"


        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <WebView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <Button
            android:id="@+id/connect_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"

            android:text="Connect."   />

        <Button . . . >

 </RelativeLayout>

========================================================================

ไฟล์ activity.java เพิ่มส่วนควบคุมการทำงานของ webview ดังนี้

public class Web extends Activity {

       . . .

        // ประกาศตัวแปล webview
WebView webview;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           // กำหนดค่าให้ webview
           webview = (WebView) findViewById(R.id.webview);
           webview.setWebViewClient(new webviewClient());

           // กำหนดให้ลิ้งไปยังเซิร์ฟเวอร์ 
           webview.loadUrl("http://10.55.10.133:8087/js.html");

            // เปิดการใช้งาน javascript
           webview.getSettings().setJavaScriptEnabled(true);

           . . .

       }
}

** เพิ่มคำสั่งที่ต้องการได้ตามปกติ

========================================================================
ศึกษาข้อมูลเพิ่มเติมเกี่ยวกับ Webview ได้ที่ http://developer.android.com/resources/tutorials/views/hello-webview.html


วัน จันทร์  ที่ 26  มีนาคม  2555

         ได้เครื่อง Android  มาแล้ว

          การ Scan หา Bluetooth Device



import . . .

public class DeviceListActivity extends Activity{

    // Return Intent extra
    public static String EXTRA_DEVICE_ADDRESS = "device_address";

    // ตัวแปลที่ต้องใช้
    private BluetoothAdapter mBtAdapter;
    private ArrayAdapter<String> mPairedDevicesArrayAdapter;
    private ArrayAdapter<String> mNewDevicesArrayAdapter;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set ค่าปุ่มที่ใช้ในการสแกน
        Button scanButton = (Button) findViewById(R.id.button_scan);
        scanButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                doDiscovery();
                v.setVisibility(View.GONE);
            }
        });

        // สร้างอาร์เรย์ไว้เก็บชื่่อ และ หมายเลยอุปกรณ์
        mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
        mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);

        // สร้างลิสท์ และ กำหนดให้ไปดึงของมูลจาก อาเรย์มาแสดง
        ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
        pairedListView.setAdapter(mPairedDevicesArrayAdapter);
        pairedListView.setOnItemClickListener(mDeviceClickListener);

        ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
        newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
        newDevicesListView.setOnItemClickListener(mDeviceClickListener);

        // สร้างรีจิสเตอร์ที่ใช้สแกน
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(mReceiver, filter);

        // หยุดการทำงานที่ bluetooth ทำอยู่
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(mReceiver, filter);

       // เรียกไปยัง bluetooth ของเครื่อง
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();

       // ดูรายชื่ออุปกรณ์ที่จับคู่แล้ว
        Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();

        // ค้นหาอุปกรณ์ที่จับคู่แล้วเก็บไว้ในอาร์เรย์
        if (pairedDevices.size() > 0) {
            findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
            for (BluetoothDevice device : pairedDevices) {
                mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        } else {
            String noDevices = getResources().getText(R.string.none_paired).toString();
            mPairedDevicesArrayAdapter.add(noDevices);
        }
    }



    // เลือกอุปกรณ์ที่จะเชื่อมต่อจากลิสท์
    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
            // Cancel discovery because it's costly and we're about to connect
            mBtAdapter.cancelDiscovery();
         
            // รับค่าเลข Mac address ของอุปกรณ์
            String info = ((TextView) v).getText().toString();
            try {
            // Attempt to extract a MAC address
            String address = info.substring(info.length() - 17);
           
            // สร้าง intent ส่งค่า Mac address ไปยังหน้าหลัก
                Intent intent = new Intent();
                intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
             
                // จบการทำงาน
                setResult(Activity.RESULT_OK, intent);
                finish();
            }catch (IndexOutOfBoundsException e) {
            // Extraction failed, set result and finish this Activity
                setResult(Activity.RESULT_CANCELED);
                finish();
            }
        }
    };

   // สแกนหาอุปกรณ์
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // จัดเก็บชื่อ และ address ของอุปกรณ์ที่พบ
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // If it's already paired, skip it, because it's been listed already
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            // เสร็จการค้นหา
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                setProgressBarIndeterminateVisibility(false);
                setTitle(R.string.select_device);
                if (mNewDevicesArrayAdapter.getCount() == 0) {
                    String noDevices = getResources().getText(R.string.none_found).toString();
                    mNewDevicesArrayAdapter.add(noDevices);
                }
            }
        }
    };
}

วันอังคารที่ 3 เมษายน พ.ศ. 2555


วัน ศุกร์  ที่ 23  มีนาคม  2555 


         ศึกษาเรื่องการใช้งาน Bluetooth ต่อ  developer.android.bluetooth

         เริ่มลงมือชำแหละ ชิ้นส่วนรถบังคับ


วัน พฤหัส  ที่ 22  มีนาคม  2555

      นักศึกษาจาก ราชภัฏสุราษฏร์ มาดูงาน  วันนี้ผมได้ให้ความรู้เกี่ยวกับ อุปกรณ์ช่วยคนตาบอดด้วย


       หลักการทำงานของเครื่องนี้ก็ คือ ใช้ Ultrasonic Sensor หรือ Infarate Sensor ซึ่งใน sensor จะประกอบไปด้วยตัวส่ง และตัวรับคลื่นวิทยุ  เมื่อส่งคลื่นวิทยุออกไปแล้วคลื่นไปกระทบกับวัตถุก็จะสะท้อนกลับมา sensor จะใช้ระยะเวลาในการเดินทางไป-กลับ ของคลื่นมาคำนวณเป็นระยะทาง  ถ้าอยู่ในขอบเขตที่กำหนด อุปกรณ์ก็จะส่งสัญญาณเตือนมายังผู้ใช้  เป็นหลักการง่ายๆแต่สามารถนำมาใช้ประโยชน์ได้จริงๆ



วันจันทร์ที่ 2 เมษายน พ.ศ. 2555

วัน พุธ  ที่ 21  มีนาคม  2555

         ศึกษาเกี่ยวการ ใช้งาน Bluetooth บน Android จาก http://developer.android.com/guide/topics/wireless/bluetooth.html
     
         ขั้นตอนการใช้งาน Bluetooth
1.  ขออนุญาติใช้งาน Bluetooth จากระบบ โดยเพิ่มคำสั่งในไฟล์ Manifest.xml ดังนี้

     <manifest>
          . . .

          <uses-permission android:name="android.permission.BLUETOOTH" />
         <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
          . . .
      </manifest>


2.  สร้าง Bluetooth Adapter ขึ้นมา ในไฟล์ Activity.java

 // คำสั่ง BluetoothAdapter.getDefaultAdapter(); เพื่อเรียกไปยัง Bluetooth ของเครื่อง
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
}


3. เปิดใช้งาน Bluetooth
// ตรวจสอบว่า Bluetooth เปิดอยู๋หรือไม่
if (!mBluetoothAdapter.isEnabled()) {
    // ขออนุญาตเปิด Bluetooth
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }

วัน อังคาร  ที่ 20  มีนาคม  2555


      ทำ Application ที่ 2 เป็น ชื่อ WebBrowser เพื่อทดสอบความเข้าใจเกี่ยวกับ Android

-----------------------------------------------------------------------------------------------------------
ไฟล์ Activity.java
package com.fibo.webbrowser;         // ที่อยู๋ package

import android.app.Activity;             // import ไฟล์ต่างๆ
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class WebBrowserActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void Go (View v){
   
        //  สร้างตัวแปลแบบ Edittext เพื่อรับค่าจากหน้า Main.xml
    EditText Web = (EditText) findViewById(R.id.editText1) ;      
   
        // สร้าง intent เพื่อใช้ส่งค่าตัวแปล ผ่าน Uri.parse
        // คำสั่ง Web.getText().toString()) รับค่าข้อความจาก edittext
    Intent AA = new Intent (android.content.Intent.ACTION_VIEW,Uri.parse("http://"+Web.getText().toString()));
        
       // แสดงข้อความ
    Toast.makeText(this,"Go to:"+Web.getText().toString(),Toast.LENGTH_SHORT).show();
    startActivity(AA);
    }

      // ฟังก์ชันทีเรียกใช้ เมื่อกดปุ่ม shortcut ไปยังหน้าเว็บต่างๆ
    public void GoFB (View v){
    Intent AA = new Intent (android.content.Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com"));
        Toast.makeText(this,"Go to:www.facebook.com",Toast.LENGTH_SHORT).show();
    startActivity(AA);
    }
    public void GoGoogle (View v){
    Intent AA = new Intent (android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
        Toast.makeText(this,"Go to:www.google.com",Toast.LENGTH_SHORT).show();
    startActivity(AA);
    }
    public void GoYoutube (View v){
    Intent AA = new Intent (android.content.Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com"));
        Toast.makeText(this,"Go to:www.youtube.com",Toast.LENGTH_SHORT).show();
    startActivity(AA);
    }
    public void GoTwitter (View v){
    Intent AA = new Intent (android.content.Intent.ACTION_VIEW,Uri.parse("http://www.twitter.com"));
        Toast.makeText(this,"Go to:www.twitter.com",Toast.LENGTH_SHORT).show();
    startActivity(AA);
    }
}


วันพฤหัสบดีที่ 29 มีนาคม พ.ศ. 2555


วัน จันทร์  ที่ 19  มีนาคม  2555   ( ฝึกงานสัปดาห์ที่ 2  )

         วันนี้ ผมก็นั่ง ศึกษา Android ต่อ มันเข้าใจยากสำหรับผม  เพราะต้องเขียน java กับ xml อีกเล็กน้อย  ไม่อยากจะบอกเลยว่า  วิชา java  ผมยังไม่ผ่านเลย  จึงต้องขอใช้เวลาซักระยะทำความเข้าใจ


   

วันพุธที่ 28 มีนาคม พ.ศ. 2555

วัน อาทิตย์  ที่ 18  มีนาคม  2555

      วันนี้เป็นวันหยุดพักผ่อนวันแรก  ในการฝึกงานของผม  อากาศเลยนอนพักผ่อนอยู่ที่ห้อง  พอช่วงเย็นแดดร่ม  ลมตก ก็ได้เวลา ออกผจญภัยในกรุงเทพ ซะหน่อย  ออกไปดูบอลคู่ บางกอกกล๊าส - วัวชน ยูไนเต็ด  มันดีคับ !  ผลการแข่งขัน บางกอกกล๊าส ชนะ 2-0 ที่แถว รังสิต  คลอง 3




 

วัน เสาร์  ที่ 17  มีนาคม  2555
   
      เข้าดูการแข่งขัน หุ่นยนต์ Humanoid (หุ่นยนต์เตะฟุตบอล) พี่ๆ กับเพื่อนที่ภาคผมก็มาแข่งด้วย  โอกาสหน้า  ถ้าว่าง  ผมก็อยากมีส่วนร่วมทำหุ่นกับเพื่อนๆ มั่ง




วัน ศุกร์  ที่ 16  มีนาคม  2555

        ศึกษา ทำความเข้าใจ Android  ต่อ  http://developer.android.com


       ช่วงบ่าย นักศึกษาจาก คณะ วิศวกรรมศาสตร์ ม.รามคำแหง มาดูงานที่  FIBO พวกผมก็เลยพลอยได้ความรู้มาด้วย หลังจากนั้น ก็ช่วยพี่จัดสถานที่  เตรียมการแข่งขันหุ่นยนต์ Humanoid



วัน พถหัส  ที่ 15  มีนาคม  2555

     ทำความเข้าใจเกี่ยวกับ Android

     ช่วงบ่ายเข้าประชุมประจำสัปดาห์ กับ พี่ๆ ที่ FIBO ( บรรยากกาศเป็นกันเองมาก )

     ตอนเย็นช่วย พี่ เล้ง ทำโต้ะ Touch Screen กลับตีหนึ่งกว่าแล้ว




 
วัน พุธ  ที่ 14  มีนาคม  2555  ( ฝึกงานวันที่สาม )

      เริ่มทำ Application แรก HelloFIBO
    ---------------------------------------------------------------------------------------------------------
     โค้ดไฟล์ Activity.java

     package com.Fibo;

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

     public class HelloFiboActivity extends Activity { 

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

    # ----------------------------------------------------------------------------------------------------- #
    โค้ดไฟล์ main.xml

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

       <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
           android:text="Hello FIBO" />

</LinearLayout>

    # ---------------------------------------------------------------------------------------------------- #

วัน อังคาร   ที่ 13  มีนาคม  2555  ( ฝึกงานวันที่สอง )
   
       เข้าพบอาจารย์ และ รับมอบหมายงานจากอาจารย์ ส่วนงานของผมคือ ทำ Application บน Android เพื่อ ติดต่อสื่อสาร กับ Microcontroller โดยผ่าน Bluetooth

       เริ่มติดตั้ง Eclipe , Android SDK & ADT และ ทำความเข้าใจ เกี่ยวกับ Android

     

        http://developer.android.com , www.androidjump.com
   
วัน จันทร์  ที่ 12  มีนาคม 2555 ( ฝึกงานวันแรก )



       ช่วงเข้า รายงานตัว เข้าพบ และ พูดคุย กับ อาจารย์ และ พี่ๆ ที่ FIBO
     
       ช่วงบ่าย อาจารย์ พาไปเที่ยว ซื้ออุปกรณ์อิเล็กทรอนิกส์ ที่บ้านหม้อ

       ( ปล. เพิ่งเคยไป เดิน กันปวดขาเลย )



สวัสดีคับ !
บล็อกนี้สร้างขึ้นเพื่อเขียนบันทึกการฝึกงาน
ของ : นาย จักรกริช  ศรียกเฮ้ง
สถานที่ฝึก : FIBO@KMUTT
ระหว่างวันที่ : 12 มี.ค - 18 พ.ค. 2555