As before, I work with Android Beacon Library,
It already worked and I can found out beacon via BLE - Bluetooth low energy,
But now, after updated to latest version of library, now method onBeaconServiceConnect()
not run anymore.
Please tell me what I need to do to make it works,
Thank you,
p/s : Code :
Manifest.xml
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<service
android:name="org.altbeacon.beacon.service.BeaconService"
android:enabled="true"
android:isolatedProcess="false"
android:label="beacon" />
<service
android:name="org.altbeacon.beacon.BeaconIntentProcessor"
android:enabled="true" />
Java
public class FoundBeaconFragment extends Fragment
implements BeaconConsumer {
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return false;
}
@Override
public Context getApplicationContext() {
return getActivity();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_found_beacon, null);
// Set adapter
foundBeaconAdapter = new FoundBeaconAdapter(
getActivity(),
R.layout.simple_list_item_found_beacon,
mAlFoundBeacon);
mLvFoundBeacon.setAdapter(foundBeaconAdapter);
// Register Scan beacons feature
register();
return v;
}
@Override
public void onDestroy() {
super.onDestroy();
try {
// Unbind scan beacon progress
if (beaconManager != null)
beaconManager.unbind(this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
}
// CUSTOM METHODS
private void register() {
beaconManager = BeaconManager.getInstanceForApplication(getActivity());
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
try {
// todo
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
} catch (Exception e) {
e.printStackTrace();
}
// CAN SEE THIS LOG CAT, NO EXCEPTION
Log.i("", "Register Service");
beaconManager.bind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(
Collection<org.altbeacon.beacon.Beacon> beacons, Region region) {
Log.i("", "IS_SCAN_BEACON " + FoundBeaconFragment.IS_SCAN_BEACON);
if (FoundBeaconFragment.IS_SCAN_BEACON) {
Log.i("", "Found " + beacons.size() + " beacon!");
if (beacons.size() > 0) {
/**
* Begin transfer data
*/
for (final org.altbeacon.beacon.Beacon beacon : beacons) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
getDataViaBLE(getActivity(), beacon.getId1() + "",
beacon.getId2() + "", beacon.getId3() + "");
}
});
}
}
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(
new Region(Constant.UUID, null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
public static String UUID = "01122334-4556-6778-899a-abbccddeeff0";
ANSWER
Since I used Fragment
not Activity
as sample codes from the library.
So I need do these changes :
@Override
public Context getApplicationContext() {
return getActivity().getApplicationContext();
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
getActivity().unbindService(serviceConnection);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return getActivity().bindService(intent, serviceConnection, i);
}