简体   繁体   English

Android的蓝牙。 空指针

[英]Android-Bluetooth. Null pointer

I am beginner in Objective-programming and I don't know what I should do. 我是目标编程的初学者,但我不知道该怎么做。 I am getting some errors with null pointer in openBT method. 我在openBT方法中使用空指针时遇到一些错误。 Below is my logcat and the code. 下面是我的logcat和代码。

MainActivity.java: MainActivity.java:

public class MainActivity extends ActionBarActivity implements DrawingViewInterface {

private DrawingView drawView;
private ImageButton currPaint;


BluetoothAdapter mBluetoothAdapter;        
BluetoothSocket mmSocket;                  
BluetoothDevice mmDevice;                  
OutputStream mmOutputStream;               
InputStream mmInputStream;                 
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);    

    drawView = (DrawingView)findViewById(R.id.drawing);    

    drawView.setViewListener(this);   



}



public void findBT(View view){          

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if(mBluetoothAdapter==null){                            
        Toast.makeText(getApplicationContext(),"Nie wykryto BT", Toast.LENGTH_LONG).show();
    }

    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("MattsBlueTooth"))
            {
                mmDevice = device;
                break;
            }
        }
    }

    Toast.makeText(getApplicationContext(),"Znaleziono BT", Toast.LENGTH_LONG).show();

}



public void openBT(View view) throws IOException
{
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();


    Toast.makeText(getApplicationContext(),"Bluetooth jest otwarty", Toast.LENGTH_LONG).show();

    sendData();                     


}

void sendData() throws IOException
{
    String msg = "chuj";
    msg += "\n";
    mmOutputStream.write(msg.getBytes());
    Toast.makeText(getApplicationContext(),"Dane zostały przesłane", Toast.LENGTH_LONG).show();
}


}

Logcat: logcat的:

    03-31 01:00:13.795  18767-18767/com.example.aplikacja3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.aplikacja3, PID: 18767
    java.lang.IllegalStateException: Could not execute method of the activity
            com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(java.util.UUID)' on a null object reference
            at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:157)
            at java.lang.reflect.Method.invoke(Native Method)
            
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-31 01:35:21.665  20598-20598/com.example.aplikacja3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.aplikacja3, PID: 20598
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4012)

            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)

            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(java.util.UUID)' on a null object reference
            at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:157)
            at java.lang.reflect.Method.invoke(Native Method)
            
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Anyone can help? 有人可以帮忙吗?

EDIT: 编辑:

Thank you! 谢谢! You help me. 你帮我。 But my problem was different. 但是我的问题不同。 I forget change name of my device in "FindBT". 我忘记了在“ FindBT”中更改设备的名称。 Ehh.. EHH ..

But now I have other problem. 但是现在我还有其他问题。 I try connect with my computer but when I try send something I get this: 我尝试与计算机连接,但是当我尝试发送内容时,我得到了以下信息:

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4012)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
            at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:517)
            at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:528)
            at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
            at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:160)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

In accordace with your logcat, your are calling the method createRfcommSocketToServiceRecord from a null object, that is the mmDevice. 与您的logcat一致,您正在从一个空对象createRfcommSocketToServiceRecord调用方法createRfcommSocketToServiceRecord。 The solution is call the openBT only after mmDevice to have a non null value. 该解决方案仅在mmDevice之后具有非空值的情况下才调用openBT。 To avoid the crash, you can put this if condition, as the first line of your openBT method: 为避免崩溃,您可以将此if条件作为openBT方法的第一行:

if (mmDevice != null) {
    // rest of your method here

You'll have to adapt the device name or remove the condition at all in the findBT method, as long as your BT device doesn't identify itself as MattsBlueTooth : 只要您的BT设备没有将其自身标识为MattsBlueTooth ,您就必须在findBT方法中修改设备名称或完全删除条件:

if(device.getName().equals("MattsBlueTooth"))
{
    mmDevice = device;
    break;
}

Still, you should apply the check suggested by @androidevil in order to avoid the crash in case the relevant bluetooth device is not paired. 不过,如果相关的蓝牙设备未配对,则应应用@androidevil建议的检查以避免崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM