반응형

안녕하세요. 아두이노 MQ Series PPM으로 변환하기 번외 편입니다. 

 

저번 글은 회로도를 보았고, 오늘은 파이썬으로 PPM으로 변환하도록 하겠습니다.

 

파이썬으로 굳이 사용한 이유는 급하게 실험하느라, 센서 값을 PPM으로 변환하지 않고 그대로 CSV파일 형식으로 받았기 때문에 파이썬으로 데이터를 전처리해야만 했습니다.

 

import csv
import numpy as np
import math
import matplotlib.pyplot as plt

"""
PPM 
VCC = 3.3V
RL = 10k

VL = VCC*(RL/(RS+RL)
RS = (RL*VCC)/VL-RL

PPM = a * ratio * b
"""

vcc = 3.3
rl = 10
mq3, mq4, mq6, mq7, mq8, mg811 = 7, 8, 9, 10, 11, 12


#  Calculate Rs 
def rs_cal(vl):
    return ((vcc * rl) / vl) - rl


# Calculate Ro
def ro_cal(rs, num, clean_air_ppm):
    ro_tmp = 0
    for i in range(num):
        ro_tmp += rs[i] / clean_air_ppm
    return ro_tmp/num


# Calculate ratio 
def ratio_cal(rs_, ro_):
    return rs_/ro_


# Calculate PPM 
def ppm_cal(ratio, a, b):
    return a * pow(ratio, b)

# Calculate logscale PPM
def ppm_log_cal(ratio, a, b):
    return (math.log10(ratio)-b)/a


def co2_cal(adc_v):
    return pow(10, ((adc_v / 8.5) - 0.220) / (0.020 / (2.602 - 3)) + 2.602)


constant = {"mq3": ['Alcohol', "mq3", 0.3934, -1.504],
            "mq4": ['CH4', "mq4", 1012.7, -2.786],
            "mq6": ['LPG', "mq6", 1009.2, -2.35],
            "mq7": ['CO', "mq7", 99.042, -1.518],
            "mq8": ['H2', "mq8", 976.97, -0.688]}


file_name_1 = '2020923_105132.csv'
file_name_2 = '2020923_113418.csv'
file_name_3 = '2020923_125810.csv'

header = ['boot_ms[ms]',
          'latitude[degE7]', 'longitude[degE7]', 'relative altitude[mm]',
          'local position x[m]', 'local position y[m]', 'local position z[m]',
          'mq3', 'mq4', 'mq6', 'mq7', 'mq8', 'mq811',
          'PM1.0', 'PM2.5', 'PM10.0', 'ozone', 'gamma']


csv_1 = np.loadtxt(file_name_1, delimiter=",")
# file = np.vstack([header, csv_1])

adc_v_list = []
rs_list = []
ratio_list = []
ppm_list = []

tmp = []

for i in range(len(csv_1)):
    adc_v_list.append(csv_1[i][mq4])

for i in range(len(csv_1)):
    rs_list.append(rs_cal(csv_1[i][mq4]))

ro = ro_cal(rs=rs_list, num=10, clean_air_ppm=4.4)

for i in range(len(csv_1)):
    ratio_list.append(ratio_cal(rs_list[i], ro))

for i in range(len(csv_1)):
    ppm_list.append(ppm_cal(ratio_list[i], constant['mq4'][2], constant['mq4'][3]))

time = []

for i in range(len(csv_1)):
    time.append((csv_1[i][0]-csv_1[0][0])/1000/60)

print("time :", time)
print("adc_v :", adc_v_list)
print("ro :", ro)
print("rs :", rs_list)
print("ratio : ", ratio_list)
print("ppm :", ppm_list)
print("clean air :", 1012.7 * pow(4.4, -2.786))

plt.plot(time, [300 for _ in range(len(csv_1))], color='red')
# plt.plot(time, [10000 for _ in range(len(csv_1))], color='red')
plt.plot(time, ppm_list, color='green')
plt.xlabel('[Minute]')
plt.ylabel('[PPM]')
plt.title("MQ4 - CH4( 300~10000ppm )")
plt.show()

 

위 소스코드 같은 경우는 프로젝트에서 중요하다고 생각한 부분만 가져왔습니다. 

 

아래는 PPM으로 변환할 때 필요한 함수입니다.

#  Calculate Rs 
def rs_cal(vl):
    return ((vcc * rl) / vl) - rl


# Calculate Ro
def ro_cal(rs, num, clean_air_ppm):
    ro_tmp = 0
    for i in range(num):
        ro_tmp += rs[i] / clean_air_ppm
    return ro_tmp/num


# Calculate ratio 
def ratio_cal(rs_, ro_):
    return rs_/ro_


# Calculate PPM 
def ppm_cal(ratio, a, b):
    return a * pow(ratio, b)

# Calculate logscale PPM
def ppm_log_cal(ratio, a, b):
    return (math.log10(ratio)-b)/a

 

그리고 기억은 잘나지 않지만, CO2 센서는 위 공식 말고 다른 식을 사용합니다. 

# Calculate CO2 PPM
def co2_cal(adc_v):
    return pow(10, ((adc_v / 8.5) - 0.220) / (0.020 / (2.602 - 3)) + 2.602)

 

1편

coding-yoon.tistory.com/98

 

[아두이노] MQ 시리즈 공기질 센서 PPM으로 변환하기! (1) Feat. MQ2, MQ3, MQ4, MQ5, MQ6, MQ7, MQ8, MQ9, MG-811

안녕하세요. 약 두 달만에 글을 씁니다. 공모전과 기사 시험의 지옥을 뒤로 잠시 여유가 생겨 시간을 냅니다. 오늘은 MQ 시리즈에서 공기질 센서를 PPM으로 변환하는 방법에 대해 글을 쓰겠습니

coding-yoon.tistory.com

 

2편

coding-yoon.tistory.com/121

 

[아두이노] MQ 시리즈 공기질 센서 PPM으로 변환하기! (2) 회로도 (Schematic)

안녕하세요. MQ시리즈 두 번째 글을 작성합니다. 확실히 글은 바로 바로 작성하는 것이 중요한 것 같습니다. 글을 쓰려고 보니 기억이 안 나서 다시 새로 공부했습니다. 저번 글은 아두이노 라이

coding-yoon.tistory.com

 

728x90
반응형
반응형

안녕하세요. 

 

MQ시리즈 두 번째 글을 작성합니다. 

 

확실히 글은 바로 바로 작성하는 것이 중요한 것 같습니다. 글을 쓰려고 보니 기억이 안 나서 다시 새로 공부했습니다.

 

저번 글은 아두이노 라이브러리를 이용하여 단순히 PPM을 구하였다면, 오늘은 조금 더 자세히 회로를 통해 좀 더 MQ 시리즈와 친해지는 것이 목적입니다. 

coding-yoon.tistory.com/98

 

[아두이노] MQ 시리즈 공기질 센서 PPM으로 변환하기! (1) Feat. MQ2, MQ3, MQ4, MQ5, MQ6, MQ7, MQ8, MQ9, MG-811

안녕하세요. 약 두 달만에 글을 씁니다. 공모전과 기사 시험의 지옥을 뒤로 잠시 여유가 생겨 시간을 냅니다. 오늘은 MQ 시리즈에서 공기질 센서를 PPM으로 변환하는 방법에 대해 글을 쓰겠습니

coding-yoon.tistory.com

 

 

MQ Series Schematic 를 구글링 하면 굉장히 많은 회로도가 나옵니다.

 

그중 가장 직관적인 회로도를 가져왔습니다. 

 

어려운 회로도가 아니기 때문에 편하게 보시면 될 것 같습니다. 

 

Heater가 회로에 있습니다. MQ 원리는 Heater를 통해 기체를 태운 반응으로 측정을 하는 방법입니다. 

 

센서가 어느 정도 가열이 돼야 센서 값의 신뢰도가 높아지므로 실내용 공기 센서로 많이 알려져 있습니다.

(이런 센서를 가지고 드론에 달았으니...)

 

이 회로를 쉽게 읽을 수 있게 다르게 그리겠습니다. 

 

이렇게 표현하면 전자회로를 한 번이라도 들은 사람이라면 많이 본 회로입니다. 

 

전압 분배 법칙을 이용해서 Vout을 구할 수 있습니다. 

 

1. Vcc는 아두이노에서 인가한 전압

2. Rs는 센서 저항으로 공기질에 대한 센서 저항

3. RL은 아래와 같이 감도조정이라고 십자 드라이버로 돌리는 곳이 있습니다. ( 최대로 돌리면 10Ω )

 

 

 

 

① Rs 구하기 

전압분배법칙

 

 

정리1
정리2
정리3 

 

② Ratio 구하기 ( 1편을 보시면 쉽게 알 수 있습니다. )

 

*Rs = 센서저항 

*Ro = 칼리브레이션 저항 

 

 

③ PPM 구하기 

// a, b 는 상수

 

Result = a * ratio ^ b[ppm]

 

 

이런 순서로 보시면은 괜찮을 것 같습니다.

728x90
반응형
반응형

삼바(samba)는 Windows 운영체제를 사용하는 PC에서 Linux 또는 UNIX 서버에 접속하여 파일이나 프린터를 공유하여 사용할 수 있도록 해 주는 소프트웨어이다. -위키백과

 

현재 라즈베리파이를 공유폴더로 사용하여 윈도우에서 작업하는데 많이 사용합니다. 

 

vim으로 작업을 해봤고, 라즈베리파이에서 vscode를 깔아서 작업도 해봤고, 기본 파이썬 툴로도 해봤지만 

코드는 길어지고, 여러 파일 작업을 할 때 답답해서 죽는 줄 알았습니다. 

 

그래서 가장 좋은 방법은 SSH나 VNC로 라즈베리파이를 모니터 없이 연결만 해놓고, 작업은 윈도우에서 하는 게 가장 

편한 것 같습니다. 

 

1. 설치

sudo apt-get install samba samba-common-bin

2. 설정

sudo vim /etc/samba/smb.conf

path = 자신이 공유하고자는 폴더

create mask = 권한 ( 상남자가 아니라면 777로 주지 않기 ) 

sudo smbpasswd -a pi

재시작 

# stop 
sudo /etc/init.d/smbd stop
# start
sudo /etc/init.d/smbd start

 

이 부분까지는 구글에 samba만 쳐도 바로 나오는 내용

 

제 윈도우 PC는 공인 IP로 직접 잡혀있고, Raspberry Pi는 Local로 잡혀 있어 포트 포워딩을 해야 하는 상황

 

공유기 관리자 모드로 들어갑니다. 

Raspi4 smb처럼 TCP포트에 139번(TCP)와 445번(TCP) 포트포워딩 합니다. 

 

Port 139: SMB originally ran on top of NetBIOS using port 139. NetBIOS is an older transport layer that allows Windows computers to talk to each other on the same network.


Port 445: Later versions of SMB (after Windows 2000) began to use port 445 on top of a TCP stack. Using TCP allows SMB to work over the internet.

 

www.varonis.com/blog/smb-port/

 

정확히 역할을 알고 싶으시면 위 사이트를 참고하세요. 

 

하지만, 블로그를 찾아 보면 보안에 굉장히 취약하기 때문에 추천하지 않는 곳이 많습니다.

 

그 이유는 CIFS + Samba 에서 CIFS1.0이 랜섬웨어에 굉장히 취약하기 때문입니다. 

 

바바리맨을 잡겠다고, 바바리를 못입게 하면 안되잖아요?

 

그래서 해결방법으로 Samba protocol version을 업그레이드 해서 보안을 강화하는 것입니다. 

 

www.cyberciti.biz/faq/how-to-configure-samba-to-use-smbv2-and-disable-smbv1-on-linux-or-unix/

 

How to configure Samba to use SMBv2 and disable SMBv1 on Linux or Unix - nixCraft

Explains how to configure Samba to use SMBv2/SMBv3 and disable SMBv1 on Linux or Unix-like systems for security reasons such as WannaCrypt/WannaCry.

www.cyberciti.biz

 

 

그런데 굳이 위를 안해주셔도 됩니다. 누가 교육용 라즈베리파이를 해킹할까요?

 

포트포워딩까지 하셨으면, 윈도우에서 네트워크 접근하겠습니다.

 

1
2

 

3
4

 

5

접근 성공

728x90
반응형
반응형

안녕하세요. 오랜만에 글을 작성합니다. 

 

오늘은 예제만 돌려봤다면, 실제로 학습된 모델을 이용하여 얼굴을 찾도록 하겠습니다. 

 

라즈베리파이로 얼굴인식을 찾아보면 Haarcascade 알고리즘이 많이 사용됩니다. 

 

하지만 오래전에 나온 알고리즘이고 좀 더 빠르고 더 정확하게 인공지능으로 얼굴 인식을 할 수 있습니다.

 

그리고 얼굴인식 자체를 Edge TPU가 처리하기 때문에 라즈베리파이의 CPU는 거의 사용되지 않습니다. 

 

그러기 때문에 라즈베리파이에서 부담 없이 강력한 효과를 보여줍니다. 

 

아래 글을 읽어보시고 가시는 것을 추천드립니다. 

 

coding-yoon.tistory.com/87?category=866905

coding-yoon.tistory.com/88?category=866905

coding-yoon.tistory.com/91?category=866905

 

[Coral] TensorFlow Lite : Tflite 모델 생성 & Edge TPU로 Inferece까지 간단하게 개발하기!

안녕하세요. 만약, 처음 이 글을 보신다면 아래 글을 먼저 읽어보시는 것을 추천드립니다. 1편 : Coral Board 대신 라즈베리파이를 사용하는 이유 : https://coding-yoon.tistory.com/85?category=866905 2편 :..

coding-yoon.tistory.com

 

우선, coral에서 Face detection tflite 파일을 먼저 받겠습니다. 

coral.ai/models/

 

Models | Coral

Download pre-compiled demo models that are compatible with the Edge TPU, or use the model source files to retrain the models for your own applications

coral.ai

위 파일 Edge TPU model을 눌러 파일을 받습니다.

ssd_mobilenet_v2_face_quant_postprocess_edgetpu.tflite
6.39MB

네트워크는 경량 된 MobileNet V2와 객체인식 SSD(Single Shot MultiBox Detection)으로 학습된 모델입니다. 

궁금하시면 두 논문을 참고하시면 될 것 같습니다.

 

arxiv.org/abs/1801.04381

 

MobileNetV2: Inverted Residuals and Linear Bottlenecks

In this paper we describe a new mobile architecture, MobileNetV2, that improves the state of the art performance of mobile models on multiple tasks and benchmarks as well as across a spectrum of different model sizes. We also describe efficient ways of app

arxiv.org

arxiv.org/abs/1512.02325

 

SSD: Single Shot MultiBox Detector

We present a method for detecting objects in images using a single deep neural network. Our approach, named SSD, discretizes the output space of bounding boxes into a set of default boxes over different aspect ratios and scales per feature map location. At

arxiv.org

 

detect.py (google git에서 구할 수 있음)

# Lint as: python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions to work with detection models."""

import collections
import numpy as np

Object = collections.namedtuple('Object', ['id', 'score', 'bbox'])


class BBox(collections.namedtuple('BBox', ['xmin', 'ymin', 'xmax', 'ymax'])):
  """Bounding box.
  Represents a rectangle which sides are either vertical or horizontal, parallel
  to the x or y axis.
  """
  __slots__ = ()

  @property
  def width(self):
    """Returns bounding box width."""
    return self.xmax - self.xmin

  @property
  def height(self):
    """Returns bounding box height."""
    return self.ymax - self.ymin

  @property
  def area(self):
    """Returns bound box area."""
    return self.width * self.height

  @property
  def valid(self):
    """Returns whether bounding box is valid or not.
    Valid bounding box has xmin <= xmax and ymin <= ymax which is equivalent to
    width >= 0 and height >= 0.
    """
    return self.width >= 0 and self.height >= 0

  def scale(self, sx, sy):
    """Returns scaled bounding box."""
    return BBox(xmin=sx * self.xmin,
                ymin=sy * self.ymin,
                xmax=sx * self.xmax,
                ymax=sy * self.ymax)

  def translate(self, dx, dy):
    """Returns translated bounding box."""
    return BBox(xmin=dx + self.xmin,
                ymin=dy + self.ymin,
                xmax=dx + self.xmax,
                ymax=dy + self.ymax)

  def map(self, f):
    """Returns bounding box modified by applying f for each coordinate."""
    return BBox(xmin=f(self.xmin),
                ymin=f(self.ymin),
                xmax=f(self.xmax),
                ymax=f(self.ymax))

  @staticmethod
  def intersect(a, b):
    """Returns the intersection of two bounding boxes (may be invalid)."""
    return BBox(xmin=max(a.xmin, b.xmin),
                ymin=max(a.ymin, b.ymin),
                xmax=min(a.xmax, b.xmax),
                ymax=min(a.ymax, b.ymax))

  @staticmethod
  def union(a, b):
    """Returns the union of two bounding boxes (always valid)."""
    return BBox(xmin=min(a.xmin, b.xmin),
                ymin=min(a.ymin, b.ymin),
                xmax=max(a.xmax, b.xmax),
                ymax=max(a.ymax, b.ymax))

  @staticmethod
  def iou(a, b):
    """Returns intersection-over-union value."""
    intersection = BBox.intersect(a, b)
    if not intersection.valid:
      return 0.0
    area = intersection.area
    return area / (a.area + b.area - area)


def input_size(interpreter):
  """Returns input image size as (width, height) tuple."""
  _, height, width, _ = interpreter.get_input_details()[0]['shape']
  return width, height


def input_tensor(interpreter):
  """Returns input tensor view as numpy array of shape (height, width, 3)."""
  tensor_index = interpreter.get_input_details()[0]['index']
  return interpreter.tensor(tensor_index)()[0]


def set_input(interpreter, size, resize):
  """Copies a resized and properly zero-padded image to the input tensor.
  Args:
    interpreter: Interpreter object.
    size: original image size as (width, height) tuple.
    resize: a function that takes a (width, height) tuple, and returns an RGB
      image resized to those dimensions.
  Returns:
    Actual resize ratio, which should be passed to `get_output` function.
  """
  width, height = input_size(interpreter)
  w, h = size
  scale = min(width / w, height / h)
  w, h = int(w * scale), int(h * scale)
  tensor = input_tensor(interpreter)
  tensor.fill(0)  # padding
  _, _, channel = tensor.shape
  tensor[:h, :w] = np.reshape(resize((w, h)), (h, w, channel))
  return scale, scale


def output_tensor(interpreter, i):
  """Returns output tensor view."""
  tensor = interpreter.tensor(interpreter.get_output_details()[i]['index'])()
  return np.squeeze(tensor)


def get_output(interpreter, score_threshold, image_scale=(1.0, 1.0)):
  """Returns list of detected objects."""
  boxes = output_tensor(interpreter, 0)
  class_ids = output_tensor(interpreter, 1)
  scores = output_tensor(interpreter, 2)
  count = int(output_tensor(interpreter, 3))

  width, height = input_size(interpreter)
  image_scale_x, image_scale_y = image_scale
  sx, sy = width / image_scale_x, height / image_scale_y

  def make(i):
    ymin, xmin, ymax, xmax = boxes[i]

    return Object(
        id=int(class_ids[i]),
        score=float(scores[i]),
        bbox=BBox(xmin=xmin,
                  ymin=ymin,
                  xmax=xmax,
                  ymax=ymax).scale(sx, sy).map(int))

  return [make(i) for i in range(count) if scores[i] >= score_threshold]

 

main.py

import detect
import tflite_runtime.interpreter as tflite
import time

from PIL import Image
from PIL import ImageDraw
import cv2
import numpy as np
import time
import os



# .tflite interpreter
interpreter = tflite.Interpreter(
    os.path.join(os.getcwd(), "ssd_mobilenet_v2_face_quant_postprocess_edgetpu.tflite"),
    experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')]
    )
interpreter.allocate_tensors()

# Draws the bounding box and label for each object.
def draw_objects(image, objs):
    for obj in objs:
        bbox = obj.bbox
        
        cv2.rectangle(image,(bbox.xmin, bbox.ymin), (bbox.xmax, bbox.ymax), (0, 255, 0),2)

        bbox_point_w = bbox.xmin + ((bbox.xmax-bbox.xmin) // 2)
        bbox_point_h = bbox.ymin + ((bbox.ymax-bbox.ymin) // 2) 
        
        cv2.circle(image, (bbox_point_w, bbox.ymax-bbox.ymin), 5, (0,0,255),-1)
        cv2.putText(image, text='%d%%' % (obj.score*100), org=(bbox.xmin, bbox.ymin), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1.0, color=(0, 255, 0), thickness=1, lineType=cv2.LINE_AA)

        

        
def main():
    cap = cv2.VideoCapture(0)    

    while True:
        ret, image = cap.read()

	    # image reshape
        image = cv2.resize(image, dsize=(320, 320), interpolation=cv2.INTER_AREA)
	    # image BGR to RGB
        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

       	tensor = detect.input_tensor(interpreter=interpreter)[:, :] = image.copy() 
        tensor.fill(0)  # padding        
        interpreter.invoke()  # start
        
        objs = detect.get_output(interpreter, 0.5, (1.0, 1.0))
        
        if len(image):
            draw_objects(image, objs)

        image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
        cv2.imshow('face detector', image)

        k = cv2.waitKey(30) & 0xff
        if k == 27: # press 'ESC' to quit # ESC exit
            break


if __name__ == '__main__':
    main()

 

 

위 코드를 돌려보시면, delay가 전혀 없이 높은 성능으로 얼굴 인식을 합니다. 

 

그렇기 때문에 얼굴 인식하고 다른 프로세스를 추가할 수 있습니다. 예를 들어 얼굴 인식하고 마스크를 검출하더라도 

전혀 delay가 없습니다.

 

그리고 얼굴인식을 하기 전,  전처리가 없습니다. 

 

딥러닝으로 만든 모델을 돌려보면, 실제로 적용되지 않는 경우가 많습니다. 

 

만들어진 모델을 성능을 높이기 위해 OpenCV의 전처리를 많이 사용하는 경우가 많습니다. 

 

하지만 이 같은 경우는 OpenCV는 사이즈를 줄이거나, 단순히 View 역할만 하고 그 외 작업은 전혀 하지 않습니다. 

 

 

마스크를 쓰지 않았을 때
마쓰크를 썻을 때

딥러닝을 공부하신다면, 실제로 모델을 적용해보시는 것도 많은 공부를 하는데 도움이 될 것 같습니다. 

728x90
반응형
반응형

 

 

 

많은 분들이 자바 전체 소스코드를 원하셔서 이렇게라도 올려봅니다.

 

제가 프로젝트를 다 삭제해서 잠깐 코드로 적어놓고 테스트는 하지 않았습니다. 

 

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
반응형

+ Recent posts