无法扫描信标
Unable to scan for beacons
我一直在尝试扫描信标,但我做不到。我尝试了所有 Whosebug 问题,但没有找到答案。
下面是我的代码:
主要活动:
public class MainActivity extends AppCompatActivity implements BeaconConsumer {
protected static final String TAG = "MonitoringActivity";
private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.bind(this);}
@Override
public void onBeaconServiceConnect() {
beaconManager.addMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "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);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", Identifier.parse(null, null, null));
} catch (RemoteException e) { }
}
清单
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
gradle.properties
manifestmerger.enabled=true
logcat
06-19 20:03:37.403 7459-7459/com.example.beaconscanner D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
06-19 20:03:37.796 7459-7459/com.example.beaconscanner I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
06-19 20:03:37.933 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON
06-19 20:03:37.938 7459-7471/com.example.beaconscanner D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=12
06-19 20:03:39.038 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON
06-19 20:03:39.040 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON
06-19 20:03:39.044 7459-7470/com.example.beaconscanner D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=12
我根据Android SDK 大于23
添加了定位和蓝牙权限
从您的调试行看来,Android 正在检测蓝牙设备,这表明蓝牙已打开并且您的应用程序具有扫描权限。
需要检查的几件事:
确保您的 BeaconLayout 正确。 "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25" 用于 AltBeacon。如果您的信标传输不同的格式(例如 iBeacon 或 Eddystone),您将需要不同的布局。这里有一个很好的参考:https://beaconlayout.wordpress.com/
确保您的设备确实发出了有效的信标信号。我建议使用我的 Locate app for Android,因为它会自动检测所有广告 iBeacon、AltBeacon 和 Eddystone 格式的信标。
我一直在尝试扫描信标,但我做不到。我尝试了所有 Whosebug 问题,但没有找到答案。
下面是我的代码:
主要活动:
public class MainActivity extends AppCompatActivity implements BeaconConsumer {
protected static final String TAG = "MonitoringActivity";
private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.bind(this);}
@Override
public void onBeaconServiceConnect() {
beaconManager.addMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.i(TAG, "I just saw an beacon for the first time!");
}
@Override
public void didExitRegion(Region region) {
Log.i(TAG, "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);
}
});
try {
beaconManager.startMonitoringBeaconsInRegion(new Region("myMonitoringUniqueId", Identifier.parse(null, null, null));
} catch (RemoteException e) { }
}
清单
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
gradle.properties
manifestmerger.enabled=true
logcat
06-19 20:03:37.403 7459-7459/com.example.beaconscanner D/BeaconParser: Parsing beacon layout: m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25
06-19 20:03:37.796 7459-7459/com.example.beaconscanner I/MonitoringActivity: I have just switched from seeing/not seeing beacons: 0
06-19 20:03:37.933 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON
06-19 20:03:37.938 7459-7471/com.example.beaconscanner D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=12
06-19 20:03:39.038 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON
06-19 20:03:39.040 7459-7729/com.example.beaconscanner D/BluetoothAdapter: STATE_ON
06-19 20:03:39.044 7459-7470/com.example.beaconscanner D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=12
我根据Android SDK 大于23
添加了定位和蓝牙权限从您的调试行看来,Android 正在检测蓝牙设备,这表明蓝牙已打开并且您的应用程序具有扫描权限。
需要检查的几件事:
确保您的 BeaconLayout 正确。 "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25" 用于 AltBeacon。如果您的信标传输不同的格式(例如 iBeacon 或 Eddystone),您将需要不同的布局。这里有一个很好的参考:https://beaconlayout.wordpress.com/
确保您的设备确实发出了有效的信标信号。我建议使用我的 Locate app for Android,因为它会自动检测所有广告 iBeacon、AltBeacon 和 Eddystone 格式的信标。