gpt4 book ai didi

BluetoothAdapter.isEnabled() 上的 Java NullPointerException

转载 作者:行者123 更新时间:2023-12-02 05:05:13 26 4
gpt4 key购买 nike

你好,我的代码有问题。我想检查该应用程序是否已启用。如果蓝牙已启用并且设备已配对,我想连接到该设备,如果没有,我想尝试连接到它。

如果我的蓝牙设备已打开并且已连接,一切正常并且我正在获取数据,但如果设备关闭或未配对,我会得到以下信息:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference.

希望你能帮助我。

主要 Activity fragment :

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_wind, container, false);

........

try
{
findBT();
openBT();
}
catch (IOException ex) { }


//Send Button


//Close button
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
closeBT();
}
catch (IOException ex) { }
}
});
.........


void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null) {
arduinodata.setText("Bluetooth Device not Found");
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);

}



Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("HC05"))
{
mmDevice = device;
break;
}
}
}
arduinodata.setText("Bluetooth Device Found");
}

void openBT() throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //Standard SerialPortService ID
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();

beginListenForData();

arduinodata.setText("Bluetooth Opened");
}

void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character

stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;

handler.post(new Runnable()
{
public void run()
{
arduinodata.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});

workerThread.start();
}


void closeBT() throws IOException
{
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
arduinodata.setText("Bluetooth Closed");
}

最佳答案

蓝牙适配器的isEnabled()方法可能是一个Wrapper Boolean类,它可以有空值。如果我们直接在 if 条件中检查 true 或 false,则可能会出现空指针异常,如下所示:

下面的代码抛出空指针异常:

    Boolean val = null;

if (val) {
System.out.println("Will not work incase of null");
}

下面的代码有效:

    if (null != val && val) {
System.out.println("Works");
}

关于BluetoothAdapter.isEnabled() 上的 Java NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56356477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com