ผู้ติดตาม

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

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

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

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