我是Android Studio和Java的新手。 快速回顾一下我的项目: 蓝牙控制的汽车,来自智能手机til arduino。就是这样。
我的代码是不同代码加上我自己的代码的组合。但是,当我尝试做出一个会启动和停止汽车的开关(现在打开和关闭LED)时,出现以下错误。
错误:不可比拟的类型:开关和布尔
也许我只是不明白if陈述。这是发生错误的部分:
Switch on_off_switch = (Switch) findViewById(R.id.on_off_switch);
on_off_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.v("Switch State=", ""+isChecked);
}
});
if (on_off_switch == true) {
command = "1";
try
{
outputStream.write(command.getBytes()); //transmits the value of command to the bluetooth module
}
catch (IOException e)
{
e.printStackTrace();
}
}
问题可能出在代码中的其他地方,所以这就是全部:
package com.example.btcar2;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
final String DEVICE_ADDRESS = "00:12:12:24:06:48"; //MAC Address of Bluetooth Module
private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private BluetoothDevice device;
private BluetoothSocket socket;
private OutputStream outputStream;
Button bluetooth_connect_btn;
String command; //string variable that will store value to be transmitted to the bluetooth module
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch on_off_switch = (Switch) findViewById(R.id.on_off_switch);
on_off_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.v("Switch State=", ""+isChecked);
}
});
if (on_off_switch == true) {
command = "1";
try
{
outputStream.write(command.getBytes()); //transmits the value of command to the bluetooth module
}
catch (IOException e)
{
e.printStackTrace();
}
}
bluetooth_connect_btn = (Button) findViewById(R.id.bluetooth_connect_btn);
//Button that connects the device to the bluetooth module when pressed
bluetooth_connect_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(BTint())
{
BTconnect();
}
}
});
}
//Initializes bluetooth module
public boolean BTint()
{
boolean found = false;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null) //Checks if the device supports bluetooth
Toast.makeText(getApplicationContext(), "Device doesn't support bluetooth", Toast.LENGTH_SHORT).show();
if(!bluetoothAdapter.isEnabled()) //Checks if bluetooth is enabled. If not, the program will ask permission from the user to enable it
{
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter,0);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
if(bondedDevices.isEmpty()) //Checks for paired bluetooth devices
Toast.makeText(getApplicationContext(), "Please pair the device first", Toast.LENGTH_SHORT).show();
else
{
for(BluetoothDevice iterator : bondedDevices)
{
if(iterator.getAddress().equals(DEVICE_ADDRESS))
{
device = iterator;
found = true;
break;
}
}
}
return found;
}
public boolean BTconnect()
{
boolean connected = true;
try
{
socket = device.createRfcommSocketToServiceRecord(PORT_UUID); //Creates a socket to handle the outgoing connection
socket.connect();
Toast.makeText(getApplicationContext(),
"Connection to bluetooth device successful", Toast.LENGTH_LONG).show();
}
catch(IOException e)
{
e.printStackTrace();
connected = false;
}
if(connected)
{
try
{
outputStream = socket.getOutputStream(); //gets the output stream of the socket
}
catch(IOException e)
{
e.printStackTrace();
}
}
return connected;
}
@Override
protected void onStart()
{
super.onStart();
}
}
对不起,如果我的问题不清楚。这也是我第一次问有关stackoverflow的问题。如果您有任何问题可以帮助我解决问题,请写信:)谢谢
答案 0 :(得分:1)
在您的代码中,您尝试将Switch
对象与boolean
值进行比较,这是不允许的。
获取Switch
状态的方法是通过isChecked()
方法:
if (on_off_switch.isChecked() == true)
或简单地:
if (on_off_switch.isChecked())
答案 1 :(得分:0)
错误:不可比拟的类型:开关和布尔值
这告诉您您正在尝试比较两个无法比较的值。在这种情况下,您尝试将代表UI元素的Switch
与boolean
(一种内置类型,只能具有值true
和`false)进行比较。 / p>
就像我说的,Switch
是一个UI元素。它可以是“ on”或“ off”。此状态存储在Switch
对象中,我们需要弄清楚如何访问它。我们可以看看the documentation来找到正确的方法。向下滚动到标有“公共方法”的部分,然后扫描方法名称以查找听起来可能可以完成工作的内容。弄清Switch
的状态似乎没有什么明显的事,因此我们必须更深入地研究。向下滚动到“继承的方法”部分,然后展开“ CompoundButton”部分。向下滚动时,我们看到有一个isChecked()
方法。
所以我们尝试一下:
if (on_off_switch.isChecked())
请注意,您不需要使用== true
。比较的值是布尔值(true
或false
),但是isChecked()
已经返回了布尔值,因此不需要额外的输入。