我试图在该区域内阅读可用的信标。在API级别21下我使用BluetoothAdapter.LeScanCallback
而在21级以上我使用ScanCallback
当我在Kitkat上运行我的应用程序时,它是scaned beacon。但是当我在Lollipop上运行我的应用程序时,它已经强制关闭并显示此错误
java.lang.NoClassDefFoundError: test.abc.com.mybeacon.Beacon$2
'Beacon'是我的活动类
这是我的代码:
private BluetoothLeScanner mBluetoothLeScanner;
private ScanCallback mScanCallback;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beacon);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//JellyBean/Kitkat
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
//lollipop+
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
processResult(result);
}
};
startScan();
}
}
private void startScan() {
mBluetoothLeScanner.startScan(mScanCallback);
}
private void stopScan() {
mBluetoothLeScanner.stopScan(mScanCallback);
}
private void processResult(ScanResult result) {
Log.d("res"," "+result.getDevice().getName().toString());
}
}
答案 0 :(得分:0)
您尝试使用Kitkat及更低版本在设备上实例化ScanCallback,但他们并不知道ScanCallback类,因为它已添加到Lollipop中。 我建议在onCreate方法中实例化ScanCallback,在那里你可以检查设备是否正在运行Lollipop或更新。
编辑:只是为了澄清,这确实会失败并出现相同的错误,或者您是否会收到其他错误?
private BluetoothLeScanner mBluetoothLeScanner;
private ScanCallback mScanCallback;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beacon);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
//JellyBean/Kitkat
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
//lollipop+
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
processResult(result);
}
};
startScan();
}
}
private void startScan() {
mBluetoothLeScanner.startScan(mScanCallback);
}
private void processResult(ScanResult result) {
Log.d("res"," "+result.getDevice().getName().toString());
}
}