Tuesday, August 13, 2013

Android Bluetooth Communication

การเขียนแอพพลิเคชั่นเพื่อใช้งาน Bluetooth บนระบบปฏิบัติการที่เป็น Android นั้นสามารถดูรายละเอียดได้ที่ http://developer.android.com ตัวอย่างแอพพลิเคชั่นที่ Google มีให้ดาวน์โหลด และหลายๆเว็บไซต์นิยมนำมาใช้อ้างอิงหรือยกตัวอย่างคือ Bluetooth Chat โดยดูวิธีการดาวน์โหลด Source Code ได้ที่ http://developer.android.com/tools/samples/index.html
การทำงานของ Bluetooth Chat จะเป็นการสื่อสารระหว่างอุปกรณ์ที่เป็น Android เหมือนกันโดยผ่านทาง Bluetooth และสามารถเข้าไปทดลองเล่นตัวแอพพลิเคชั่น Bluetooth Chat นี้ได้ที่ Google Play
Android <—— Bluetooth —–> Android
นอกจากนั้นเรายังสามารถนำ Source Code มาดัดแปลงใช้กับอุปกรณ์อื่นๆที่ไม่ใช่ Android ก็ได้ ยกตัวอย่างเช่น นำมาใช้กับ Microcontroller เพื่อความคุมการทำงานของอุปกรณ์ต่างๆได้
Android <—– Bluetooth —–> Bluetooth Module* <—–> Microcontroller
อันนี้เป็นตัวอย่างที่ผมทดสอบแบบง่ายๆโดยการทำปุ่ม ON, OFF เพื่อความคุมการเปิดปิดของหลอด LED
* ตัวอย่าง Bluetooth Module ที่ราคาไม่แพงและใช้งานง่ายเช่น JY_MCU Bluetooth Module จะมีหน้าตาเป็นแบบนี้
ค่า Default ของตัวโมดูลนี้คือ Default serial port setting : 9600, N, 8, 1; Pairing code : 1234
ส่วนค่าอีกตัวหนึ่งของโมดูลที่จะใช้ที่ต้องรู้คือ Unique identifier ซึ่งจะอยู่ในรูปของตัวเลขฐาน 16 จำนวน 6 ชุด เช่น CC:8C:E3:DC:63:20 ถ้ามีพีซีหรือโน๊ตบุคที่เป็น Windows 7 และที่ตัวเครื่องมี Bluetooth อยู่แล้วสามารถดูได้ดังนี้
1. ไปที่ Devices and Printer เลือก Add a device
2. เมื่อ Windows เริ่มค้นหาอุปกรณ์ก็ต่อไฟเลี้ยงเข้าที่ตัวโมดูล แล้วรอซักครู่ Windows จะแจ้งว่าเจออุปกรณ์ BT UART Bluetooth Other
3. คลิกขวาที่ตัว BT UART ที่ Windows เจอแล้วเลือก Properties
4. กดที่ Tab Bluetooth ดูที่ด้านล่างจะมีบรรทัดที่เขียนว่า Unique identifier
เมื่อเราได้ Unique identifier ของ Bluetooth Module ที่จะใช้มาแล้วต่อไปจะเป็นขั้นตอนการดัดแปลง Source Code ของแอพพลิเคชั่น Bluetooth Chat เพื่อให้อุปกรณ์ Android สามารถสื่อสารกับ Bluetooth Module นี้ได้มีขั้นตอนดังนี้
1. ไปที่ class BluetoothChatService หาบรรทัดที่พิมพ์ว่า
// Unique UUID for this application
private static final UUID MY_UUID = UUID.fromString(“fa87c0d0-afac-11de-8a39-0800200c9a66″);
แก้บรรทัดนี้เป็น
// Unique UUID for this application
//private static final UUID MY_UUID = UUID.fromString(“fa87c0d0-afac-11de-8a39-0800200c9a66″);
private static final UUID MY_UUID= UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);
2. ถ้าต้องการให้ทุกครั้งที่รันแอพพลิเคชั่นให้เชื่อมต่อกับ Bluetooth Module อัตโนมัติก็สามารถทำได้โดยเข้าไปที่ class BluetoothChat extends Activity แล้วไปที่ event onStart()
หาบรรทัดที่พิมพ์ว่า setupChat();
แล้วเพิ่มคำสั่งเข้าไปเป็น
setupChat();
String address = “CC:8C:E3:DC:63:20″;
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mChatService.connect(device);
เมื่อเราทำการ Paired ให้มือถือรู้จักกับ Bluetooth Module แล้วเพียงเท่านี้แอพพลิเคชั่น Bluetooth Chat ที่ทำการดัดแปลงแล้วนี้ก็จะสามารถเชื่อมต่ออัตโนมัติและสื่อสารกับ Bluetooth Module ที่มี Unique identifier = CC:8C:E3:DC:63:20 ได้

HTML5 Geolocation

HTML5 เป็นมาตรฐานใหม่ในการพัฒนาเว็บไซต์ โดยมีการเพิ่มคุณสมบัติใหม่ๆเข้าไปทำให้สามารถพัฒนาเว็บไซต์ให้มีลูกเล่นและมีประสิทธิภาพมากขึ้น จากที่ระบุไว้ใน http://www.w3schools.com เกี่ยวกับคุณสมบัติที่มีใน HTML5 คือ

HTML5

  • New Elements
  • New Attributes
  • Full CSS3 Support
  • Video and Audio
  • 2D/3D Graphics
  • Local Storage
  • Local SQL Database
  • Web Applications
Browser ที่รองรับ Internet Explorer 9+, Firefox, Chrome, Safari and Opera
ตอนนี้จะเป็นตัวอย่างของการนำคุณสมบัติเกี่ยวกับ Geolocation หรือการหาพิกัด GPS. ของผู้ใช้งาน เช่นตัวอย่างจาก http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_error เป็นตัวอย่างที่ค่อนข้างจะสมบูรณ์และสามารถนำไปประยุกต์ใช้ในด้านต่างๆได้เป็นอย่างดี
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude; 
  }
function showError(error)
  {
  switch(error.code)
    {
    case error.PERMISSION_DENIED:
      x.innerHTML="User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML="Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML="The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML="An unknown error occurred."
      break;
    }
  }
</script>
</body>
</html>

และถ้าต้องการแสดงพิกัดของผู้ใช้งานเป็นแผนที่โดยใช้ googlemap ก็สามารถทำได้โดยการแก้ไขฟังค์ชั่น showPosition เป็นดังนี้

function showPosition(position)
{
  var latlon=position.coords.latitude+","+position.coords.longitude;
 
  +latlon+"&zoom=14&size=400x300&sensor=false";
  document.getElementById("mapholder").innerHTML="<img src='"+img_url+"'>";
}

ทดสอบการทำงานของตัวอย่างได้ที่ http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation_map