package com.android.BTcontrol;


import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * This is the main Activity that displays the current chat session.
 */
public class Bluetooth_connect extends Activity {
    // Debugging
    private static final String TAG = "Bluetooth_connect";
    private static final boolean D = true;

    // Message types sent from the BluetoothChatService Handler
    public static final int MESSAGE_STATE_CHANGE = 1;
    public static final int MESSAGE_READ = 2;
    public static final int MESSAGE_WRITE = 3;
    public static final int MESSAGE_DEVICE_NAME = 4;
    public static final int MESSAGE_TOAST = 5;

    // Key names received from the BluetoothChatService Handler
    public static final String DEVICE_NAME = "device_name";
    public static final String TOAST = "toast";

    // Intent request codes
    private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;

    // Layout Views
    private TextView mTitle;
    private ListView mConversationView;
    private EditText mOutEditText;
    private Button mSendButton;

    // Name of the connected device
    private String mConnectedDeviceName = null;
    // Array adapter for the conversation thread
    private ArrayAdapter<String> mConversationArrayAdapter;
    // String buffer for outgoing messages
    private StringBuffer mOutStringBuffer;
    // Local Bluetooth adapter
    private BluetoothAdapter mBluetoothAdapter = null;
    // Member object for the chat services
    private BluetoothChatService mChatService = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(D) Log.e(TAG, "+++ ON CREATE +++");

        // Set up the window layout
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

        // Set up the custom title
        mTitle = (TextView) findViewById(R.id.title_left_text);
        mTitle.setText(R.string.app_name);
        mTitle = (TextView) findViewById(R.id.title_right_text);

        // Get local Bluetooth adapter
      
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // If the adapter is null, then Bluetooth is not supported
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "BT Not Available", Toast.LENGTH_LONG).show();
            finish();
           
        }
       //=======비밀번호 변경 버튼
        Button launch=(Button)findViewById(R.id.button1);
     launch.setOnClickListener(new Button.OnClickListener(){
           public void onClick(View v) {
            Intent intent = new Intent(Bluetooth_connect.this, password_modify.class); // 두번째 액티비티를 실행하기 위한 인텐트
              startActivity(intent); // 두번째 액티비티를 실행합니다.
           }
     });
     //-----비밀번호 변경 버튼
    }

    @Override
    public void onStart() {
        super.onStart();
        if(D) Log.e(TAG, "++ ON START ++");

        // If BT is not on, request that it be enabled.
        // setupChat() will then be called during onActivityResult
       
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        // Otherwise, setup the chat session
        } else {
            if (mChatService == null) setupChat();
        }
    }

    @Override
    public synchronized void onResume() {
        super.onResume();
        if(D) Log.e(TAG, "+ ON RESUME +");

        // Performing this check in onResume() covers the case in which BT was
        // not enabled during onStart(), so we were paused to enable it...
        // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
        if (mChatService != null) {
            // Only if the state is STATE_NONE, do we know that we haven't started already
            if (mChatService.getState() == BluetoothChatService.STATE_NONE) {
              // Start the Bluetooth chat services
              mChatService.start();
            }
        }
    }

    private void setupChat() {
        Log.d(TAG, "setupChat()");

        // Initialize the array adapter for the conversation thread
        mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
        mConversationView = (ListView) findViewById(R.id.in);
        mConversationView.setAdapter(mConversationArrayAdapter);

        // Initialize the compose field with a listener for the return key
        mOutEditText = (EditText) findViewById(R.id.edit_text_out);
        mOutEditText.setOnEditorActionListener(mWriteListener);

        // Initialize the send button with a listener that for click events
        mSendButton = (Button) findViewById(R.id.button_send);
        mSendButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Send a message using content of the edit text widget
                TextView view = (TextView) findViewById(R.id.edit_text_out);
                String message = view.getText().toString();
                sendMessage(message);
            }
        });

        // Initialize the BluetoothChatService to perform bluetooth connections
        mChatService = new BluetoothChatService(this, mHandler);

        // Initialize the buffer for outgoing messages
        mOutStringBuffer = new StringBuffer("");
    }

    @Override
    public synchronized void onPause() {
        super.onPause();
        if(D) Log.e(TAG, "- ON PAUSE -");
    }

    @Override
    public void onStop() {
        super.onStop();
        if(D) Log.e(TAG, "-- ON STOP --");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Stop the Bluetooth chat services
        if (mChatService != null) mChatService.stop();
        if(D) Log.e(TAG, "--- ON DESTROY ---");
    }

    private void ensureDiscoverable() {     
        if(D) Log.d(TAG, "ensure discoverable");
     
       
        if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverableIntent);
        }
    }

    /**
     * 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);
        }
    }

    // The action listener for the EditText widget, to listen for the return key
    private TextView.OnEditorActionListener mWriteListener = new TextView.OnEditorActionListener() {
     
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            // If the action is a key-up event on the return key, send the message
         
            if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
                String message = view.getText().toString();
                sendMessage(message);
            }
            if(D) Log.i(TAG, "END onEditorAction");
            return true;
        }
    };

    // The Handler that gets information back from the BluetoothChatService
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
           
            case MESSAGE_STATE_CHANGE:
             
                if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
                switch (msg.arg1) {
                case BluetoothChatService.STATE_CONNECTED:
                    mTitle.setText(R.string.title_connected_to);
                    mTitle.append(mConnectedDeviceName);
                    mConversationArrayAdapter.clear();
                    break;
                   
                case BluetoothChatService.STATE_CONNECTING:
                    mTitle.setText(R.string.title_connecting);
                    break;
                   
                case BluetoothChatService.STATE_LISTEN:
                case BluetoothChatService.STATE_NONE:
                    mTitle.setText(R.string.title_not_connected);
                    break;
                   
                }
                break;
               
            case MESSAGE_WRITE:
                byte[] writeBuf = (byte[]) msg.obj;
                // construct a string from the buffer
                String writeMessage = new String(writeBuf);
                mConversationArrayAdapter.add("Me:  " + writeMessage);
                break;
               
            case MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                // construct a string from the valid bytes in the buffer
                String readMessage = new String(readBuf, 0, msg.arg1);
                mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
                break;
               
            case MESSAGE_DEVICE_NAME:
                // save the connected device's name
                mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                Toast.makeText(getApplicationContext(), "Connected to "
                               + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
                break;
               
            case MESSAGE_TOAST:
                Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                               Toast.LENGTH_SHORT).show();
                break;
            }
        }
    };

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
       
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                mChatService.connect(device);
            }
            break;
           
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a chat session
                setupChat();
            } else {
                // User did not enable Bluetooth or an error occured
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.scan:
            // Launch the DeviceListActivity to see devices and do scan
            Intent serverIntent = new Intent(this, DeviceListActivity.class);
            startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
            return true;
        case R.id.discoverable:
            // Ensure this device is discoverable by others
            ensureDiscoverable();
            return true;
        }
        return false;
    }

}

-----------------------------------------------------------------2번째 액티비티 소스

 

package com.android.BTcontrol;


import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Bluetooth_controlActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index_password);
              
        SharedPreferences pref= getSharedPreferences("preftest_pw", 2);//쉐어드 프리퍼런스 선언
      final String temp1 = pref.getString("password","1234"); //디폴트값 저장
       
        Button launch=(Button)findViewById(R.id.button1);
        launch.setOnClickListener(new Button.OnClickListener(){
                        public void onClick(View v) {
                         EditText edit = (EditText)findViewById(R.id.editText1);
                         String temp =  edit.getText().toString();
                         // TODO Auto-generated method stub
                      if(temp1.equals(temp)){ //비밀번호와 입력값이 일치하다면
                       Intent intent = new Intent(Bluetooth_controlActivity.this, Bluetooth_connect.class); // 두번째 액티비티를 실행하기 위한 인텐트
                         startActivity(intent); // 두번째 액티비티를 실행합니다.
                      }
                      else{ //비밀번호가 틀리다면
                      Toast.makeText(getApplicationContext(), "비밀번호 오류! 다시입력!", Toast.LENGTH_SHORT).show();             
                      }
                     }
                     
                 });
        
    }
   
}

-------------------------------------------------------------1번째 액티비티인데요...

 

첫번째 액티비티에서 인증을 거친뒤에 두번째 액티비티로 넘어가는 순서로 가는데

 

강제종료가 됩니다... 2번째 소스의경우는 블루투스 채팅 소스를 반이상 복사한거라 틀린것도 없고

 

잘 실행될텐데 좀 의문이 생깁니다..

 

요건 제 로그이고요...

09-30 09:16:33.299: WARN/KeyCharacterMap(303): No keyboard for id 0
09-30 09:16:33.299: WARN/KeyCharacterMap(303): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-30 09:16:39.212: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{4506fe30 com.android.BTcontrol/.Bluetooth_controlActivity}
09-30 09:16:39.212: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{450f6368 com.android.BTcontrol/.Bluetooth_connect}
09-30 09:17:15.561: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.android.BTcontrol/.Bluetooth_connect }
09-30 09:17:15.630: ERROR/Bluetooth_connect(303): +++ ON CREATE +++
09-30 09:17:15.689: DEBUG/AndroidRuntime(303): Shutting down VM
09-30 09:17:15.689: WARN/dalvikvm(303): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-30 09:17:15.709: ERROR/AndroidRuntime(303): FATAL EXCEPTION: main
09-30 09:17:15.709: ERROR/AndroidRuntime(303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.BTcontrol/com.android.BTcontrol.Bluetooth_connect}: java.lang.NullPointerException
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.os.Looper.loop(Looper.java:123)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at java.lang.reflect.Method.invoke(Method.java:521)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at dalvik.system.NativeStart.main(Native Method)
09-30 09:17:15.709: ERROR/AndroidRuntime(303): Caused by: java.lang.NullPointerException
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at com.android.BTcontrol.Bluetooth_connect.onCreate(Bluetooth_connect.java:96)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-30 09:17:15.709: ERROR/AndroidRuntime(303):     ... 11 more
09-30 09:17:15.730: WARN/ActivityManager(59):   Force finishing activity com.android.BTcontrol/.Bluetooth_controlActivity
09-30 09:17:16.189: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{45089690 com.android.BTcontrol/.Bluetooth_connect}
09-30 09:17:26.754: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{450b89b8 com.android.BTcontrol/.Bluetooth_controlActivity}
09-30 09:17:26.754: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{45089690 com.android.BTcontrol/.Bluetooth_connect}
09-30 09:17:51.959: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
09-30 09:19:17.659: INFO/Process(303): Sending signal. PID: 303 SIG: 9
09-30 09:19:17.690: INFO/ActivityManager(59): Process com.android.BTcontrol (pid 303) has died.
09-30 09:19:17.690: INFO/WindowManager(59): WIN DEATH: Window{4509dc40 com.android.BTcontrol/com.android.BTcontrol.Bluetooth_controlActivity paused=true}
09-30 09:19:17.769: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 303 uid 10042

 

 

뭐가 문제인지좀 부탁드립니다..ㅠ_ㅠ