0

So here's how it is supposed to go.

The Host will make his/her device discoverable then choose the type of game board that will be loaded. The Client will connect to the Host device and both the devices will be connected. The type of game board that the Host chose earlier should be loaded on both the devices.

Here is my code, and so far it only causes an error

BluetoothActivity.java

case MESSAGE_DEVICE_NAME:
    // save the connected device's name
    mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
    Toast.makeText(TwoPlayerActivityBluetooth.this, "Connected to " + mConnectedDeviceName, Toast.LENGTH_SHORT).show();

    /* If the device chose a game board, then send*/
    if (GBChoice != 0)
    {
        sendGamepiece(GBChoice);
    }

    /* If the device didn't choose a game board, then receive */
    else
    {
        byte[] readBuf2 = (byte[]) msg.obj;
        String readMessage2 = new String(readBuf2, 0, msg.arg1);

        /* Save the received data to a variable */
        ChosenGB = readMessage2;
    }


    /* If the sent game board choice and received game board choice is both 1, then load the game board1*/
    if (GBChoice == 1 && ChosenGB == 1)
    {
        loadGameBoard1();
    }
    break;

BluetoothService.java

        public void run()
        {
            readGamepiece();
            readLocation();
        }

        public void readGamepiece()
        {

            byte[] buffer2 = new byte[1024];
            int bytes;

        while(true)
        {
            try
            {
                bytes = mInStream.read(buffer2);
            mHandler.obtainMessage(BluetoothActivity.MESSAGE_DEVICE_NAME, bytes, -1, buffer2).sendToTarget();
            }
            catch(IOException e)
            {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }

    }

    public void writeGamepiece(byte[] buffer2)
    {
        try
        {
            mOutStream.write(buffer2);
            mHandler.obtainMessage(BluetoothActivity.MESSAGE_DEVICE_NAME, -1 , -1 , buffer2).sendToTarget();

        }
        catch(IOException e)
        {
        Log.e(TAG, "Exception during write", e);
        }
    }


    public void readLocation()
    {

        byte[] buffer = new byte[1024];
        int bytes;

        while(true)
        {
            try
            {
                bytes = mInStream.read(buffer);
                mHandler.obtainMessage(BluetoothActivity.PLAYER_LOCATION_READ, bytes, -1, buffer).sendToTarget();
            }
            catch(IOException e)
            {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }


    public void writeLocation(byte[] buffer)
    {
        try
        {
            mOutStream.write(buffer);
            mHandler.obtainMessage(BluetoothActivity.PLAYER_LOCATION_WRITE, -1 , -1 , buffer).sendToTarget();
        }
        catch(IOException e)
        {
            Log.e(TAG, "Exception during write", e);
        }
    }

Error:

FATAL EXCEPTION: mainProcess: com.example.try34.midproject, PID: 3829
java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes()' on a null object reference
at com.example.try34.midproject.view.BluetoothActivity.sendGamepiece(BluetoothActivity.java:1024)
at com.example.try34.midproject.view.BluetoothActivity.access$1800(BluetoothActivity.java:44)
at com.example.try34.midproject.view.BluetoothActivity$9.handleMessage(BluetoothActivity.java:741)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

0 Answers0