Page 95 - 2020학년도 MDP과제발표회 자료집 (통신과) (3)
P. 95

@Override
                    public  void  onBackPressed()  {  //  뒤로가기  버튼을  번  눌렀을  때  종료2
                            if  (System.currentTimeMillis()  -  lastTimeBackPressed  <  1500)  {

                                    mp.stop();
                                    finish();
                                    return;
                            }
                    Toast.makeText(getApplicationContext(),  "     뒤로  가기  버튼을  한  번  더  눌러

                    종료합니다.",  Toast.LENGTH_LONG).show();
                          lastTimeBackPressed  =  System.currentTimeMillis();
                    }





                    //   블루투스  장치의  이름이  주어졌을때  해당  블루투스  장치  객체를  페어링  된  장치  목록에
            서  찾아내는  코드.
                    BluetoothDevice  getDeviceFromBondedList(String  name)  {
                            //  BluetoothDevice  :  페어링  된  기기  목록을  얻어옴.
                            BluetoothDevice  selectedDevice  =  null;

                            //  getBondedDevices   함수가  반환하는  페어링  된  기기  목록은  Set        형식이며,
                            //  Set   형식에서는  n   번째  원소를  얻어오는  방법이  없으므로  주어진  이름과  비교해서
            찾는다.
                            for  (BluetoothDevice  deivce  :  mDevices)  {
                                    //  getName()  :  단말기의  Bluetooth  Adapter  이름을  반환

                                    if  (name.equals(deivce.getName()))  {
                                            selectedDevice  =  deivce;
                                            break;
                                    }
                            }

                            return  selectedDevice;
                    }


                    //  문자열  전송하는  함수 쓰레드  사용 (        x)
                    void  sendData(String  msg)  {

                            msg  +=  mStrDelimiter;    //   문자열  종료표시  (\n)
                            try  {
                                    //  getBytes()  :  String 을  byte 로  변환
                                    //  OutputStream.write  :  데이터를  쓸때는  write(byte[])  메소드를  사용함.
                                    //  byte[]   안에  있는  데이터를  한번에  기록해  준다.

                                    mOutputStream.write(msg.getBytes());    //   문자열  전송.
                            }  catch  (Exception  e)  {    //  문자열  전송  도중  오류가  발생한  경우


                                                         -  98  -
   90   91   92   93   94   95   96   97   98   99   100