簡體   English   中英

Android-在BLE設備附近獲得通知

[英]Android - Get notified when near BLE device

當它在特定BLE設備的范圍內時,我需要通知我的應用程序。 進入范圍后,我想連接到該設備並向其發送數據。

方法1:
定期BLE掃描以一定間隔(即每30秒)進行。 但是,這對電池不利。 通過僅在設備位置靠近BLE設備的位置時進行掃描(通過Android Geofence API),我可以對此進行一些改進。 但是,對於電池來說並不是很好。

方法二:
使用Android Awareness API。 這似乎是一個很好的候選人。 但是,您似乎被迫向Google注冊BLE設備作為BLE信標。 有人知道這是否是絕對要求嗎? 我不想在Google上注冊。

看看Android Altbeacon庫。 它可以利用省電功能滿足您的需求。

關於方法1,來自下面庫的示例代碼。

public class MonitoringActivity extends Activity implements BeaconConsumer {
    protected static final String TAG = "MonitoringActivity";
    private BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranging);
                beaconManager = BeaconManager.getInstanceForApplication(this);
        // 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.
        // beaconManager.getBeaconParsers().add(new BeaconParser().
        //        setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        beaconManager.bind(this);
    }
    @Override 
    protected void onDestroy() {
        super.onDestroy();
        beaconManager.unbind(this);
    }
    @Override
    public void onBeaconServiceConnect() {
        beaconManager.removeAllMonitorNotifiers();
        beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
            Log.i(TAG, "I just saw an beacon for the first time!");        
        }

        @Override
        public void didExitRegion(Region region) {
            Log.i(TAG, "I no longer see an beacon");
        }

        @Override
            public void didDetermineStateForRegion(int state, Region region) {
            Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);        
            }
        });

        try {
            beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
        } catch (RemoteException e) {    }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM