๋งŽ์€ ๋ถ„๋“ค์ด ์ž๋ฐ” ์ „์ฒด ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์›ํ•˜์…”์„œ ์ด๋ ‡๊ฒŒ๋ผ๋„ ์˜ฌ๋ ค๋ด…๋‹ˆ๋‹ค.

 

์ œ๊ฐ€ ํ”„๋กœ์ ํŠธ๋ฅผ ๋‹ค ์‚ญ์ œํ•ด์„œ ์ž ๊น ์ฝ”๋“œ๋กœ ์ ์–ด๋†“๊ณ  ํ…Œ์ŠคํŠธ๋Š” ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. 

 

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import app.akexorcist.bluetotohspp.library.BluetoothSPP;
import app.akexorcist.bluetotohspp.library.BluetoothState;
import app.akexorcist.bluetotohspp.library.DeviceList;

public class MainActivity extends AppCompatActivity {

    private BluetoothSPP bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt = new BluetoothSPP(this); //Initializing

        if (!bt.isBluetoothAvailable()) { //๋ธ”๋ฃจํˆฌ์Šค ์‚ฌ์šฉ ๋ถˆ๊ฐ€
            Toast.makeText(getApplicationContext()
                    , "Bluetooth is not available"
                    , Toast.LENGTH_SHORT).show();
            finish();
        }
        
        // ------------------------------ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ ๋ถ€ ----------------------------- //
        bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() { //๋ฐ์ดํ„ฐ ์ˆ˜์‹ 
            TextView temp = findViewById(R.id.temp);
            TextView humd = findViewById(R.id.humd);

            public void onDataReceived(byte[] data, String message) {

                String[] array = message.split(",");
                temp.setText(array[0].concat("C"));
                humd.setText(array[1].concat("%") );
            }
        });
        // ------------------------------ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ ๋ถ€ ----------------------------- //

        bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() { //์—ฐ๊ฒฐ๋์„ ๋•Œ
            public void onDeviceConnected(String name, String address) {
                Toast.makeText(getApplicationContext()
                        , "Connected to " + name + "\n" + address
                        , Toast.LENGTH_SHORT).show();
            }

            public void onDeviceDisconnected() { //์—ฐ๊ฒฐํ•ด์ œ
                Toast.makeText(getApplicationContext()
                        , "Connection lost", Toast.LENGTH_SHORT).show();
            }

            public void onDeviceConnectionFailed() { //์—ฐ๊ฒฐ์‹คํŒจ
                Toast.makeText(getApplicationContext()
                        , "Unable to connect", Toast.LENGTH_SHORT).show();
            }
        });

        Button btnConnect = findViewById(R.id.btnConnect); //์—ฐ๊ฒฐ์‹œ๋„
        btnConnect.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
                    bt.disconnect();
                } else {
                    Intent intent = new Intent(getApplicationContext(), DeviceList.class);
                    startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
                }
            }
        });
    }

    public void onDestroy() {
        super.onDestroy();
        bt.stopService(); //๋ธ”๋ฃจํˆฌ์Šค ์ค‘์ง€
    }

    public void onStart() {
        super.onStart();
        if (!bt.isBluetoothEnabled()) { //
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
        } else {
            if (!bt.isServiceAvailable()) {
                bt.setupService();
                bt.startService(BluetoothState.DEVICE_OTHER); //DEVICE_ANDROID๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ๊ธฐ๊ธฐ ๋ผ๋ฆฌ
                setup();
            }
        }
    }

    public void setup() {
        Button btnSend = findViewById(R.id.btnSend); //๋ฐ์ดํ„ฐ ์ „์†ก
        btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                bt.send("Text", true);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
            if (resultCode == Activity.RESULT_OK)
                bt.connect(data);
        } else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
            if (resultCode == Activity.RESULT_OK) {
                bt.setupService();
                bt.startService(BluetoothState.DEVICE_OTHER);
                setup();
            } else {
                Toast.makeText(getApplicationContext()
                        , "Bluetooth was not enabled."
                        , Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }
}


728x90
๋ฐ˜์‘ํ˜•
18์ง„์ˆ˜