-
-
Save ToeJamson/635b14892364af30e3ad to your computer and use it in GitHub Desktop.
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()); | |
} | |
} |
private BluetoothLeScanner mBluetoothLeScanner;
private BluetoothAdapter mBluetoothAdapter;
private ScanFilter mScanFilter;
private ScanSettings mScanSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
@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();
}
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();
}
вцелом спасибо, перехожу с AS3 на JAVA в Андроид Студии ускоренными темпами, вот думаю уже купить готовую библиотеку за 25$ и не ебать себе мозги
Что за библиотека?
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.
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) ?
Does anyone figured out the missing mess.
@ToeJamson
After a long time, can you share the source code for everyone to refer to?
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;