【问题标题】:Bluetooth Low Energy device scanning Failed with an exception蓝牙低功耗设备扫描失败并出现异常
【发布时间】:2014-04-23 21:41:50
【问题描述】:

我指的是开发者文档来扫描 BLE 设备 -

http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

在我的活动类LookUpActivity 中,我在单击按钮时调用了一个方法findBLE()-

//This will scan BLE devices
public void findBLE()
{
    Intent intent = new Intent(LookUpActivity.this, DeviceScanActivity.class);
    startActivity(intent);
}

但出现以下异常 -

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;
    }
}

更新:

为了更具体,我从 --- 下载了 BLE 的确切项目代码

http://developer.android.com/samples/BluetoothLeGatt/project.html

即使使用项目中提到的代码,也再次遇到相同的异常。

这次是错误控制台--

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError:   
com.example.android.bluetoothlegatt
.DeviceScanActivity$1
at com.example.android.bluetoothlegatt.DeviceScanActivity.
<init>  (DeviceScanActivity.java:250)

【问题讨论】:

  • 查看相关代码。我也提到了那里的行号。是new BluetoothAdapter.LeScanCallback() { //This is line 179 causing error...
  • 可能是 proguard 配置问题(如果您使用的是通过它处理的发布版本)?或者在没有蓝牙 API 的设备上运行?
  • 我没有使用 proguard。刚刚从开发人员站点下载了源代码项目,并从 eclipse 运行它,但出现了这个异常。我的设备是Sony Xperia J,它有蓝牙功能。
  • Xperia J 规范蓝牙 v2.1 + EDR 暗示它没有 BLE。
  • 所以bluetooth 功能本身与设备中的BLE feature 不同?

标签: android bluetooth-lowenergy android-bluetooth


【解决方案1】:

您必须执行全面检查以查看设备是否支持蓝牙低功耗 (BLE)。该设备可能运行 Android 4.3 或更高版本,但缺少 BLE 所需的硬件。 Nexus 7 (2012) 就是这种设备的一个例子。代码如下:

  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;
  }

【讨论】:

  • 谢谢。所以我有一个支持 BLE 的黑盒设备,我用它作为传感器来记录车辆的速度。该传感器将与我的安卓手机对话以发送数据。那么我的安卓手机应该支持BLE还是经典的蓝牙功能可以很好地向传感器发送数据和从传感器接收数据?
  • 这取决于传感器。其中一些既支持经典蓝牙又支持 BLE。测试它的一个好方法是在 Android 设备上打开您的设置,转到蓝牙并开始扫描。如果您的传感器没有出现,那么它只是 BLE。那么您是否在设备上运行了 bleCheck() 方法?
  • 是的,它返回 false。所以兼容 BLE 的传感器永远不会出现在我的设备上?我什至不能用我的设备测试我的代码,因为它不是 BLE。我必须购买兼容 BLE 的安卓手机?
  • 正确。 Google Nexus 7 (2013) 是一个不错的选择。三星设备不错,但我只会买一台配备 Android 4.3 的设备。运行 Android 4.2 的设备使用三星蓝牙 SDK。你能接受我的回答吗?
  • +1。在使用 BLE 的未来开发中可能需要您的帮助。现在需要更改我的 BLE 发现/扫描代码,因为在这方面的知识不足,我为经典蓝牙发现/扫描做了它,这是另一回事,没有用。谢谢。
猜你喜欢
  • 2014-04-23
  • 2014-09-29
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多