由于提到问题here,我按如下方式配置mornitoring example code,但我的应用无法检测到部署的iBeacon广告客户。
public class MainActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "MornitoringActivity";
public static final String UUID = "UUID: D57092AC-DFAA-446C-8EF3-C81AA22815B5";
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
TextView mInfoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInfoView = (TextView) findViewById(R.id.show_info_lable);
beaconManager.setDebug(true);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);
Log.i(TAG, "on create complete!");
}
@Override
protected void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
Log.i(TAG, "on destroy complete!");
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
mInfoView.setText("I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "I no longer see an beacon");
mInfoView.setText("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);
mInfoView.setText("I have just switched from seeing/not seeing beacons: "+state); }
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", Identifier.parse(UUID), null, null));
} catch (RemoteException e) { }
}
}
将android-beacon-library / libs /中的class.jar复制到myProject / libs /中,并将其添加到构建路径
手动编辑AndroidManifest.xml,这是我的xml文件,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mymornitoring"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/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>
<service
android:enabled="true"
android:exported="true"
android:isolatedProcess="false"
android:label="beacon"
android:name="org.altbeacon.beacon.service.BeaconService">
</service>
<service
android:enabled="true"
android:name="org.altbeacon.beacon.BeaconIntentProcessor">
</service>
<receiver android:name="org.altbeacon.beacon.startup.StartupBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
</application>
使用运行BeaconOSX应用程序的Macbook作为iBeacon广告客户。(并且我已经在iPhone 4s上使用AirLocate应用程序来确认iBeacon广告客户的工作原理,但iBeacon无法检测到找不到android由Radius Networks开发的应用程序
然后我在Nexus 5上运行myProject,logcat消息如下
11-06 10:49:08.838: I/BeaconService(5023): beaconService version 2.0 is starting up
11-06 10:49:08.878: D/BeaconService(5023): No org.altbeacon.beacon.SimulatedScanData class exists.
11-06 10:49:08.878: I/BeaconService(5023): binding
11-06 10:49:08.908: I/BeaconService(5023): start monitoring received
11-06 10:49:08.908: D/BeaconService(5023): startMonitoring called
11-06 10:49:08.908: D/BeaconService(5023): Currently monitoring 1 regions.
11-06 10:49:08.928: D/BeaconService(5023): Waiting to stop scan for another 1100 milliseconds
11-06 10:49:08.928: D/BeaconService(5023): Scan started
11-06 10:49:09.938: D/BeaconService(5023): Waiting to stop scan for another 99 milliseconds
11-06 10:49:10.038: D/BeaconService(5023): Done with scan cycle
11-06 10:49:10.048: D/BeaconService(5023): Restarting scan. Unique beacons seen last cycle: 0 Total beacon advertisement packets seen: 0
BeaconService很好用。
11-06 10:48:41.238: D/BtGatt.GattService(1483): registerClient() - UUID=6eaaf541-90fb-4e88-a0d5-1f267769128b
11-06 10:48:41.248: D/BtGatt.GattService(1483): onClientRegistered() - UUID=6eaaf541-90fb-4e88-a0d5-1f267769128b, clientIf=5
11-06 10:48:41.248: D/BtGatt.GattService(1483): startScan() - queue=0
11-06 10:48:41.248: D/BtGatt.GattService(1483): startScan() - adding client=5
11-06 10:48:51.258: D/BtGatt.GattService(1483): stopScan() - queue=1
11-06 10:48:51.258: D/BtGatt.GattService(1483): stopScan() - queue empty; stopping scan
11-06 10:48:51.258: D/BtGatt.GattService(1483): unregisterClient() - clientIf=5
11-06 10:48:41.238: D/BluetoothAdapter(1365): startLeScan(): null
11-06 10:48:41.248: D/BluetoothAdapter(1365): onClientRegistered() - status=0 clientIf=5
11-06 10:48:51.258: D/BluetoothAdapter(1365): stopLeScan()
11-06 10:49:08.918: D/BluetoothAdapter(5023): startLeScan(): null
11-06 10:49:08.928: D/BluetoothAdapter(5023): onClientRegistered() - status=0 clientIf=5
11-06 10:49:10.038: D/BluetoothAdapter(5023): stopLeScan()
11-06 10:49:10.048: D/BluetoothAdapter(5023): startLeScan(): null
我注意到我的Nexus 5上有两个应用程序,它们打印相同的BluetoothAdapter日志消息。这有关系吗?从未调用didEnterRegion / didExitRegion。问题是什么?