我无法在 Android 中使用 AltbeaconLibrary 检测(监控)Ibeacon
I cannot detect(monitoring) Ibeacon with using AltbeaconLibrary in Android
我有一个 android 应用程序。我的目的是使用 AltBeacon 库检测 Ibeacon。我使用了来自 AltBeacon 图书馆网站 Link: (http://altbeacon.github.io/android-beacon-library/samples.html) 的 监控示例代码 。但我无法检测到任何 Ibeacon。并且无法获取有关检测到任何信标的任何日志。
我的清单文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ibeaconscanner.ibeaconproject.ibeaconproject" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我在 MainActivity.java 中添加了 Requesting Permission 代码来解决问题,但无法解决。
我的 MainActivity.java 是这样的:
public class MainActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "MyApp";
private BeaconManager beaconManager;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location acces");
builder.setMessage("Please grat location acces so this app can detect beacons");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
}
}
setContentView(R.layout.activity_main);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.d(TAG, "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.d(TAG, "I no longer see an beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.d(TAG, "I have just switched from seeing/not seeing beacons: " + state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
} catch (RemoteException e) {
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "coarse location permission granted");
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
}
}
);
builder.show();
}
return;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
几点提示:
确保您可以使用现成的信标检测应用程序检测您的信标,例如 Locate. 如果您看不到信标,则可能是硬件问题。
默认情况下,库仅检测发送 AltBeacon 格式的信标。如果您使用专有信标类型,则需要调用 beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("**PUT LAYOUT EXPRESSION HERE**"));
为您的信标类型添加布局。
如果您使用的是Android6,请确保您的应用已授予位置权限。转到设置 -> 应用程序 -> [您的应用程序名称] -> 权限,并确保授予位置权限。
如果您使用的是 Android6,请检查“设置”->“位置”是否设置为“开”。这对于包括 Nexus 5 在内的某些设备是必需的。
如果上述方法没有帮助,请尝试向您的应用程序添加日志记录(如 onBeaconServiceConnect()
)并报告哪些方法被调用,哪些方法没有被调用。
要检测 iBeacon,您需要修改 altbeacon 库以使其理解 ibeacon 数据包格式。在 onCreate 函数中试试这个:
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.setForegroundScanPeriod(1000l);
beaconManager.setForegroundBetweenScanPeriod(10000l);
beaconManager.bind(this);
setBeaconLayout 定义检测 iBeacons 的数据包结构。可以详细了解包结构here.
setForegroundScanPeriod 在我的代码中设置为 1 秒,setForegroundBetweenScanPeriod 设置为 10 秒。您可以将其更改为您想要的任何值。您可能希望根据需要将 setForegroundBetweenScanPeriod 减少到 3 秒。基本上,这两个设置定义设备处于扫描模式 1 秒,并每 10 秒重复扫描一次。
更新
要显示检测到的信标的 UUID,请使用以下内容:
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
for (Beacon beacon : beacons) {
logToDisplay("Detected " + beacon.toString() + " with UUID " + beacon.getId1().toString() + " and Major ID " + beacon.getId2().toString() + " and Minor ID " + beacon.getId3().toString());
}
}
else {
logToDisplay("No iBeacons detected");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(myRegion);
} catch (RemoteException e) { }
}
private void logToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
EditText editText = (EditText)RangingActivity.this.findViewById(R.id.rangingText);
editText.append(line+"\n");
}
});
}
我有一个 android 应用程序。我的目的是使用 AltBeacon 库检测 Ibeacon。我使用了来自 AltBeacon 图书馆网站 Link: (http://altbeacon.github.io/android-beacon-library/samples.html) 的 监控示例代码 。但我无法检测到任何 Ibeacon。并且无法获取有关检测到任何信标的任何日志。
我的清单文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ibeaconscanner.ibeaconproject.ibeaconproject" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我在 MainActivity.java 中添加了 Requesting Permission 代码来解决问题,但无法解决。 我的 MainActivity.java 是这样的:
public class MainActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "MyApp";
private BeaconManager beaconManager;
private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location acces");
builder.setMessage("Please grat location acces so this app can detect beacons");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
}
});
}
}
setContentView(R.layout.activity_main);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.d(TAG, "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.d(TAG, "I no longer see an beacon");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.d(TAG, "I have just switched from seeing/not seeing beacons: " + state);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", null, null, null));
} catch (RemoteException e) {
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "coarse location permission granted");
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Functionality limited");
builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
}
}
);
builder.show();
}
return;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
几点提示:
确保您可以使用现成的信标检测应用程序检测您的信标,例如 Locate. 如果您看不到信标,则可能是硬件问题。
默认情况下,库仅检测发送 AltBeacon 格式的信标。如果您使用专有信标类型,则需要调用
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("**PUT LAYOUT EXPRESSION HERE**"));
为您的信标类型添加布局。如果您使用的是Android6,请确保您的应用已授予位置权限。转到设置 -> 应用程序 -> [您的应用程序名称] -> 权限,并确保授予位置权限。
如果您使用的是 Android6,请检查“设置”->“位置”是否设置为“开”。这对于包括 Nexus 5 在内的某些设备是必需的。
如果上述方法没有帮助,请尝试向您的应用程序添加日志记录(如
onBeaconServiceConnect()
)并报告哪些方法被调用,哪些方法没有被调用。
要检测 iBeacon,您需要修改 altbeacon 库以使其理解 ibeacon 数据包格式。在 onCreate 函数中试试这个:
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.setForegroundScanPeriod(1000l);
beaconManager.setForegroundBetweenScanPeriod(10000l);
beaconManager.bind(this);
setBeaconLayout 定义检测 iBeacons 的数据包结构。可以详细了解包结构here.
setForegroundScanPeriod 在我的代码中设置为 1 秒,setForegroundBetweenScanPeriod 设置为 10 秒。您可以将其更改为您想要的任何值。您可能希望根据需要将 setForegroundBetweenScanPeriod 减少到 3 秒。基本上,这两个设置定义设备处于扫描模式 1 秒,并每 10 秒重复扫描一次。
更新
要显示检测到的信标的 UUID,请使用以下内容:
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
for (Beacon beacon : beacons) {
logToDisplay("Detected " + beacon.toString() + " with UUID " + beacon.getId1().toString() + " and Major ID " + beacon.getId2().toString() + " and Minor ID " + beacon.getId3().toString());
}
}
else {
logToDisplay("No iBeacons detected");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(myRegion);
} catch (RemoteException e) { }
}
private void logToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
EditText editText = (EditText)RangingActivity.this.findViewById(R.id.rangingText);
editText.append(line+"\n");
}
});
}