package org.japan.techbooster.sample.BlueTooth;

import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class BlueToothSample extends Activity {
    /** Called when the activity is first created. */
	
	static BluetoothAdapter mBtAdapter;
	int REQUEST_ENABLE_BLUETOOTH = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        boolean suportRet = supportConfirm();
        if(suportRet == false){
        	//ñT|[g¾Á½ê
        	Toast.makeText(this, "BTñT|", Toast.LENGTH_SHORT).show();
        	finish();
        }else{
	        Toast.makeText(this, "BTT|[g³êÄÜ·", Toast.LENGTH_SHORT).show();
	        
	        //BluetoothªOn©Off©»f·é
	        //Off¾Á½êÍ_CAO\¦µÄONÉ·éæ¤£·
	        boolean btEnabledRet = OnOffConfirm();
	        if(btEnabledRet == false){
	        	//Off¾Á½ê
	        	//Intent°½ ÆAONÉ·éOÉ±ÌÉüé½ßAONÉµ½ãÌÍ±±É©È¢B
	        }
        }
    }
    
    private boolean supportConfirm(){
        //BluetoothªT|[g³êÄ¢é©Û©ðm©ßé
        BluetoothAdapter Bt = BluetoothAdapter.getDefaultAdapter();
        if(Bt.equals(null)){
        	
        	return false;
        }

        mBtAdapter = Bt;
        return true;
    }
    
    private boolean OnOffConfirm(){
    	//BluetoothªOnÉÈÁÄ¢é©Ç¤©ðm©ßé
    	boolean btEnable = mBtAdapter.isEnabled();
    	if(btEnable == true){
    		Intent intent = new Intent(getApplicationContext(), DeviceListActivity.class);
    		startActivity(intent);        		
    		return true;
    	}
    	//OnÅÈ¢êAOnÉ·é½ßÌ_CAOð\¦·é
    	Intent BtOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    	startActivityForResult(BtOn, REQUEST_ENABLE_BLUETOOTH);
    	return false;
    }
    
    @Override
    protected void onActivityResult(int requestCode, int ResultCode, Intent date){
    	if(requestCode == REQUEST_ENABLE_BLUETOOTH){
    		if(ResultCode == Activity.RESULT_OK){
    			//BluetoothªOn¾Á½êÌ
    			Log("BTªOnÉÈèÜµ½");
    			//yAOæÊÉJÚ·é
        		Intent intent = new Intent(getApplicationContext(), DeviceListActivity.class);
        		startActivity(intent);        		
    		}else{
    			Log("BTªOnÉµÄàç¦Ü¹ñÅµ½");
    		}
    	}
    }
    
    public void Log(String string){
		Toast.makeText(this, string, Toast.LENGTH_SHORT).show();    	
    }
}