Skip to content

Instantly share code, notes, and snippets.

View massimilianochiodi's full-sized avatar

Massimiliano Chiodi massimilianochiodi

View GitHub Profile
@massimilianochiodi
massimilianochiodi / reduce_expand_byte.java
Created April 15, 2022 08:31
Reduce Latitude and Longitude bytes for adverstise in ble - java
byte[] dammidatibyteridotti(double lat, double lon, double pre) {
byte[] ritorno;
String _latitudine = String.format(Locale.ENGLISH,"%.5f", lat);
if(_latitudine.length() < 8) { _latitudine = "0" + _latitudine; }
String _longitudine = String.format(Locale.ENGLISH,"%.5f", lon);
if(_longitudine.length() < 8) { _longitudine = "0" + _longitudine; }
String _precisione = String.format(Locale.ENGLISH, "%d", (int) pre);
@massimilianochiodi
massimilianochiodi / concatenatebytearray.java
Created April 15, 2022 08:36
Concatenate X byte array onto one
byte[] concat(byte[]...arrays)
{
// Determine the length of the result array
int totalLength = 0;
for (byte[] array : arrays) {
totalLength += array.length;
}
// create the result array
byte[] result = new byte[totalLength];
@massimilianochiodi
massimilianochiodi / calculatebledistance.java
Created April 15, 2022 08:37
Calculate Ble device distance with RSSI + TXPowerLevel
private static double calculateDistance(float txPower, double rssi) {
if (rssi == 0) {
return -1.0; // if we cannot determine distance, return -1.
}
double ratio = rssi * 1.0 / txPower;
if (ratio < 1.0) {
return Math.pow(ratio, 10);
@massimilianochiodi
massimilianochiodi / bytearray.java
Created April 15, 2022 08:38
Byte Array to hex string and back
public static String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder(a.length * 2);
for(byte b: a)
sb.append(String.format("%02x", b));
return sb.toString();
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len/2];
@massimilianochiodi
massimilianochiodi / wordwrap.java
Created April 15, 2022 08:39
Word Wrap Text ( java ) add /n to text at specific size
private String wordwrap(String testo, int dimensione ) {
String temp_testo;
temp_testo = Html.fromHtml(testo, Html.FROM_HTML_MODE_LEGACY).toString();
StringBuilder sb = new StringBuilder(temp_testo);
int i = 0;
while (i + dimensione < sb.length() && (i = sb.lastIndexOf(" ", i + dimensione)) != -1) {
sb.replace(i, i + 1, "\n");
}
return sb.toString();
@massimilianochiodi
massimilianochiodi / createbitmapiconwithtext.java
Created April 15, 2022 08:40
Sample create Bitmap icon with text (Java)
private Bitmap icogen(int tipo, Context context) {
String testo = "";
IconGenerator icg = new IconGenerator( context );
icg.setBackground(ContextCompat.getDrawable(context, R.drawable.amu_bubble_mask ) );
if (tipo == 1) {
icg.setColor( R.color.nero );
icg.setStyle( IconGenerator.STYLE_GREEN );
testo = "Varco";
} else if (tipo == 2) {
icg.setColor( R.color.bianco );
@massimilianochiodi
massimilianochiodi / verifyifrooted.java
Created April 15, 2022 08:41
Simple verify if phone is rooted ( java )
public static boolean findBinary(String binaryName) {
boolean found = false;
String[] places = { "/sbin/", "/system/bin/", "/system/xbin/",
"/data/local/xbin/", "/data/local/bin/",
"/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/" };
for (String where : places) {
if (new File(where + binaryName).exists()) {
found = true;
break;
@massimilianochiodi
massimilianochiodi / verifyifreachable.java
Created April 15, 2022 08:43
Verify if internet address is reachable
public boolean AddressReachable(String hostUrl) {
try {
URL url = new URL(hostUrl);
final HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Android Application");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(10 * 1000);
urlc.connect();
// Fix per connessione con certificato
if (urlc.getResponseCode() == 200 || urlc.getResponseCode() == 403) {
@massimilianochiodi
massimilianochiodi / fragmentservice.java
Created April 15, 2022 08:44
Simple class for fragment service
public class FragmentService {
private final FragmentManager _fragmentManager;
public FragmentService(FragmentManager fragmentManager) {
_fragmentManager = fragmentManager;
}
public void loadFragment(int id, Fragment fragment, String name){
FragmentTransaction fragmentTransaction = _fragmentManager.beginTransaction();
@massimilianochiodi
massimilianochiodi / formatmacaddress.java
Created April 15, 2022 08:46
Format mac address on EditText ( java )
txtmacmacaddress.addTextChangedListener(new TextWatcher() {
String mPreviousMac = null;
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}