Skip to content

Commit

Permalink
BleService for Android: Add required permission (#204)
Browse files Browse the repository at this point in the history
* Add required permission

* Pass proximity value

* Make debug static

* Remove static from Ble methods

Co-authored-by: jose.pereda <[email protected]>
  • Loading branch information
José Pereda and jperedadnr authored Sep 11, 2020
1 parent e01ead9 commit 7b89bc4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
28 changes: 17 additions & 11 deletions modules/ble/src/main/native/android/dalvik/DalvikBleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ private ScanCallback createCallback() {
ScanCallback answer = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
Log.v(TAG, "BLESERVICE: onScanResult, callbacktype = " + callbackType);
if (debug) {
Log.v(TAG, "BLESERVICE: onScanResult, callbacktype = " + callbackType);
}
ScanRecord mScanRecord = result.getScanRecord();
byte[] scanRecord = mScanRecord.getBytes();

Expand Down Expand Up @@ -236,13 +238,15 @@ private void processAD(byte[] scanRecord, int init, int mRssi) {
power -= 256;
int proximity = calculateProximity(power, mRssi);

Log.v(TAG, "Scan: mID: "+mID+", beaconID: "+beaconID+", uuid: "+scannedUuid+
", major: "+major+", minor: "+minor+", power: "+power+", distance: "+proximity);
scanDetected (scannedUuid, major, minor, power, 0);
if (debug) {
Log.v(TAG, "Scan: mID: " + mID + ", beaconID: " + beaconID + ", uuid: " + scannedUuid +
", major: " + major + ", minor: " + minor + ", power: " + power + ", distance: " + proximity);
}
scanDetected (scannedUuid, major, minor, power, proximity);
}
}

private static String ByteArrayToUUIDString(byte[] ba) {
private String ByteArrayToUUIDString(byte[] ba) {
StringBuilder hex = new StringBuilder();
for (byte b : ba) {
hex.append(new Formatter().format("%02x", b));
Expand All @@ -251,9 +255,11 @@ private static String ByteArrayToUUIDString(byte[] ba) {
"$1-$2-$3-$4-$5" );
}

private static int calculateProximity (int txPower, double rssi) {
private int calculateProximity (int txPower, double rssi) {
double accuracy = calculateAccuracy(txPower, rssi);
Log.v(TAG, "accuracy = "+accuracy+", power = "+txPower+", rssi = "+rssi);
if (debug) {
Log.v(TAG, "accuracy = "+accuracy+", power = "+txPower+", rssi = "+rssi);
}
if (accuracy < 0) {
return 0;
}
Expand All @@ -266,7 +272,7 @@ private static int calculateProximity (int txPower, double rssi) {
return 3;
}

private static double calculateAccuracy(int txPower, double rssi) {
private double calculateAccuracy(int txPower, double rssi) {
if (rssi == 0 || txPower == 0) {
return -1.0; // if we cannot determine accuracy, return -1.
}
Expand All @@ -280,7 +286,7 @@ private static double calculateAccuracy(int txPower, double rssi) {
}
}

private static byte[] getPayload(String uuid, int major, int minor) {
private byte[] getPayload(String uuid, int major, int minor) {
byte[] prefixArray = getBytesFromShort((short) 533);
byte[] uuidArray = getBytesFromUUID(uuid);
byte[] majorArray = getBytesFromShort((short) major);
Expand All @@ -299,15 +305,15 @@ private static byte[] getPayload(String uuid, int major, int minor) {
return buff.array();
}

private static byte[] getBytesFromUUID(String uuidString) {
private byte[] getBytesFromUUID(String uuidString) {
final UUID uuid = UUID.fromString(uuidString);
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
buffer.putLong(uuid.getMostSignificantBits());
buffer.putLong(uuid.getLeastSignificantBits());
return buffer.array();
}

private static byte[] getBytesFromShort(short value) {
private byte[] getBytesFromShort(short value) {
ByteBuffer buffer = ByteBuffer.wrap(new byte[2]);
buffer.putShort(value);
return buffer.array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<uses-sdk android:minSdkVersion="21" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

0 comments on commit 7b89bc4

Please sign in to comment.