我无法让我的Android设备通过蓝牙LE连接到我的加速器。
我正在尝试从Razor 9DOF传感器读取加速度值,
在此处找到:https://www.sparkfun.com/products/10736。
使用蓝牙LE,
在此处找到:https://www.sparkfun.com/products/13019
这是我正在使用的代码:
这就是我搜索蓝牙LE设备的方式。
public class BluetoothActivity extends ActionBarActivity {
private final static String TAG = BluetoothActivity.class.getSimpleName();
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
private String mBluetoothDeviceAddress;
private BluetoothGatt mBluetoothGatt;
private int mConnectionState = STATE_DISCONNECTED;
private MelodySmartDevice melodySmartDevice;
private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;
private ListView bluetoothListView;
private LeDeviceListAdapter mLeDeviceListAdapter;
private boolean mScanning;
private Handler mHandler;
protected String mDeviceAddress;
protected String mDeviceName;
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);
setContentView(R.layout.activity_bluetooth);
mHandler = new Handler();
melodySmartDevice = MelodySmartDevice.getInstance();
melodySmartDevice.init(getApplicationContext());
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
bluetoothListView = (ListView) findViewById(R.id.bluetoothListView);
mLeDeviceListAdapter = new LeDeviceListAdapter();
bluetoothListView.setAdapter(mLeDeviceListAdapter);
bluetoothListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
scanLeDevice(false);
mDeviceAddress = viewHolder.deviceAddress.getText().toString();
mDeviceName = viewHolder.deviceName.getText().toString();
Intent i = new Intent(getApplicationContext(), OperatingDataActivity.class);
i.putExtra("deviceAddress", mDeviceAddress);
i.putExtra("deviceName", mDeviceName);
startActivity(i);
}
});
//set to scan when view opened
if (!mScanning) {
mLeDeviceListAdapter.clear();
scanLeDevice(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_bluetooth, menu);
if (!mScanning) {
menu.findItem(R.id.menu_stop).setVisible(false);
menu.findItem(R.id.menu_scan).setVisible(true);
menu.findItem(R.id.menu_refresh).setActionView(null);
} else {
menu.findItem(R.id.menu_stop).setVisible(true);
menu.findItem(R.id.menu_scan).setVisible(false);
menu.findItem(R.id.menu_refresh).setActionView(
R.layout.actionbar_indeterminate_progress);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_scan:
mLeDeviceListAdapter.clear();
scanLeDevice(true);
break;
case R.id.menu_stop:
scanLeDevice(false);
break;
}
return true;
}
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;
melodySmartDevice.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
melodySmartDevice.startLeScan(mLeScanCallback);
} else {
mScanning = false;
melodySmartDevice.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
// 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 = BluetoothActivity.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.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
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() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
private class ViewHolder {
TextView deviceName;
TextView deviceAddress;
}
}
然后我将设备名称和地址发送到此活动以开始阅读值。
public class OperatingDataActivity extends ActionBarActivity {
protected static final String TAG = OperatingDataActivity.class.getSimpleName();
private boolean mConnected = false;
private RazorAHRS razor;
private BluetoothAdapter mBluetoothAdapter;
private TextView txtStroke;
private TextView connectButton;
private AlertDialog connectingDialog;
private String deviceAddress;
private String deviceName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_operating_data);
setTextViews();
setupBluetooth();
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Button Pressed");
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceAddress);
razor = new RazorAHRS(device, new RazorListener() {
@Override
public void onAnglesUpdate(float yaw, float pitch, float roll) {
}
@Override
public void onSensorsUpdate(float accX, float accY, float accZ, float magX, float magY, float magZ, float gyrX, float gyrY, float gyrZ) {
txtStroke.setText(accZ + "");
}
@Override
public void onIOExceptionAndDisconnect(IOException e) {
}
@Override
public void onConnectAttempt(int attempt, int maxAttempts) {
Toast.makeText(OperatingDataActivity.this, "Connect attempt " + attempt + " of " + maxAttempts + "...", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectOk() {
Log.d(TAG, "Connected");
}
@Override
public void onConnectFail(Exception e) {
Log.d(TAG, "Connection Failed");
}
});
}
});
}
private void setupBluetooth() {
//Bluetooth LE-------------------
/* Get the instance of the bluetooth le Android library and initialize it */
deviceAddress = getIntent().getStringExtra("deviceAddress");
deviceName = getIntent().getStringExtra("deviceAddress");
if (deviceAddress != null) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Toast.makeText(getApplicationContext(), Html.fromHtml("Connected to <b>" + deviceName + "</b>."), Toast.LENGTH_SHORT).show();
}
//----------------------------------
}
private void setTextViews() {
txtStroke = (TextView) findViewById(R.id.txtStroke2);
connectButton = (TextView) findViewById(R.id.connectButton2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_operating_data, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_email) {
Intent intent = new Intent(this, EmailActivity.class);
startActivity(intent);
overridePendingTransition(R.transition.slide_in_right, R.transition.slide_out_left);
} else if (id == R.id.action_info) {
Intent intent = new Intent(this, VibrationHelp.class);
startActivity(intent);
overridePendingTransition(R.transition.slide_in_right, R.transition.slide_out_left);
} else if (id == R.id.action_bluetooth) {
Intent intent = new Intent(this, BluetoothActivity.class);
startActivity(intent);
overridePendingTransition(R.transition.slide_in_right, R.transition.slide_out_left);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
}
感谢任何帮助。
感谢