通过在Android和Arduino之间传输数据来解决蓝牙问题

时间:2017-07-19 08:42:00

标签: java android bluetooth arduino

正如您所看到的,我制作了一个应用程序,可以将数据从Android设备发送到蓝牙模块。除了一件小事之外,一切都很完美:每次我想打开应用程序我都应该通过我的手机设置打开蓝牙,否则应用程序会崩溃,打开蓝牙后我必须重新打开它运行得当。

我自己设计了一个蓝牙点击监听按钮,但是当我通过按钮启用它时它仍然崩溃。

你能帮我在代码中找到错误吗?

union {
  float fl;
  uint32_t ui;
} a, b

float a.fl = (math expressions);
float b.fl = (another math expressions); // both giving theoretically the same results

// comparition of

if(a.fl == b.fl) .... //UB - as float shall not be compared for being equal
if(a.ui == b.ui) .... //UB - as ui is an unsigned representation of the bytes of fl

1 个答案:

答案 0 :(得分:0)

您必须检查BT是否已启用。试试这个:

protected void checkBTState() {
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    if(mBtAdapter == null) {
        String message = getResources().getText(R.string.bluetooth_not_supported).toString();
        Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
    } else {
        if (mBtAdapter.isEnabled()) {
            Log.d(TAG, "...Bluetooth ON...");
        } else {
            // Prompt user to turn on Bluetooth //
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
        }
    }
}

将其放入onResume

@Override
public void onResume() {
    super.onResume();
    checkBTState();
    // ...
    // some code
    // ...        
}