This article is not perfect, please do not reprint, thank you. When the Demo project is complete, it will be uploaded to my github
package com.bipbip.ble;import java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.map;import java.util.set;import android.util.log;/** * * @author Administrator */public class BlePreventLostCore { Private static string tag=blepreventlostcore.class.getsimplename (); private final static double deviatebaseflag=0.2; // private final static double wcf=0.5; //weight compensation flag, weight compensation factor private static Map<String,List<Integer>> sacnedbledevicesdata; /** * Remove the dirty value from the data collected * deviateBaseFlag the dirty offset flag for the Datum , if the length is relatively large, can be suitably increased * Simple with arithmetic average as reference, greater than average DEVIATEBASEFLAThe g*average is judged to be a dirty value. * * */ public static void cleardigest () {// double deviateFlag=0.2**......&&N; // Map<String,Double> sAverageSSID=new HashMap<> (); //simple arithmetic average set<string> ks =sacnedbledevicesdata.keyset (); //the arithmetic average of the watch. for (string key : ks ) { double average=0.0; list<integer> ssidlist=sacnedbledevicesdata.get (key); int size=sacnedbledevicesdata.get (key). Size (); for (int i=0;i<size;i++) { average=average+ssidlist.get (i); } average= ( Double) Average/size; saveragessid.put (key, average); } //Remove the dirty value. for(string key : ks ) { double average=0.0; list<integer> Ssidlist=sacnedbledevicesdata.get (key); int size=sacnedbledevicesdata.get (key). Size (); double averagetemp=saveragessid.get (key); for (int i=0;i<size;i + +) {// average=average+ssidlist.get (i); if (Math.abs (ssidList.get (i)-averagetemp) > deviatebaseflag*averagetemp) { Ssidlist.remove (i); } } } } /** * ssid distribution of weights to use [... , ...] * * @return */ public static map<string,list <double>> getweghtcompensationmap () { map<string,list<double>> Weghtcompensationdatas = &nBsp;new hashmap<string,list<double>> (); //weighting distribution// map<string,double> weghtcompensationdecreasing; //weight decrement factor //1. Initialize weights distribution Set<String> ks =sacnedbledevicesdata.keyset (); for (string key : ks ) { final int size=sacnedbledevicesdata.get (key). Size (); list<double> weghtCompDataList=new ArrayList<Double> (size-1) //1. Initialization of the first half of the weight distribution for (int i=0;i<size;i++) { double decreasing=wcf-wcf*2* (i+1)/size; if ( DECREASING>0&&I<SIZE/2) { weghtcompdatalist.add (i, decreasing); }else{ weghtcompdatalist.add (i, (double) 0); } &nbSP;} //2. The second half of the weight distribution initializes the for (int j=size-1;j>size/2;j--) { double temp=weghtcompdatalist.get (size-j-1); Weghtcompdatalist.set (j,temp); } //3. Weight distribution actual processing for ( int i=0;i<size/2;i++) { weghtcompdatalist.set (i, (1-weghtCompDataList.get (i )) (/size); } for (int j=size-1;j>size/2;j--) { Weghtcompdatalist.set (j, (1+weghtcompdatalist.get (j))/size); } //4. The median of the weighted distribution is actually processed if (size%2==0) { Weghtcompdatalist.set (size/2,1.0/size); }else{ weghtcompdatalist.set ( size/2, 1.0/size); weghtcompdatalist.set (size/2-1, 1.0/size); &NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//5. Weight pointsCloth test, the addition should be infinitely close to 100/100=1 double test=0; for (int s=0;s<size;s++) { test=test+weghtcompdatalist.get (s); } weghtcompensationdatas.put (key, weghtcompdatalist); }//Weight distribution complete return weghtCompensationDatas; } /** * time, the greater the weight * * @param sacnedBleDD * @return */ Public static map<string,double> getdevicestate (Final map<string,list<integer >> sacnedbledd) { sacnedbledevicesdata=sacnedbledd; cleardigest (); map<string,list<double>> weightcompensation=getweghtcompensationmap (); //weighting distribution factor &NBSP;&NBSP;&NBSP;LOG.E (TAG, " " +weightcompensation); map<strinG,double> averagessid=new hashmap<> (); set<string> ks = Sacnedbledevicesdata.keyset (); for (string key : ks ) { double average=0.0; list<double> weghtcompdatalist=weightcompensation.get (key); list<integer> ssidlist=sacnedbledevicesdata.get (key); for (int i=0;i<weghtcompdatalist.size (); i++) { average=average+weghtcompdatalist.get (i) * Ssidlist.get (i); } averagessid.put (Key, average); //bingo. } return averagessid; }}
Android Bluetooth anti-loss (ii)