How to use nearby service to develop accurate advertising push function for nearby people


When you want to find a restaurant for dinner, but you don’t know which one to go to, a notice will pop up on your mobile phone and automatically push the information of the nearby high-quality restaurants for you. Will you click to check it? When you are still struggling in the store about whether to buy a pair of shoes, the mobile app gives you a 50% discount coupon for regular customers. Do you have any reason to refuse such an advertisement?

This kind of advertisement not only won’t cause the user‘s boredom, but also satisfied the user’s demand. More accurate advertising push, reduce unnecessary interruption to users, improve user satisfaction with the application. So how can I add a precise advertising push function for nearby people to my app?

How to use nearby service to develop accurate advertising push function for nearby people

It can access Huawei’s short distance communication service and realize it through Bluetooth beacon message When beacon is deployed in shopping malls, beacon messages provide accurate relative location information. When users approach restaurants and stores, they will receive pre configured promotional messages, such as coupons, discount information, and so on, so as to promote the store conveniently. The following figure shows the function.

How to use nearby service to develop accurate advertising push function for nearby people

   if you are interested in the implementation, you can download the source code from GitHub:…

1. Development preparation

If you are already a Huawei developer, you can omit this step. If you have no previous experience in integrating Huawei mobile services, you need to configure appgallery connect first, open up the short-range communication service and integrate the HMS SDK. Please refer to the official documents for the relevant steps.

1.1 add Huawei Maven warehouse and AGCP configuration in project level gradle

Add the following Maven address and AGCP configuration incrementally. Do not modify the following but copy.

buildscript {
     repositories {
         maven { url '' }
     dependencies {
         classpath ''
         classpath 'com.huawei.agconnect:agcp:'
 allprojects {
     repositories {
         maven { url '' }

1.2 at the application level build.gradle Add SDK dependency

To introduce the short range communication service SDK, the most important points are as follows com.huawei SDK at the beginning.

dependencies {
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation 'androidx.appcompat:appcompat:1.1.0'
     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     implementation "com.huawei.hmf:tasks:"
     implementation "com.huawei.hms:network-grs:"
     implementation 'com.huawei.agconnect:agconnect-core:'
     implementation 'com.huawei.hms:nearby:'
     api ''

1.3 in AndroidManifest.xml File application network permissions, Bluetooth permissions and location permissions

The following permissions are shown in the name, such as android.permission.INTERNET You need network permissions, android.permission.BLUETOOTH You need Bluetooth rights. The following permissions are required.

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2. Code development

2.1 initialization and dynamic permission application

Oncreate is a method that will be called when an activity is created. In this method, we can do some preparatory actions, such as applying for necessary permissions, and checking whether mobile network, Bluetooth and GPS are enabled.

@RequiresApi(api = Build.VERSION_CODES.P)
 protected void onCreate(Bundle savedInstanceState) {
     Log.i(TAG, "onCreate");
     boolean isSuccess = requestPermissions(this, this);
     if (!isSuccess) {
     Log.i(TAG, "requestPermissions success");
     if (!NetCheckUtil.isNetworkAvailable(this)) {
     if (!BluetoothCheckUtil.isBlueEnabled()) {
     if (!GpsCheckUtil.isGpsEnabled(this)) {

  Register to monitor. When it is detected that the Bluetooth, GPS and network of the mobile phone are not connected, a prompt will be given:The following is the alert dialog component in Android to prompt.

private void showWarnDialog(String content) {
     DialogInterface.OnClickListener onClickListener =
         new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setNegativeButton(getText(R.string.btn_confirm), onClickListener);;

2.2 process of receiving beacon message

The following startscanning method is called in the OnStart method to start Bluetooth scanning. In the following message handler object, four callback methods are encapsulated (onfound indicates that the beacon message has been obtained, onlost means that the message is lost, ondistancechanged indicates the distance change between beacon and the mobile phone, and onblesignalchanged indicates that the signal change of beacon is detected (2).

The most important method is the following doonfound method, which represents the processing method of the client when receiving the beacon message, which can be displayed individually.

private void doOnFound(Message message) {
     if (message == null) {
     String type = message.getType();
     if (type == null) {
     String messageContent = new String(message.getContent());
     Log.d(TAG, "New Message:" + messageContent + " type:" + type);
     if (type.equalsIgnoreCase(Constant.CANTEEN)) {
     } else if (type.equalsIgnoreCase(Constant.NOTICE)) {

Custom display:
   the following method is just a simple demonstration of one of the message processing methods, including the notification bar banner notification on the client, page font sliding display and other operations.

private void operateOnFoundCanteen(String messageContent) {
     CanteenAdapterInfo canteenAdapterInfo =
             (CanteenAdapterInfo) JsonUtils.json2Object(messageContent, CanteenAdapterInfo.class);
     if (canteenAdapterInfo == null) {
     String canteenName = canteenAdapterInfo.getCanteenName();
     if (canteenName == null) {
     Log.d(TAG, "canteenName:" + canteenName);
     if (!canteenNameList.contains(canteenName)) {
     String notice = "";
     if (receivedNoticeMap.containsKey(canteenName)) {
         notice = receivedNoticeMap.get(canteenName);
     int canteenImage = getCanteenImage(canteenName);
     int requestCode = getRequestCode(canteenName);
     canteenAdapterInfoMap.put(canteenName, canteenAdapterInfo);
     sendNotification(Constant.NOTIFICATION_TITLE, Constant.NOTIFICATION_SUBTITLE, canteenName, requestCode);
         new Runnable() {
             public void run() {


In this demo, the Bluetooth beacon message subscription function of Huawei nearby service is used

Based on the ability of nearby beacon message, it can not only be used for advertising push, but also realize the following related functions:

  1. After integrating the nearby beacon message function, the car life application can identify the user approaching the car, notify the app to enable keyless entry, record the driving track, etc.
  2. The office application integrates the nearby beacon message function, which can quickly and accurately record the employees’ clock in position.
  3. After tourism and Exhibition applications integrate the nearby beacon message function, users can get the corresponding introduction information when they are close to an exhibit or cultural relic.
  4. Game applications use nearby beacon message to realize the interaction between game and real scene. Trigger game levels through real objects, or give game props to players who participate in offline activities.

For more detailed development guidelines, please refer to the official website of Huawei developer alliance

Past links: super simple integration of ML kit to broadcast dictation words
Link to the original text:
Original author: Zhao Zhao

Recommended Today

CentOS install Chinese Fonts

preface When we encounter some Chinese display garbled problems, it may be because the server does not install Chinese fonts install 1. View system fontsBefore starting the installation, we first check the installed fonts in the system. To view the installed fonts in the system, we can use thefc-listIf there is no command in the […]