Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 3개 있습니다.)

Xamarin - 기존 리모컨 기능을 핸드폰의 적외선 송신으로 구현

집에 있는 리모컨의 기능을 핸드폰으로 복사하는 것이 가능합니다. 이를 위해서는 우선 기존 리모컨들의 신호 값을 알아야 하는데, 다음의 글에 따라 아두이노를 이용하면 가능합니다.

아두이노에서 적외선 수신기 기본 사용법
; https://www.sysnet.pe.kr/2/0/11670

사실 송신 기능도 핸드폰 대신 아두이노를 이용한 송신기로 구현할 수는 있습니다.

아두이노에서 적외선 송신기 기본 사용법
; https://www.sysnet.pe.kr/2/0/11671

문제는 저런 식으로 아두이노로 구현하는 것은 불편한 면이 많습니다. 가령, 현실적으로 쓰기 위해선 기능에 따른 버튼도 더 늘려야 하고 브레드보드가 아닌 만능 기판에, ... 케이스까지 만들기 위해 3D 프린터라든가 기타 여러 도구들까지 장만을 해야 합니다. (사실, 언젠가는 저도 ^^ 그렇게까지 해보고 싶습니다.) 그런데, 생각해 보면 마감 잘 된 도구가 하나 있습니다. 바로 휴대폰입니다. 검색해 보면 마침 API 19(KitKat)부터 적외선 송신 기능도 제공한다고 합니다.

Using Infrared port
; https://forums.xamarin.com/discussion/12205/using-infrared-port

Xamarin의 경우 ConsumerIrManager 타입을 이용해,

Android.Hardware.ConsumerIrManager Class
; https://developer.xamarin.com/api/type/Android.Hardware.ConsumerIrManager/

쉽게 접근할 수 있습니다. 그럼 뭐, 다 준비된 거죠. ^^ Visual Studio에서 Xamarin 프로젝트를 생성하고 해당 App에서 적외선 송신 기능을 사용하겠다는 요구를 manifest에 기록하고,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0"
          package="IRController.IRController">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.TRANSMIT_IR"></uses-permission>

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
    </application>

</manifest>

다음과 같이 리모컨 기능을 대신할 버튼을 만든 다음,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/btnTurnLightOnOff"
    />
</RelativeLayout>

이것을 누르면 그에 해당하는 적외선 송신을 하면 됩니다.

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;

namespace IRController
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        Android.Hardware.ConsumerIrManager _ir;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            Button button = FindViewById<Button>(Resource.Id.btnTurnLightOnOff);
            button.Click += btnTurnLightOnOff_Click;

        }

        private void btnTurnLightOnOff_Click(object sender, System.EventArgs e)
        {
            _ir = ApplicationContext.GetSystemService(Context.ConsumerIrService)
                as Android.Hardware.ConsumerIrManager;

            int [] light_onoff_signal_buf = { 3550, 1750, 600, 400, 600, 1800, 550, 450, 600, 450, 600, 1800, 550, 450, 600, 450, 600, 500, 600, 450, 600, 400, 650, 400, 550 };
            _ir.Transmit(38, light_onoff_signal_buf);
        }
    }
}

위의 코드에서 Transmit 메서드를 보면,

Android.Hardware.ConsumerIrManager.Transmit Method
; https://developer.xamarin.com/api/member/Android.Hardware.ConsumerIrManager.Transmit/p/System.Int32/System.Int32[]/

지난번 "아두이노에서 적외선 송신기 기본 사용법"의 코드와 거의 똑같다는 것을 알 수 있습니다. 즉, Raw Data와 함께 주파수만 지정하면 되는 것입니다.

제가 가지고 있는 핸드폰이 LG V20인데 다행히 적외선 송신 기능이 있는 제품입니다. 위의 앱을 설치해서 실행해 보면, 와~~~ ^^ 정말 핸드폰으로 형광등을 On/Off 하는 것이 가능해졌습니다. 그런데, 여기서 문제가 있습니다. V20 핸드폰의 적외선 송신기가 원래의 형광등을 제어하던 리모컨의 출력보다 약한 것 같습니다. 재미있는 것은 동일 높이의 정면에서 하면 핸드폰으로 잘 동작을 하는데 높이가 달라지거나 사잇각이 커지면 핸드폰으로는 대상 수신기에서 잘 잡아내지 못합니다.

종합해 보면, 어떤 적외선 수신 제품을 (현실적으로) 제어하고 싶은 경우라면 직접 핸드폰에서 신호를 보내는 것보다는 네트워크 기능이 있는 아두이노를 상시 켜 놓고 그 아두이노의 적외선 송신기를 (잘 수신이 되도록 위치를 잡아 놓고) 사용하는 것이 좋을 것 같습니다. 암튼, 좀 아쉽군요. ^^ 핸드폰 하나로 다 될 것 같아서 기대에 부풀었는데.

(첨부 파일은 이 글의 Android/Xamarin 프로젝트입니다.)




적외선 통신을 다루면서 왜 38Khz를 고정 상수로 전달할까... 하는 의문이 있었는데요, 다음의 글에서 이를 설명하고 있습니다.

적외선 통신을 알아보자.
; http://dokkodai.tistory.com/106

즉, 적외선 통신은 37 ~ 42 Khz로 변조를 한다는데, 리모컨의 경우 38Khz로 하는 것이 일반적이라고 합니다.




참고로, 다음과 같은 식의 오류가 발생한다면?

Unhandled Exception:

Java.Lang.SecurityException: <Timeout exceeded getting exception details> occurred

"Timeout"이긴 하지만 예외의 종류가 SecurityException인 것이 더 중요합니다. 즉, IR 기능 사용에 대한 사용자 허락을 받을 수 있도록 manifest에 "android.permission.TRANSMIT_IR" 권한 요구를 명시하지 않아서 그런 것입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 8/29/2018]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... [91]  92  93  94  95  96  97  98  99  100  101  102  103  104  105  ...
NoWriterDateCnt.TitleFile(s)
11377정성태11/29/201713048.NET Framework: 700. CommonOpenFileDialog 사용 시 사용자가 선택한 파일 목록을 구하는 방법 [3]파일 다운로드1
11376정성태11/28/201716493VS.NET IDE: 123. Visual Studio 편집기의 \r\n (crlf) 개행을 \n으로 폴더 단위로 설정하는 방법
11375정성태11/28/201712860오류 유형: 434. Visual Studio로 ASP.NET 디버깅 중 System.Web.HttpException - Could not load type 오류
11374정성태11/27/201717389사물인터넷: 14. 라즈베리 파이 - (윈도우의 NT 서비스처럼) 부팅 시 시작하는 프로그램 설정 [1]
11373정성태11/27/201716453오류 유형: 433. Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록
11372정성태11/25/201719702사물인터넷: 13. 윈도우즈 사용자를 위한 라즈베리 파이 제로 W 모델을 설정하는 방법 [4]
11371정성태11/25/201713667오류 유형: 432. Hyper-V 가상 스위치 생성 시 Failed to connect Ethernet switch port 0x80070002 오류 발생
11370정성태11/25/201713243오류 유형: 431. Hyper-V의 Virtual Switch 생성 시 "External network" 목록에 특정 네트워크 어댑터 항목이 없는 경우
11369정성태11/25/201715548사물인터넷: 12. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠) [1]
11368정성태11/25/201720746.NET Framework: 699. UDP 브로드캐스트 주소 255.255.255.255와 192.168.0.255의 차이점과 이를 고려한 C# UDP 서버/클라이언트 예제 [2]파일 다운로드1
11367정성태11/25/201720839개발 환경 구성: 337. 윈도우 운영체제의 route 명령어 사용법
11366정성태11/25/201712629오류 유형: 430. 이벤트 로그 - Cryptographic Services failed while processing the OnIdentity() call in the System Writer Object.
11365정성태11/25/201714900오류 유형: 429. 이벤트 로그 - User Policy could not be updated successfully
11364정성태11/24/201715926사물인터넷: 11. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 (절대 좌표) [2]
11363정성태11/23/201715677사물인터넷: 10. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 (두 번째 이야기)
11362정성태11/22/201713260오류 유형: 428. 윈도우 업데이트 KB4048953 - 0x800705b4 [2]
11361정성태11/22/201715994오류 유형: 427. 이벤트 로그 - Filter Manager failed to attach to volume '\Device\HarddiskVolume??' 0xC03A001C
11360정성태11/22/201715696오류 유형: 426. 이벤트 로그 - The kernel power manager has initiated a shutdown transition.
11359정성태11/16/201715077오류 유형: 425. 윈도우 10 Version 1709 (OS Build 16299.64) 업그레이드 시 발생한 문제 2가지
11358정성태11/15/201719412사물인터넷: 9. Visual Studio 2017에서 Raspberry Pi C++ 응용 프로그램 제작 [1]
11357정성태11/15/201720194개발 환경 구성: 336. 윈도우 10 Bash 쉘에서 C++ 컴파일하는 방법
11356정성태11/15/201721494사물인터넷: 8. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법 [4]
11355정성태11/15/201717987사물인터넷: 7. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 [2]파일 다운로드2
11354정성태11/14/201721262사물인터넷: 6. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법 [8]
11353정성태11/14/201718985사물인터넷: 5. Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 이더넷 카드로 쓰는 방법 [1]
11352정성태11/14/201714679사물인터넷: 4. Samba를 이용해 윈도우와 Raspberry Pi간의 파일 교환 [1]
... [91]  92  93  94  95  96  97  98  99  100  101  102  103  104  105  ...