Notes
Notes - notes.io |
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.StringReader;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.UUID;
public class miniax extends Activity {
final BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
public ScanFilter scanFilter;
public ScanSettings scanSettings;
public static String UUId = "E20A39F4-73F5-4BC4-A12F-17D1AD07A961";
public static String UUIdDev = "";
public static String MajorID = "";
public static String MinorID = "";
public int TXPow;
public int RSSI;
public BluetoothDevice MAC;
public double Distance;
private ProgressBar progressBar;
public Context context = this;
private class ScanBeacon extends AsyncTask {
@Override
protected Object doInBackground(Object[] params) {
scanFilter=setScanFilter(scanFilter);
scanSettings=setScanSettings(scanSettings);
bluetoothLeScanner.startScan(Arrays.asList(scanFilter), scanSettings, scanCallback);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Object Obj) {
progressBar.setVisibility(View.GONE);
TextView JSONObjHead = (TextView) findViewById(R.id.UUIDval);
JSONObjHead.setText(UUIdDev);
RequestQueue queue = Volley.newRequestQueue(context);
if(UUId.equals(UUIdDev)) {
StringRequest request = new StringRequest(Request.Method.GET,"http://192.168.1.11:8080/"+UUIdDev,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String jsonString=response;
try {
JSONObject reader=new JSONObject(jsonString);
JSONArray dataArray = reader.getJSONArray("data");
JSONObject obj = dataArray.getJSONObject(0);
String DevID = obj.optString("_id").toString();
String Detail = obj.optString("detail").toString();
String Tag =obj.optString("tag").toString();
String Title = obj.optString("title").toString();
TextView JSONObjHead = (TextView) findViewById(R.id.JSONObjHead);
JSONObjHead.setText(Title);
TextView JSONObjDetail = (TextView) findViewById(R.id.JSONObjDetail);
JSONObjDetail.setText(Detail);
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_miniax);
bluetoothAdapter.enable();
progressBar = (ProgressBar)findViewById(R.id.loading);
progressBar.setVisibility(View.VISIBLE);
ScanBeacon BeacObj = new ScanBeacon();
BeacObj.execute();
}
protected ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
ScanRecord mScanRecord = result.getScanRecord();
byte[] manufacturerData = mScanRecord.getManufacturerSpecificData(224);
UUIdDev = GetUUID(manufacturerData);
MajorID = GetMajor(manufacturerData);
MinorID =GetMinor(manufacturerData);
TXPow = mScanRecord.getTxPowerLevel();
RSSI = result.getRssi();
MAC = result.getDevice();
Distance = (int)CalculateDistance(TXPow, RSSI);
}
};
private ScanFilter setScanFilter(ScanFilter scanFilter) {
ScanFilter.Builder mBuilder = new ScanFilter.Builder();
ByteBuffer mManufacturerData = ByteBuffer.allocate(23);
ByteBuffer mManufacturerDataMask = ByteBuffer.allocate(24);
byte[] uuid = getIdAsByte(UUID.fromString(UUId));
mManufacturerData.put(0, (byte)0xBE);
mManufacturerData.put(1, (byte)0xAC);
for (int i=2; i<=17; i++) { mManufacturerData.put(i, uuid[i-2]); }
for (int i=0; i<=17; i++) { mManufacturerDataMask.put((byte)0x01); }
mBuilder.setManufacturerData(224, mManufacturerData.array(), mManufacturerDataMask.array());
scanFilter = mBuilder.build();
return scanFilter;
};
private ScanSettings setScanSettings(ScanSettings scanSettings) {
ScanSettings.Builder mBuilder = new ScanSettings.Builder();
mBuilder.setReportDelay(0);
mBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
scanSettings = mBuilder.build();
return scanSettings;
};
private String IntTOHex(int num){
if(num<0){ return Integer.toHexString(num+256); }
return Integer.toHexString(num);
};
private byte[] getIdAsByte(UUID UUId) {
ByteBuffer BB = ByteBuffer.wrap(new byte[16]);
BB.putLong(UUId.getMostSignificantBits());
BB.putLong(UUId.getLeastSignificantBits());
return BB.array();
};
private String GetMajor(byte[] manufacturerData){
String MajorID = "";
for (int i=18; i<20;i++){
String Tmp = IntTOHex(manufacturerData[i]);
if(Tmp.length()==1){ MajorID += "0" + Tmp; }
else { MajorID +=Tmp; }
}
return MajorID.toUpperCase();
};
private String GetMinor(byte[] manufacturerData){
String MinorID = "";
for (int i=20; i<22;i++){
String Tmp = IntTOHex(manufacturerData[i]);
if(Tmp.length()==1){ MinorID += "0" + Tmp; }
else { MinorID +=Tmp; }
}
return MinorID.toUpperCase();
};
private String GetUUID(byte[] manufacturerData){
String UUId = "";
for (int i=2; i<18;i++){
String Tmp = IntTOHex(manufacturerData[i]);
if(Tmp.length()==1){ UUId += "0" + Tmp; }
else { UUId += Tmp; }
if(i==5){ UUId+='-'; }
if(i==7){ UUId+='-'; }
if(i==9){ UUId+='-'; }
if(i==11){ UUId+='-'; }
}
return UUId.toUpperCase();
};
public double CalculateDistance(int txPower, double RSSI) {
if (RSSI == 0) { return -1.0; }
double ratio = RSSI*1.0/txPower;
if (ratio < 1.0) { return Math.pow(ratio,10); }
else { return ((0.89976)*Math.pow(ratio,7.7095) + 0.111); }
};
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team