2016-04-03 13 views
0

近くのビーコンに及ぶことができるアンドロイドアプリケーションを構築しようとしており、ビーコンからのUUIDや距離などの関連情報を取得しようとしています。私が今直面している問題は、レンジング機能が単一のビーコンを全く検出できないことです。同じデバイス上でビーコンをかなりうまくスキャンできる別のデモプロジェクトをダウンロードしたので、関数が正しいと私は確信しています。ALTビーコンレンジング機能が動作しない

アプリケーションは現在beaconManager.startRangingBeaconsInRegion(新しい地域(「myRangingUniqueId」、NULL、NULL、NULL))関数の後にまったく反応がない、とデバッガはdidRangeBeaconsInRegion機能で立ち往生スレッドを示し、ビーコンのサイズは常にあります0

私のコードに何か問題がありますか?それとも、私の設定や構成が正しくないからですか?

コード:

package com.example.ma.contextualawarenessapplication; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

import android.app.Activity; 
import android.os.RemoteException; 
import android.widget.TextView; 

import org.altbeacon.beacon.Beacon; 
import org.altbeacon.beacon.BeaconConsumer; 
import org.altbeacon.beacon.BeaconManager; 
import org.altbeacon.beacon.BeaconParser; 
import org.altbeacon.beacon.RangeNotifier; 
import org.altbeacon.beacon.Region; 

import java.text.SimpleDateFormat; 
import java.util.Collection; 
import java.util.Date; 
import java.util.Locale; 

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

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_geofencing); 

    beaconManager = BeaconManager.getInstanceForApplication(this); 
    beaconManager.bind(this); 

} 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    beaconManager.unbind(this); 
} 
@Override 
protected void onPause() { 
    super.onPause(); 
    if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(true); 
} 
@Override 
protected void onResume() { 
    super.onResume(); 
    if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false); 
} 

@Override 
public void onBeaconServiceConnect() { 
    beaconManager.setRangeNotifier(new RangeNotifier() { 
     @Override 
     public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
      for (Beacon beacon : beacons) { 
       logToDisplay(getCurrentTimeStamp() + " | Beacon " + beacon.toString() + " is about " + beacon.getDistance() + " meters away."); 
      } 
     /*if (beacons.size() > 0) { 
      EditText editText = (EditText)GeofencingActivity.this 
        .findViewById(R.id.geofencingText); 
      Beacon firstBeacon = beacons.iterator().next(); 
      logToDisplay("The first beacon "+firstBeacon.toString()+" is about "+firstBeacon.getDistance()+" meters away.");   } 
    */} 

    }); 

    try { 
     beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null)); 
    } catch (RemoteException e) { } 
} 
private void logToDisplay(final String line) { 
    runOnUiThread(new Runnable() { 
     public void run() { 
      TextView editText = (TextView)GeofencingActivity.this 
        .findViewById(R.id.geofencingText); 
      editText.append(line+"\n"); 
     } 
    }); 
} 

private static String getCurrentTimeStamp() { 
    Locale locale = new Locale("es", "ES"); 
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS", locale); 
    Date now = new Date(); 
    return sdf.format(now); 
} 
} 

build.gradle:

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 23 
buildToolsVersion "23.0.2" 

defaultConfig { 
    applicationId "com.example.ma.contextualawarenessapplication" 
    minSdkVersion 18 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'),  'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:23.1.1' 
compile 'org.altbeacon:android-beacon-library:[email protected]' 
} 

buildscript { 
repositories { 
    jcenter() 
    mavenCentral() 
    flatDir { 
     dirs 'libs' 
    } 
} 
dependencies { 
    classpath 'com.android.tools.build:gradle:2.1.0-alpha5' 

    // NOTE: Do not place your application dependencies here; they belong 
    // in the individual module build.gradle files 
} 
} 

allprojects { 
repositories { 
    jcenter() 
} 
} 

task clean(type: Delete) { 
delete rootProject.buildDir 
} 

答えて

0

あなたはAndroid Beacon Libraryで検出を得ることができないときチェックするためにいくつかのこと:

  • あなたのビーコンがあることを確認しますAltBeacon形式を送信します。そうでない場合は、BeaconParser式を登録するだけで、EddystoneやiBeaconのような独自のビーコンタイプを検出できます。

  • アドバタイズされた形式が不明な場合は、検出する形式を示すLocate appを試してみてください。

  • 問題を検出するデバイスにAndroid 6.0以降が搭載されている場合は、hereに記載されているように、場所のアクセス権を取得していることを確認してください。

+0

ターゲットsdkを21に変更して、場所のアクセス許可を追加した後、ついにそれが動作しました。あなたは私の週末を救った、ありがとう! –

関連する問題