Skip to content

Instantly share code, notes, and snippets.

@wgkoro
Created July 7, 2012 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wgkoro/3065965 to your computer and use it in GitHub Desktop.
Save wgkoro/3065965 to your computer and use it in GitHub Desktop.
on{X} WiFi Toggle
// Initializing variables ==================================
// WiFi, BlueToothをONにする場所をセット。
// http://www.geocoding.jp/ を使うと緯度経度が分かります。
var location = [
{ name : "tokyo_tower", latitude : "35.65861", longitude : "139.745447" },
{ name : "ueno_station", latitude : "35.713768", longitude : "139.777254" }
];
var radius = 1000; // 目標地点を中心とした半径距離。この円を境目にスイッチ稼働。単位:メートル
var turn_on_bluetooth = false; // BluetoothもONにするならtrueに。
var show_message = true; // スイッチ稼働時にメッセージを表示する場合はtrueに。
// End of variables initializing ===========================
var regions = [];
var notification = device.notifications.createNotification('WiFi Switcher');
var setTrigger = function(){
var len = location.length;
if (len < 1){
return;
}
for (var i=0;i<len;i++){
var loc = location[i];
var region = device.regions.createRegion({
latitude: parseFloat(loc.latitude, 10),
longitude: parseFloat(loc.longitude, 10),
name: loc.name,
radius: radius
});
region.on('enter', function () {
switchNetwork(true, loc.name);
});
region.on('exit', function () {
switchNetwork(false, loc.name);
});
regions.push(region);
}
var len_r = regions.length;
for (var s=0;s<len_r;s++){
device.regions.startMonitoring(regions[s]);
}
};
var switchNetwork = function(turnTo, name){
var bt = '';
device.network.wifiEnabled = turnTo;
if(turn_on_bluetooth){
device.bluetooth.enabled = turnTo;
bt = 'and Bluetooth';
}
if(show_message){
if(turnTo){
notification.content = 'Set WiFi' +bt +' ON. (' +name +')';
}
else{
notification.content = 'Set WiFi' +bt +' OFF. (' +name +')';
}
notification.show();
}
};
setTrigger();
console.log('Started WiFi Switcher!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment