Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Last active December 8, 2021 13:28
Show Gist options
  • Save ToeJamson/635b14892364af30e3ad to your computer and use it in GitHub Desktop.
Save ToeJamson/635b14892364af30e3ad to your computer and use it in GitHub Desktop.
Android Beacon Tutorial (Building the Detector - Listener - Observer)
private void setScanFilter() {
ScanFilter.Builder mBuilder = new ScanFilter.Builder();
ByteBuffer mManufacturerData = ByteBuffer.allocate(23);
ByteBuffer mManufacturerDataMask = ByteBuffer.allocate(24);
byte[] uuid = getIdAsByte(UUID.fromString("0CF052C297CA407C84F8B62AAC4E9020");
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());
mScanFilter = mBuilder.build();
}
public void getAdOfTheDay(int major, int minor) {
String channel = "YourCompany_"+major+"_"+minor;
try {
pubnub.subscribe(channel, mPubNubCallback);
} catch (PubnubException e) {
System.out.println(e.toString());
}
}
protected Callback mPubNubCallback = new Callback() {
@Override
public void successCallback(String channel, Object message) {
System.out.println(message.toString());
}
};
private void setScanSettings() {
ScanSettings.Builder mBuilder = new ScanSettings.Builder();
mBuilder.setReportDelay(0);
mBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
mScanSettings = mBuilder.build();
}
mBluetoothLeScanner.startScan(Arrays.asList(mScanFilter), mScanSettings, mScanCallback);
protected ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
ScanRecord mScanRecord = result.getScanRecord();
int[] manufacturerData = mScanRecord.getManufacturerSpecificData(224);
int mRssi = result.getRssi();
}
}
public double calculateDistance(int txPower, double rssi) {
if (rssi == 0) {
return -1.0; // if we cannot determine accuracy, return -1.
}
double ratio = rssi*1.0/txPower;
if (ratio < 1.0) {
return Math.pow(ratio,10);
}
else {
double accuracy = (0.89976)*Math.pow(ratio,7.7095) + 0.111;
return accuracy;
}
}
private String getDistance(accuracy) {
if (accuracy == -1.0) {
return "Unknown";
} else if (accuracy < 1) {
return "Immediate";
} else if (accuracy < 3) {
return "Near";
} else {
return "Far";
}
}
<uses-permission android:name="android.permission.INTERNET"/>
this.pubnub = new Pubnub("publish_key", "subscribe_key");
@Override
public void onScanResult(int callbackType, ScanResult result) {
[...]
if ((mBeaconScanRecord.getDistance() == "Immediate") && !isSubscribed()) {
getAdOfTheDay(mIBeaconScanRecord.getMajor(), mIBeaconScanRecord.getMinor());
}
}
@ohera
Copy link

ohera commented Jan 28, 2016

Hi,
I have a number of errors with the above codes and the errors are mostly as "identifier not found".
Kindly help on what should be corrected

@Azhar0790
Copy link

How to implement this? I done with pubnub sign up.. but not getting further process properly

@TotalDay
Copy link

add this

package com.my.myny.beacon;

import android.bluetooth.BluetoothAdapter;
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.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.UUID;

@TotalDay
Copy link

private BluetoothLeScanner mBluetoothLeScanner;
private BluetoothAdapter mBluetoothAdapter;
private ScanFilter mScanFilter;
private ScanSettings mScanSettings;

@Override
protected void onCreate(Bundle savedInstanceState) {

@TotalDay
Copy link

@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    mBluetoothLeScanner.startScan(Arrays.asList(mScanFilter), mScanSettings, mScanCallback);

    mBluetoothLeScanner.startScan(Arrays.asList(mScanFilter), mScanSettings, mScanCallback);

}

private void setScanFilter() {
    ScanFilter.Builder mBuilder = new ScanFilter.Builder();
    ByteBuffer mManufacturerData = ByteBuffer.allocate(23);
    ByteBuffer mManufacturerDataMask = ByteBuffer.allocate(24);
    byte[] uuid = getIdAsByte(UUID.fromString("0CF052C297CA407C84F8B62AAC4E9020"));
    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());
    mScanFilter = mBuilder.build();
}

@TotalDay
Copy link

public static byte[] getIdAsByte(java.util.UUID uuid)
{
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
}

@TotalDay
Copy link

вцелом спасибо, перехожу с AS3 на JAVA в Андроид Студии ускоренными темпами, вот думаю уже купить готовую библиотеку за 25$ и не ебать себе мозги

@shvyrev
Copy link

shvyrev commented Apr 4, 2016

Что за библиотека?

@troiallday
Copy link

#newbieAlert there seems to be something wrong with the onScanResult method ??? keeps saying cannot resolve method 'getDistance' , 'isSubscribed', 'getMajor' and 'getMinor' ...is there something that i left out
screenshot 195

@Rollpopptery
Copy link

Rollpopptery commented Mar 4, 2017

Yes, I had this issue:

Cannot resolve method getDistance()
didRangeBeaconsInRegion(Collection beacons, Region region)

// Should be
didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region)

The example code is wrong, or the compiler they used auto-magically worked out that beacon was in a collection of Beacons.

@DragonPigGQ
Copy link

Anybody can share the full code? the sample codes have a lot of errors and I cannot resolve method getDistance(), where did you even find this line of code didRangeBeaconsInRegion(Collection beacons, Region region) ?

@marriusco
Copy link

Does anyone figured out the missing mess.

@huygao
Copy link

huygao commented Dec 8, 2021

@ToeJamson
After a long time, can you share the source code for everyone to refer to?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment