2014-04-22 24 views
0

我指的是開發者文檔失敗藍牙低功耗設備掃描來掃描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; 
    } 
} 

UPDATE:

爲了更具體,我下載了從BLE確切的項目代碼---

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

再次得到了相同即使使用項目中提到的代碼也是例外。

這一次的錯誤console--

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

什麼是第179行? –

+0

查看相關代碼。我在那裏還提到了行號。它是'new BluetoothAdapter.LeScanCallback(){//這是第179行導致錯誤......' –

+0

也許是一個proguard配置問題(如果你使用的是通過它發佈的版本)?或者在沒有藍牙API的設備上運行? –

回答

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

謝謝。所以我有一個黑匣子設備,它可以用作傳感器來記錄車輛的速度。那個傳感器會跟我的android手機發送數據。那麼,我的android手機應該是BLE能力還是經典的藍牙功能可以很好地向傳感器發送數據和從傳感器接收數據? –

+0

這取決於傳感器。其中一些是藍牙經典和BLE能力。測試它的好方法是在Android設備上打開您的設置,轉到藍牙並開始掃描。如果你的傳感器沒有出現,那麼它只是BLE。那麼您是否在設備上運行了bleCheck()方法? – itsben

+0

是的,它返回false。那麼與BLE兼容的傳感器將永遠不會出現在我的設備上?我甚至不能用我的設備測試我的代碼,因爲它不是BLE。我必須購買一部BLE兼容的android手機? –