我有一个android应用程序。我的目的是检测Ibeacons使用AltBeacon库。我使用监控示例代码从AltBeacon图书馆网站链接:(http://altbeacon.github.io/android-beacon-library/samples.html)。但是我没有探测到任何信标。并且无法获得任何关于任何信标检测的日志。
我的Manifest文件是这样的:
<?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来解决问题,但不能。我的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**"));
来为你的信标类型添加布局。如果你是在Android 6上,确保你的应用程序已授予位置权限。进入设置->应用程序->[你的应用程序名称]->权限,并确保授予位置权限。
如果你使用的是Android 6,检查Settings -> Location是否设置为on。
如果上面没有帮助,试着在你的应用程序中添加日志记录(如
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定义了检测ibeacon的数据包结构。
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");
}
});
}