일반 블루투스 채팅예제를 바탕으로 현재 진행중입니다.
BluetoothChat에서 블루투스 연결이 되어있을 경우
/**
* Sends a message.
* @param message A string of text to send.
*/
private void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
// Check that there's actually something to send
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send);
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}
}
아래와 같이 입력된 String을 byte로 변경하여 BluetoothService의 write 함수를 통해 데이터를 전송합니다.
위와 같이 저는 시계의 시간을 변경하기 위해서
Hour=Integer.parseInt(time_hour.getText().toString());
Min=Integer.parseInt(time_min.getText().toString());
Timeset[0] = (byte) (((Hour/10)<<4 | (Hour%10)) & 0xff);
Timeset[1] = (byte) (((Min/10)<<4 | (Min%10)) & 0xff);
//데이터 전송
final byte time_setting[] = new byte[]
{
(byte) 0x77, (byte) Timeset[0], (byte) Timeset[1]};
SystemClock.sleep(1000);
mService.write(time_setting);
위와 같이 byte로 전송을 하는데
mService.write(time_setting); 부분에서 nullpoint exception이 발생합니다.
아래는 Error 메시지 입니다.
12-13 17:37:53.501: E/AndroidRuntime(27896): FATAL EXCEPTION: main
12-13 17:37:53.501: E/AndroidRuntime(27896): Process: healthall.company.smartpillbox_h, PID: 27896
12-13 17:37:53.501: E/AndroidRuntime(27896): java.lang.NullPointerException: Attempt to invoke virtual method 'void healthall.company.smartpillbox_h.BluetoothService.write(byte[])' on a null object reference
12-13 17:37:53.501: E/AndroidRuntime(27896): at healthall.company.smartpillbox_h.TimerSetting$2.onClick(TimerSetting.java:77)
12-13 17:37:53.501: E/AndroidRuntime(27896): at android.view.View.performClick(View.java:5721)
12-13 17:37:53.501: E/AndroidRuntime(27896): at android.view.View$PerformClick.run(View.java:22620)
12-13 17:37:53.501: E/AndroidRuntime(27896): at android.os.Handler.handleCallback(Handler.java:739)
12-13 17:37:53.501: E/AndroidRuntime(27896): at android.os.Handler.dispatchMessage(Handler.java:95)
12-13 17:37:53.501: E/AndroidRuntime(27896): at android.os.Looper.loop(Looper.java:148)
12-13 17:37:53.501: E/AndroidRuntime(27896): at android.app.ActivityThread.main(ActivityThread.java:7406)
12-13 17:37:53.501: E/AndroidRuntime(27896): at java.lang.reflect.Method.invoke(Native Method)
12-13 17:37:53.501: E/AndroidRuntime(27896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
12-13 17:37:53.501: E/AndroidRuntime(27896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
굶은 표시가 write부분입니다. 데이터 전송시 에러가 발생하는데 이유가 궁금합니다..