ผู้ติดตาม

วันจันทร์ที่ 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); }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น