尝试创建套接字时获取空指针异常?

时间:2017-02-06 13:58:22

标签: android sockets bluetooth

我正在尝试在android和arduino之间进行通信。我打开蓝牙并手动配对设备。我从audrino接收数据并在textviews中显示。以下是我的代码

    private static UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");
//mac address of bluetooth module in audrino
    private static String address = "20:16:03:21:70:44";
    private BluetoothAdapter btAdapter = null;
        private BluetoothSocket btSocket = null;
     private final static int REQUEST_ENABLE_BT = 1;

           public void syncDevice(View view) throws  IOException {
                    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                    if (!mBluetoothAdapter.isEnabled()) {
                        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                    }
        //getting error in this line
                    BluetoothDevice device = btAdapter.getRemoteDevice(address);
                    btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                    btSocket.connect();
                    ConnectedThread b1 = new ConnectedThread(btSocket);
                }
                public class ConnectedThread extends Thread {
                    private final BluetoothSocket mmSocket;
                    private final InputStream mmInStream;
                    private final OutputStream mmOutStream;
                    private byte[] mmBuffer; // mmBuffer store for the stream

                    public ConnectedThread(BluetoothSocket socket) {
                        mmSocket = socket;
                        InputStream tmpIn = null;
                        OutputStream tmpOut = null;

                        // Get the input and output streams; using temp objects because
                        // member streams are final.
                        try {
                            tmpIn = socket.getInputStream();
                        } catch (IOException e) {
                            ///Log.e(TAG, "Error occurred when creating input stream", e);
                        }
                        try {
                            tmpOut = socket.getOutputStream();
                        } catch (IOException e) {
                            // Log.e(TAG, "Error occurred when creating output stream", e);
                        }

                        mmInStream = tmpIn;
                        mmOutStream = tmpOut;
                    }

                    public void run() {
                        mmBuffer = new byte[1024];
                        int numBytes; // bytes returned from read()

                        // Keep listening to the InputStream until an exception occurs.
                        while (true) {
                            try {
                                // Read from the InputStream.
                                numBytes = mmInStream.read(mmBuffer);
                                // Send the obtained bytes to the UI activity.
                                Message readMsg = mHandler.obtainMessage(
                                   MessageConstants.MESSAGE_READ, numBytes, -1,
                                        mmBuffer);
                                readMsg.sendToTarget();

                            } catch (IOException e) {

                                break;
                            }
                        }
                    }
                }

错误即将到来

java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)

0 个答案:

没有答案