현재 블루투스 통신쪽을 라이브러리로 제공하려고
블루투스 통신부분을 개발중에 있습니다.
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에 뭔가를 추가해 줘야 하는건가요?....