Android EddyStone 信标的信标库
Android Beacon Library for EddyStone beacons
我是使用 Android 扫描信标的新手。我正在使用上面提到的库,这很容易理解。参考应用程序工作并检测我的 Eddystone 信标。基于示例代码,我编写了一个简单的应用程序来检测 Eddystone 信标。这是行不通的。
在 logcat 上,我收到以下消息;
D/BluetoothAdapter: STATE_ON
D/BluetoothLeScanner: 找不到回调包装器
这是我的代码,谁能告诉我,我做错了什么。
请帮助。
public class MainActivity extends AppCompatActivity implements BeaconConsumer {
private BeaconManager beaconManager;
Identifier myBeaconNamespaceId;
Identifier myBeaconInstanceId;
Region region;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBeaconNamespaceId = Identifier.parse("0x334652820242ac130002");
myBeaconInstanceId = Identifier.parse("0x987654321cba");
beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
// beaconManager.setDebug(true);
region = new Region("MyRegion",
myBeaconNamespaceId, myBeaconInstanceId, null);
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.removeAllRangeNotifiers();
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i("INFORMATION", "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("MyRegion", myBeaconNamespaceId, myBeaconInstanceId, null));
} catch (RemoteException e) {
Toast.makeText(getApplicationContext(), e.getMessage() ,Toast.LENGTH_LONG).show();
}
}
private void logToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
EditText editText = (EditText)MainActivity.this.findViewById(R.id.rangingText);
editText.append(line+"\n");
}
});
}
}
确保您已通过动态请求获得用户的位置权限:
https://altbeacon.github.io/android-beacon-library/requesting_permission.html
如果这不起作用,我这里有许多其他故障排除步骤:
https://altbeacon.github.io/android-beacon-library/detection-trouble.html
我是使用 Android 扫描信标的新手。我正在使用上面提到的库,这很容易理解。参考应用程序工作并检测我的 Eddystone 信标。基于示例代码,我编写了一个简单的应用程序来检测 Eddystone 信标。这是行不通的。 在 logcat 上,我收到以下消息; D/BluetoothAdapter: STATE_ON D/BluetoothLeScanner: 找不到回调包装器
这是我的代码,谁能告诉我,我做错了什么。 请帮助。
public class MainActivity extends AppCompatActivity implements BeaconConsumer {
private BeaconManager beaconManager;
Identifier myBeaconNamespaceId;
Identifier myBeaconInstanceId;
Region region;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBeaconNamespaceId = Identifier.parse("0x334652820242ac130002");
myBeaconInstanceId = Identifier.parse("0x987654321cba");
beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
// beaconManager.setDebug(true);
region = new Region("MyRegion",
myBeaconNamespaceId, myBeaconInstanceId, null);
beaconManager.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.removeAllRangeNotifiers();
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i("INFORMATION", "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("MyRegion", myBeaconNamespaceId, myBeaconInstanceId, null));
} catch (RemoteException e) {
Toast.makeText(getApplicationContext(), e.getMessage() ,Toast.LENGTH_LONG).show();
}
}
private void logToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
EditText editText = (EditText)MainActivity.this.findViewById(R.id.rangingText);
editText.append(line+"\n");
}
});
}
}
确保您已通过动态请求获得用户的位置权限:
https://altbeacon.github.io/android-beacon-library/requesting_permission.html
如果这不起作用,我这里有许多其他故障排除步骤:
https://altbeacon.github.io/android-beacon-library/detection-trouble.html