Back to project page WifiHotspotBTEnabler.
The source code is released under:
GNU General Public License
If you think the Android project WifiHotspotBTEnabler listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package ca.diogosoares.android.HotspotBTEnabler; // w w w. j av a 2s. c om import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Locale; import org.json.JSONArray; import com.google.gson.Gson; import android.app.AlertDialog; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.net.wifi.WifiManager; import ca.diogosoares.android.listeners.BluetoothUtilitiesListener; import ca.diogosoares.android.listeners.HotspotUtilitiesListener; import ca.diogosoares.android.listeners.WifiUtilitiesListener; import ca.diogosoares.android.utilities.BluetoothUtilities; import ca.diogosoares.android.utilities.HotspotUtilities; import ca.diogosoares.android.utilities.WifiUtilities; /*** * * @author Diogo Soares, 02/2014 * */ public class HotspotBTEnablerManager implements BluetoothUtilitiesListener, HotspotUtilitiesListener, WifiUtilitiesListener { private Config mConfig = null; private Context mContext = null; private ArrayList<HotspotBTEnablerListener> mListeners = null; private int mEnableStateChanged = 0; private HotspotUtilities mHotspotUtilities = null; private WifiUtilities mWifiUtilities = null; private BluetoothUtilities mBluetoothUtilities = null; private ArrayList<String> mLogs = null; public HotspotBTEnablerManager(Context context) { this.mContext = context; this.mListeners = new ArrayList<HotspotBTEnablerListener>(); this.mBluetoothUtilities = new BluetoothUtilities(mContext); this.mHotspotUtilities = new HotspotUtilities(mContext); this.mWifiUtilities = new WifiUtilities(mContext); this.mLogs = new ArrayList<String>(20); loadAppSettings(); registerAsListener(); } public void onDispose() { removeAsListener(); } public void registerAsListener(HotspotBTEnablerListener listener) { this.mListeners.add(listener); } public void removeAsListener(HotspotBTEnablerListener listener) { this.mListeners.remove(listener); } private void registerAsListener() { this.mBluetoothUtilities.setListener(this); this.mHotspotUtilities.setListener(this); this.mWifiUtilities.setListener(this); } private void removeAsListener() { this.mBluetoothUtilities.removeListener(this); this.mHotspotUtilities.removeListener(this); this.mWifiUtilities.removeListener(this); } public void setEnableWifiHotspot(boolean enable) { disableHotspotStateChanged(); int NewState = enable ? HotspotUtilities.WIFI_AP_STATE_ENABLED : HotspotUtilities.WIFI_AP_STATE_DISABLED ; if (mHotspotUtilities.getWifiAPState() != NewState) { if (enable) { mWifiUtilities.setEnableWifiHotspot(false); } mHotspotUtilities.setEnableWifiHotspot(enable); if (!enable && mWifiUtilities.getPreviousFinalState() == WifiManager.WIFI_STATE_ENABLED) { mWifiUtilities.setEnableWifiHotspot(true); } } enableHotspotStateChanged(); } public String getLightweightState() { return "WiFi state: " + WifiUtilities.getWifiStateString(mWifiUtilities.getState()) + " " + System.getProperty("line.separator") + "WiFi Hotspot state: " + HotspotUtilities.getWifiAPStateString(mHotspotUtilities.getWifiAPState()) + System.getProperty("line.separator") + "Bluetooth state: " + BluetoothUtilities.getStateString(mBluetoothUtilities.getState()) + System.getProperty("line.separator") + "Bluetooth Num Bonded Devices: " + String.valueOf(mBluetoothUtilities.getBondedDevices().size()) + System.getProperty("line.separator"); } public String getFullState() { String fullState = getLightweightState(); fullState += (System.getProperty("line.separator") + "Devices (Name - Address - Major - Type)" + System.getProperty("line.separator")); if (mBluetoothUtilities.getBondedDevices().size() > 0) { ArrayList<String> tempDevicesInfo = new ArrayList<String>(mBluetoothUtilities.getBondedDevices().size()); for (BluetoothDevice bd:mBluetoothUtilities.getBondedDevices()) { if (bd.getAddress().equals(getSelectedDeviceAddress())) { fullState += ("? ?" + bd.getName() + " - " + bd.getAddress() + " - " + BluetoothUtilities.getMajorString(bd.getBluetoothClass().getMajorDeviceClass()) + " - " + BluetoothUtilities.getDeviceType(bd.getType()) + System.getProperty("line.separator")); } else { tempDevicesInfo.add(" ?" + bd.getName() + " - " + bd.getAddress() + " - " + BluetoothUtilities.getMajorString(bd.getBluetoothClass().getMajorDeviceClass()) + " - " + BluetoothUtilities.getDeviceType(bd.getType()) + System.getProperty("line.separator")); } } Collections.sort(tempDevicesInfo); for (String s:tempDevicesInfo) fullState += s; } else { fullState += " Any device available"; } return fullState; } public int getWifiHotspotState() { return mHotspotUtilities.getWifiAPState(); } public String getLogs() { StringBuilder log = new StringBuilder(); for (String line:mLogs) log.append(line + System.getProperty("line.separator")); return log.toString(); } public void setEnableAutoMode(boolean enable) { mConfig.enableAuoMode = enable; saveAppSettings(); raiseHotspotStateChanged(); } public boolean getEnableAutoMode() { return mConfig.enableAuoMode; } public ArrayList<BTDevice> getBTDevices() { ArrayList<BTDevice> BTDevices = new ArrayList<BTDevice>(); for (BluetoothDevice bt:mBluetoothUtilities.getBondedDevices()) BTDevices.add(new BTDevice(bt.getName(), bt.getAddress())); Collections.sort(BTDevices, new BTDeviceComparator()); return BTDevices; } public void setSelectedDevice(BTDevice device) { if (!mConfig.selectedDeviceAddress.equals(device.address)) { mConfig.selectedDeviceName = device.name; mConfig.selectedDeviceAddress = device.address; setEnableWifiHotspot(mHotspotUtilities.getWifiAPState() == HotspotUtilities.WIFI_AP_STATE_ENABLED); saveAppSettings(); logEvent("New device selected: " + device.toString()); } } public String getSelectedDeviceName() { return mConfig.selectedDeviceName; } public String getSelectedDeviceAddress() { return mConfig.selectedDeviceAddress; } @Override public void onBluetoothStateChanged() { raiseHotspotStateChanged(); } @Override public void onBluetoothACLChanged(String action, BluetoothDevice btDevice) { if (btDevice.getAddress().equals(mConfig.selectedDeviceAddress)) { String ACLChange = ""; if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { ACLChange = "Connected:"; if (mConfig.enableAuoMode && btDevice.getAddress().equals(mConfig.selectedDeviceAddress)) { setEnableWifiHotspot(true); } } else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) { ACLChange = "Disconnect request:"; } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { ACLChange = "Disconnected:"; if (mConfig.enableAuoMode && btDevice.getAddress().equals(mConfig.selectedDeviceAddress)) { setEnableWifiHotspot(false); } } logEvent(ACLChange + " " + btDevice.getName()); } } private void logEvent(String log) { if (mLogs.size() == 20) { mLogs.remove(0); } SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a",Locale.getDefault()); mLogs.add(formatter.format(new Date()) + " " + log); for (HotspotBTEnablerListener listener:mListeners) listener.onLogsChanged(); saveAppSettings(); } private void saveAppSettings() { SharedPreferences settings = mContext.getSharedPreferences("UserInfo", 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("selectedDeviceName", mConfig.selectedDeviceName); editor.putString("selectedDeviceAddress", mConfig.selectedDeviceAddress); editor.putBoolean("enableAutoMode", mConfig.enableAuoMode); Gson gson = new Gson(); String json = gson.toJson(mLogs); editor.putString("logs", json); editor.commit(); } private void loadAppSettings() { mConfig = new Config(); SharedPreferences settings = mContext.getSharedPreferences("UserInfo", 0); mConfig.selectedDeviceName = settings.getString("selectedDeviceName", ""); mConfig.selectedDeviceAddress = settings.getString("selectedDeviceAddress", ""); mConfig.enableAuoMode = settings.getBoolean("enableAutoMode", false); Gson gson = new Gson(); String json = settings.getString("logs", ""); if (!json.equals("")) mLogs = (ArrayList<String>) gson.fromJson(json, ArrayList.class); } @Override public void onWifiStateChanged() { raiseHotspotStateChanged(); } @Override public void onWifiAPStateChanged() { raiseHotspotStateChanged(); } private void enableHotspotStateChanged() { mEnableStateChanged -= 1; if (mEnableStateChanged == 0) { raiseHotspotStateChanged(); } } private void disableHotspotStateChanged() { mEnableStateChanged += 1; } private void raiseHotspotStateChanged() { if (mEnableStateChanged == 0) { for (HotspotBTEnablerListener listener:mListeners) listener.onHotspotStateChanged(); } } @Override public void onBluetoothBondStateChanged() { for (HotspotBTEnablerListener listener:mListeners) listener.onBluetoothBondChanged(); } }