蓝牙低功耗设备扫描无法与一个异常 [英] Bluetooth Low Energy device scanning Failed with an exception
问题描述
我指的是开发者文档扫描BLE装置 -
http://developer.android.com/guide/topics/连接/蓝牙le.html
在我的活动类 LookUpActivity
,我叫上点击一个按钮的方法 findBLE()
-
//这将扫描BLE装置
公共无效findBLE()
{
意向意图=新意图(LookUpActivity.this,DeviceScanActivity.class);
startActivity(意向);
}
的但得到了以下异常的 -
找不到类的com.testapp.main.DeviceScanActivity $ 1',
从方法com.testapp.main.DeviceScanActivity引用<&初始化GT;
com.testapp.main致命错误:com.testapp.main.DeviceScanActivity $ 1
java.lang.NoClassDefFoundError的:com.testapp.main.DeviceScanActivity $ 1
在com.testapp.main.DeviceScanActivity<&初始化GT;(DeviceScanActivity.java:179)
DeviceScanActivity.java ---
/ **
*用于扫描和显示可用的蓝牙LE设备活动。
* /
@燮pressLint(NewApi)
公共类DeviceScanActivity扩展ListActivity {
私人LeDeviceListAdapter mLeDeviceListAdapter;
私人BluetoothAdapter mBluetoothAdapter;
私人布尔mScanning;
私人处理器mHandler; 私有静态最终诠释REQUEST_ENABLE_BT = 1;
// 10秒后停止扫描。
私有静态最后长SCAN_PERIOD = 10000; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
mHandler =新的处理程序();
} @覆盖
保护无效onResume(){
super.onResume(); //确保蓝牙设备上启用。如果蓝牙当前未启用,
//火的意图来显示一个对话框,要求用户授予权限,以启用它。
如果(!mBluetoothAdapter.isEnabled()){
如果(!mBluetoothAdapter.isEnabled()){
意图enableBtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
} //初始化列表视图适配器。
mLeDeviceListAdapter =新LeDeviceListAdapter();
setListAdapter(mLeDeviceListAdapter);
scanLeDevice(真);
} @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
//用户选择不启用蓝牙。
如果(要求code == REQUEST_ENABLE_BT&放大器;&安培;结果code == Activity.RESULT_CANCELED){
完();
返回;
}
super.onActivityResult(要求code,结果code,数据);
} @覆盖
保护无效的onPause(){
super.onPause();
scanLeDevice(假);
mLeDeviceListAdapter.clear();
} 私人无效scanLeDevice(最终布尔启用){
如果(启用){
//一个pre定义的扫描时间后停止扫描。
mHandler.postDelayed(新的Runnable(){
@覆盖
公共无效的run(){
mScanning = FALSE;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
},SCAN_PERIOD); mScanning = TRUE;
mBluetoothAdapter.startLeScan(mLeScanCallback);
}其他{
mScanning = FALSE;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
} //适配器保持通扫描找到的设备。
私有类LeDeviceListAdapter延伸BaseAdapter {
私人的ArrayList<&BluetoothDevice类GT; mLeDevices;
私人LayoutInflater mInflator; 公共LeDeviceListAdapter(){
超();
mLeDevices =新的ArrayList<&BluetoothDevice类GT;();
mInflator = DeviceScanActivity.this.getLayoutInflater();
} 公共无效的AddDevice(BluetoothDevice类设备){
如果(!mLeDevices.contains(设备)){
mLeDevices.add(设备);
}
} 公共BluetoothDevice类的getDevice(INT位置){
返回mLeDevices.get(位置);
} 公共无效清除(){
mLeDevices.clear();
} @覆盖
公众诠释的getCount(){
返回mLeDevices.size();
} @覆盖
公共对象的getItem(int i)以{
返回mLeDevices.get(ⅰ);
} @覆盖
众长getItemId(int i)以{
返回我;
} @覆盖
公共查看getView(INT I,观景,ViewGroup中的ViewGroup){
ViewHolder viewHolder;
//一般的ListView优化code。
如果(查看== NULL){
鉴于= mInflator.inflate(R.layout.mapentries,NULL);
viewHolder =新ViewHolder();
viewHolder.deviceAddress =(TextView中)view.findViewById(R.id.entriestitle);
//viewHolder.deviceName =(TextView中)view.findViewById(R.id.device_name);
view.setTag(viewHolder);
}其他{
viewHolder =(ViewHolder)view.getTag();
} BluetoothDevice类设备= mLeDevices.get(ⅰ);
最后弦乐DEVICENAME = device.getName();
/ *如果(DEVICENAME = NULL&放大器;!&安培; deviceName.length()0)
viewHolder.deviceName.setText(DEVICENAME);
其他
viewHolder.deviceName.setText(R.string.unknown_device); * / viewHolder.deviceAddress.setText(device.getAddress()); 返回视图。
}
} //设备扫描回调。
私人BluetoothAdapter.LeScanCallback mLeScanCallback =
新BluetoothAdapter.LeScanCallback(){//这是行179导致错误... @覆盖
公共无效onLeScan(最终BluetoothDevice类设备,INT RSSI,字节[] scanRecord){
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
mLeDeviceListAdapter.addDevice(设备);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
}; 静态类ViewHolder {
// TextView的DEVICENAME;
TextView的deviceAddress;
}
}
更新:
为了更具体,我下载了确切的项目code为BLE从---
http://developer.android.com/samples/BluetoothLeGatt/project.html
和再次得到了同样的例外,甚至在项目中使用中提到的code。
这一次错误控制台 -
致命异常:主要
java.lang.NoClassDefFoundError的:
com.example.android.bluetoothlegatt
.DeviceScanActivity $ 1
在com.example.android.bluetoothlegatt.DeviceScanActivity。
<&初始化GT; (DeviceScanActivity.java:250)
您必须执行全面检查,以查看该设备是否支持蓝牙低功耗(BLE)。该设备可以运行Android 4.3或更高版本,但缺乏必要的BLE硬件。 Nexus的7(2012)是这样的装置的一个例子。这里的code:
私人布尔bleCheck(){
布尔结果= FALSE;
如果(getPackageManager()。hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
mBluetoothManager =(BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
如果(mBluetoothManager!= NULL){
mBluetoothAdapter = mBluetoothManager.getAdapter();
如果(mBluetoothAdapter!= NULL){
如果(mBluetoothAdapter.isEnabled()){
结果=真;
}
}
}
}
返回结果;
}
I am referring to developer docs to scan BLE devices -
http://developer.android.com/guide/topics/connectivity/bluetooth-le.html
In my activity class LookUpActivity
, I called a method findBLE()
on a button click-
//This will scan BLE devices
public void findBLE()
{
Intent intent = new Intent(LookUpActivity.this, DeviceScanActivity.class);
startActivity(intent);
}
But got the following exception -
Could not find class 'com.testapp.main.DeviceScanActivity$1',
referenced from method com.testapp.main.DeviceScanActivity.<init>
com.testapp.main fatal error : com.testapp.main.DeviceScanActivity$1
java.lang.NoClassDefFoundError: com.testapp.main.DeviceScanActivity$1
at com.testapp.main.DeviceScanActivity.<init>(DeviceScanActivity.java:179)
DeviceScanActivity.java ---
/**
* Activity for scanning and displaying available Bluetooth LE devices.
*/
@SuppressLint("NewApi")
public class DeviceScanActivity extends ListActivity {
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
}
@Override
protected void onResume() {
super.onResume();
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
// Initializes list view adapter.
mLeDeviceListAdapter = new LeDeviceListAdapter();
setListAdapter(mLeDeviceListAdapter);
scanLeDevice(true);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = DeviceScanActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.mapentries, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.entriestitle);
//viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(i);
final String deviceName = device.getName();
/* if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);*/
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() { //This is line 179 causing error...
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
static class ViewHolder {
//TextView deviceName;
TextView deviceAddress;
}
}
UPDATE:
In order to be more specific, I downloaded the exact project code for BLE from ---
http://developer.android.com/samples/BluetoothLeGatt/project.html
And again got the same exception even using the code mentioned in the project.
This time the error console--
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError:
com.example.android.bluetoothlegatt
.DeviceScanActivity$1
at com.example.android.bluetoothlegatt.DeviceScanActivity.
<init> (DeviceScanActivity.java:250)
You must perform the full check to see if the device supports Bluetooth Low Energy (BLE). The device could be running Android 4.3 or later but lacks the hardware necessary for BLE. The Nexus 7 (2012) is an example of such device. Here's the code:
private boolean bleCheck() {
boolean result = false;
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager != null) {
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isEnabled()) {
result = true;
}
}
}
}
return result;
}
这篇关于蓝牙低功耗设备扫描无法与一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!