마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

[부탁드려요] 안드로이드 블루투스 디바이스 검색 문의 입니다.

0 추천

현재 블루투스 통신쪽을 라이브러리로 제공하려고 

블루투스 통신부분을 개발중에 있습니다.

1. jar로 묶을 프로젝트 생성

2. 1번을 테스트해 볼 APP 프로젝트 생성

3. App 프로젝트에서 1번의 프로젝트를 참조

 

이 방식으로 개발과 테스트를 동시에 진행중입니다.

1번에서 생성한 프로젝트에서 블루투스 디바이스를 검색하는 함수를 만들었는데, 계속 nullPointException이 떨어집니다.

소스를 같이 올리오니, 도움 부탁드립니다.

 

public int doDeviceDiscovery(){

        

        stopDeviceDiscovery();

        

        int deviceDiscoveryStatus = -1;

        

//        // Register for broadcasts when a device is discovered

//        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);

//        this.registerReceiver(mDiscoveryReceiver, filter);

//        

//        // Register for broadcasts when discovery has finished

//        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

//        this.registerReceiver(mDiscoveryReceiver, filter);

        

        this.registerReceiver(mDiscoveryReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));        

        try {            

            Log.d(TAG, "in doDeviceDiscovery!");

 

            boolean flag = false;

            //mBtAdapter == null check

            if(mBtAdapter == null){

                mBtAdapter = BluetoothAdapter.getDefaultAdapter();

            }

            if(!mBtAdapter.isEnabled()){

                Log.d(TAG, "Bluetooth disable!");

                deviceDiscoveryStatus = -2;

                return deviceDiscoveryStatus;

            }

                if(btCheck){

                    Log.d(TAG, "doDeviceDiscovery!");

                }

                

                if(mBtAdapter.isDiscovering()){

                    mBtAdapter.cancelDiscovery();

                }            

                mBtAdapter.startDiscovery();

                //Debuging

                if(mBtAdapter.isDiscovering()){

                    Log.e(TAG, "Device Discovering...");

                }

                flag = mBtAdapter.startDiscovery();

                

                if(flag){

                    //success

                    deviceDiscoveryStatus = 0;

                }else{

                    //fail

                    deviceDiscoveryStatus = -1;

                }

                Log.d(TAG, "Start Device Discovery!");            

        } catch (Exception e) {

            // TODO: handle exception

            e.printStackTrace();

            return -3;

        }        

        return deviceDiscoveryStatus;

       }    //doDeviceDiscovery

 

  this.registerReceiver(mDiscoveryReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

 이 부분에서 Exception이 발생합니다.

그 위에 IntentFilter를 생성하는 부분에서도 똑같이 발생하여

  this.registerReceiver(mDiscoveryReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));  으로

대체 해보았지만, 역시나 Exception이 발생합니다.

04-26 19:36:38.193: E/AndroidRuntime(6996): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)' on a null object reference
04-26 19:36:38.193: E/AndroidRuntime(6996):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:503)

로그도 함께 첨부하오니, 도움 부탁드립니다.

앱을 개발했었을떄는 IntentFilter부분을 onCreate()에서 선언해줬지만,

SDK 개발이라 별도 함수로 뺀 것인데, 뭐가 문제인지 모르겠네요.

혹시 SDK 개발하는 프로젝트에 Manifest에 뭔가를 추가해 줘야 하는건가요?....

 

쮸뿌쮸뿌 (2,470 포인트) 님이 2016년 4월 26일 질문
쮸뿌쮸뿌님이 2016년 4월 26일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...