Page 42 - MDP2020-2
P. 42

|    인천전자마이스터고등학교  ·············································································································
            36


                                    public  void  onDeviceConnected(String  name,  String  address)  {
                                            Toast.makeText(getApplicationContext()
                                                            ,  "연결  성공  "  +  name  +  "\n"  +  address
                                                            ,  Toast.LENGTH_SHORT).show();
                                    }


                                    public  void  onDeviceDisconnected()  {  //연결해제
                                            Toast.makeText(getApplicationContext()
                                                            ,  "연결  해재",  Toast.LENGTH_SHORT).show();
                                    }


                                    public  void  onDeviceConnectionFailed()  {  //연결실패
                                            Toast.makeText(getApplicationContext()
                                                            ,  "연결  실패",  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는  안드로이드
            기기  끼리
   37   38   39   40   41   42   43   44   45   46   47