/**
 * 
 */
package com.tcl.factory.view;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.bluetooth.BluetoothDevice;

import com.tcl.tvmanager.TvManager;
import com.tcl.factory.bsp.TCL;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.thirdparty.ThirdParty;

/**
 * function: 针对于有些项目没有3D功能，但有蓝牙功能。所以特此增加此广播接收器，来单独设置蓝牙的开和关
 * @author wangst
 * @Date 20150701
 */
public class BluetoothReceiver extends BroadcastReceiver {

	private final String TAG = "BluetoothReceiver";
	public static Context mContext;
	public static String[] DeviceName = new String[30];
	public static short[] DeviceRssi = new short[30];
	public static int DeviceNum =0;
	private final String BLUETOOTH_BROADCAST = "android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED";
	/* (non-Javadoc)
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		TCL.implemetThirdPartyIsntance(context);
		if(ThirdParty.getPModeStatus())
		{
		String action = intent.getAction();
		Log.v(TAG, "onReceive() Action:" + action);	
		if(null != action && action.equals(BLUETOOTH_BROADCAST)){ // "android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED"
			BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
			if(null == adapter){
				Log.v(TAG, "BluetoothAdapter = null");
				return;
			}
			int blueState = adapter.getState();
			Log.v(TAG, "onReceive() blueState:" + blueState);
			if(BluetoothAdapter.STATE_ON == blueState){ //蓝牙打开或蓝牙正在打开
				// 0,1
				ThirdParty.setBluetooth3dWorkStatus(1);
			} else if(BluetoothAdapter.STATE_OFF == blueState
					|| BluetoothAdapter.STATE_TURNING_OFF == blueState){  //蓝牙关闭或蓝牙正关闭
				// 0,1
				ThirdParty.setBluetooth3dWorkStatus(0);
			} 
		}
		if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
			int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
			Log.v(TAG, "onReceive() state:" + state);
			if(BluetoothAdapter.STATE_ON == state){ //蓝牙打开或蓝牙正在打开
				// 0,1
				ThirdParty.setBluetooth3dWorkStatus(1);
			} else if(BluetoothAdapter.STATE_OFF == state
					|| BluetoothAdapter.STATE_TURNING_OFF == state){  //蓝牙关闭或蓝牙正关闭
				// 0,1
				ThirdParty.setBluetooth3dWorkStatus(0);
			} 
		}
		
		if(action.equals(BluetoothDevice.ACTION_FOUND)){//每扫描到一个设备，系统都会发送此广播。  
            //获取蓝牙设备  
            BluetoothDevice scanDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
           
            if(scanDevice != null && scanDevice.getName() != null) 
            {
            	if(DeviceNum < 30)           //最多添加30个蓝牙设备
            	{	            
		            	for(int i = 0;i < DeviceNum;i++ )
		            	{
		            		if((DeviceName[i].equals(scanDevice.getName())))   //判断新发现设备是否已经在列表中
		            		{
		            			return;
		            		}
		            	} 
	            	DeviceName[DeviceNum] = scanDevice.getName();
        			DeviceRssi[DeviceNum] = intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);
        			DeviceRssi[DeviceNum] +=100;   //信号rssi为负数 转换成正数
	            	DeviceNum++;
            	}

            }       
            
        }else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){
        	StringBuilder BtDeviceInfoBuf = new StringBuilder();
        	TvManager tvManager = TvManager.getInstance(mContext);
        	//property字符串拼接
        	if(DeviceNum>0)
        	{
        		BtDeviceInfoBuf.append(DeviceName[0]+"*"+DeviceRssi[0]);
        		for (int i =1;i < DeviceNum ;i++)
            	{                       		
            		BtDeviceInfoBuf.append("|"+DeviceName[i]+"*"+DeviceRssi[i]);            		
            	}
        	}        
        	tvManager.getProperty().set(DEFS.SITA_PROPERTY_BLUETOOTH_CACHE_DEVICES,BtDeviceInfoBuf.toString());
        	Log.d("Factory BluetoothDevicesFonud setProperty",BtDeviceInfoBuf.toString());
        	
        	for(int i =0;i < DeviceNum ;i++)
        	{
        		DeviceName[i] = "";
        		DeviceRssi[i] = 0;
        	}
        	DeviceNum = 0;
        } 
	}
	}

}
