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


更高效處理 micro second 的方式

更高效處理 micro second 的方式…  以 STM32 為例… __IO unsigned long sys_tick = 0; void SysTick_Handler(void) {     HAL_IncTick();     sys_tick += (SysTi...