1

The application is to automatically connect to a give BT connection with known device ID. Connection with paired device is successful but when I attempt to send data it crashes.

This is the BroadcastReceiver that listens for the paired device (needs work but it works) makes the connection.

receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            devices.add(device);

            for (int a = 0; a < pairedDevices.size(); a++) {
                if (device.getName().equals(pairedDevices.get(a))) {
                    // append
                    Log.i("TAG", "IS Paired " + pairedDevices.get(a));
                    // s = "(Paired)";
                    isBTConnected = true;

                    if (btAdapter.isDiscovering()) {
                        btAdapter.cancelDiscovery();
                    }

                    BluetoothDevice selectedDevice = devices.get(0);
                    ConnectThread connect = new ConnectThread(selectedDevice);
                    connect.start();
                    break;
                }
            }

        } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Log.i("TAG", "ACTION_DISCOVERY_STARTED");

        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            Log.i("TAG", "ACTION_DISCOVERY_FINISHED");
            if (isBTConnected == false) {
                Log.i("TAG", "Unable to connect");

            } else {
                Log.i("TAG", "Connected");
            }
        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            Log.i("TAG", "ACTION_STATE_CHANGED");

            if (btAdapter.getState() == btAdapter.STATE_OFF) {

                Log.i("TAG", "Disconnected ");
                // turnOnBT();

            }
        }

    }
};

This is the ConnectThread that created the socket and sends message to main UI to start.

private class ConnectThread extends Thread {

private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {

    BluetoothSocket tmp = null;
    mmDevice = device;
    Log.d("TAG", "construct");
    // Get a BluetoothSocket to connect with the given BluetoothDevice
    try {
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID); // MY_UUID is the app's UUID string, also used by the server code
    } catch (IOException e) {
        Log.i("TAG", "get socket failed");

    }
    mmSocket = tmp;
}

public void run() {
    // Cancel discovery because it will slow down the connection
    btAdapter.cancelDiscovery();
    Log.i("TAG", "connect - run");
    try {
        // Connect the device through the socket. This will block
        // until it succeeds or throws an exception
        mmSocket.connect();
        Log.i("TAG", "connect - succeeded");
    } catch (IOException connectException) {
        Log.i("TAG", "connect failed");
        // Unable to connect; close the socket and get out
        try {
            mmSocket.close();
        } catch (IOException closeException) {
        }
        return;
    }

    // Do work to manage the connection (in a separate thread)

    mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}

}

This is the Handler that receives success message to start listening

mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        Log.i("TAG", "in handler");
        super.handleMessage(msg);
        switch (msg.what) {
        case SUCCESS_CONNECT:

            ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
            Toast.makeText(getApplicationContext(), "CONNECT", Toast.LENGTH_SHORT).show();
            // String s = "successfully connected";
            // connectedThread.write(s.getBytes());
            Log.i("TAG", "connected");
            break;
        case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
            String string = new String(readBuf);
            Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
            break;
        }
    }

};

Here is the kicker that crashes. Connection is made and socket is created so I am stummped.

mmOutputStream.write(buffer); is the crash

Method where the error occurs

private void SendPacket(int FixtureNumber, int R_value, int G_value, int B_value) {
byte buffer[] = new byte[7];
buffer[0] = ((byte) FixtureNumber);
buffer[1] = ((byte) ProcessDimmer(R_value, Master_value));
buffer[2] = ((byte) ProcessDimmer(G_value, Master_value));
buffer[3] = ((byte) ProcessDimmer(B_value, Master_value));
buffer[4] = ((byte) Tx_Control_Byte);
buffer[5] = ((byte) Tx_Control_Byte_0);
buffer[6] = ((byte) 254);

if (isBTConnected == true) {

    try {
        mmOutputStream.write(buffer);
    } catch (IOException e) {

        e.printStackTrace();
    }

}

}

Logcat

04-02 20:00:02.833: E/InputEventReceiver(6522): Exception dispatching input event.
04-02 20:00:02.833: E/MessageQueue-JNI(6522): Exception in MessageQueue callback: handleReceiveCallback
04-02 20:00:02.843: E/MessageQueue-JNI(6522): java.lang.NullPointerException
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at com.example.simplergb.SimpleRGB_Main.SendPacket(SimpleRGB_Main.java:2277)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at com.example.simplergb.SimpleRGB_Main.onFragmentSliderDoSomething(SimpleRGB_Main.java:1961)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at com.example.simplergb.SliderControls$1.onProgressChanged(SliderControls.java:184)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:655)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.widget.ProgressBar.refreshProgress(ProgressBar.java:667)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.widget.ProgressBar.setProgress(ProgressBar.java:714)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:451)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:372)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.View.dispatchTouchEvent(View.java:7384)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2231)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.View.dispatchPointerEvent(View.java:7564)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.os.MessageQueue.nativePollOnce(Native Method)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.os.MessageQueue.next(MessageQueue.java:132)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.os.Looper.loop(Looper.java:124)
04-02 20:00:02.843: E/MessageQueue-JNI(6522):   at android.app.Activit
04-02 20:00:02.843: D/AndroidRuntime(6522): Shutting down VM
04-02 20:00:02.843: W/dalvikvm(6522): threadid=1: thread exiting with uncaught exception (group=0x415ce700)
04-02 20:00:02.853: E/AndroidRuntime(6522): FATAL EXCEPTION: main
04-02 20:00:02.853: E/AndroidRuntime(6522): java.lang.NullPointerException
04-02 20:00:02.853: E/AndroidRuntime(6522):     at com.example.simplergb.SimpleRGB_Main.SendPacket(SimpleRGB_Main.java:2277)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at com.example.simplergb.SimpleRGB_Main.onFragmentSliderDoSomething(SimpleRGB_Main.java:1961)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at com.example.simplergb.SliderControls$1.onProgressChanged(SliderControls.java:184)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.widget.SeekBar.onProgressRefresh(SeekBar.java:91)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:655)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.widget.ProgressBar.refreshProgress(ProgressBar.java:667)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.widget.ProgressBar.setProgress(ProgressBar.java:714)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:451)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:372)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.View.dispatchTouchEvent(View.java:7384)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2231)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2209)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1910)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.View.dispatchPointerEvent(View.java:7564)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.os.MessageQueue.nativePollOnce(Native Method)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.os.MessageQueue.next(MessageQueue.java:132)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at android.os.Looper.loop(Looper.java:124)
04-02 20:00:02.853: E/AndroidRuntime(6522):     at
04-02 20:00:02.873: D/dalvikvm(6522): GC_FOR_ALLOC freed 1211K, 9% free 12572K/13808K, paused 15ms, total 15ms

This is the ConnectedThread that did not get posted in original post that starts the outputstream.

    private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInputStream;
    private final OutputStream mmOutputStream;

    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 = mmSocket.getInputStream();
            tmpOut = mmSocket.getOutputStream();
        } catch (IOException e) {
        }

        mmInputStream = tmpIn;
        mmOutputStream = tmpOut;
    }

    public void run() {
        byte[] buffer;  
        int bytes;  

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                buffer = new byte[1024];
                bytes = mmInputStream.read(buffer);
                // Send the obtained bytes to the UI activity
                // mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();

            } catch (IOException e) {
                break;
            }
        }
    }

    public void write(byte[] bytes) {
        try {
            mmOutputStream.write(bytes);
        } catch (IOException e) {
        }
    }


    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
        }
    }
}

More test results. I commented out the outputstream and tested the connection.

    private void SendPacket(int FixtureNumber, int R_value, int G_value, int B_value) {
    byte buffer[] = new byte[7];
    buffer[0] = ((byte) FixtureNumber);
    buffer[1] = ((byte) ProcessDimmer(R_value, Master_value));
    buffer[2] = ((byte) ProcessDimmer(G_value, Master_value));
    buffer[3] = ((byte) ProcessDimmer(B_value, Master_value));
    buffer[4] = ((byte) Tx_Control_Byte);
    buffer[5] = ((byte) Tx_Control_Byte_0);
    buffer[6] = ((byte) 254);


     Log.d("TAG", "  Test  " + isBTConnected + "   Socket   " + mmSocket + "  Device  " + mmDevice);

  //        if (isBTConnected == true) {
 // 
 //         try {
 //         
 //             mmOutputStream.write(buffer);
 //         } catch (IOException e) {
 //
 //             e.printStackTrace();
 //         }
 //
 //     }

}

Here is the log from that test method

04-03 17:28:38.605: D/TAG(29004):   Test  true   Socket
android.bluetooth.BluetoothSocket@422f1bb8  Device  00:06:66:4B:45:A9

SendPacket() is run from a Fragment listener. All that code is functioning properly

case 1: {
    if (Fixture_Active_ColorRoll[FixtureNumber] == false) {
        // RGB Value
        // Log.d("TAG", "Scene Data  " + Scene_ColorRoll_Option + " " + FixtureNumber);
        intArrayRed[0] = R_value;
        intArrayGreen[0] = G_value;
        intArrayBlue[0] = B_value;
        SetColorDot(0, R_value, G_value, B_value);
        SendPacket(FixtureNumber, R_value, G_value, B_value);
codeMagic
  • 44,549
  • 13
  • 77
  • 93
Bobby
  • 659
  • 2
  • 13
  • 26
  • Logcat would be very helpful. Filter it by error before posting and it shouldn't be too long – codeMagic Apr 02 '14 at 23:35
  • How should I filter all messages are E, – Bobby Apr 03 '14 at 00:01
  • That's what we needed. What is at line 2277 of `SimpleRGB_Main`? Something there is `null`. It looks like `mmOutputStream` is `null`. I don't see where you try to initialize it so I can't tell you why without seeing that. – codeMagic Apr 03 '14 at 00:37
  • mmOutputStream.write(buffer); line 2277 – Bobby Apr 03 '14 at 00:44
  • Yep, see my edited comment – codeMagic Apr 03 '14 at 00:45
  • mmOutputStream was initialized in private class ConnectedThread extends Thread. Am I missing something ? – Bobby Apr 03 '14 at 00:59
  • I posted the connectedthread, still at a loss why mmOutputstream= null. Anyone see the issue ? – Bobby Apr 03 '14 at 17:42
  • Where is `SendPacket()` called? – codeMagic Apr 03 '14 at 17:45
  • inside the main activity: public class SimpleRGB_Main extends FragmentActivity implements SliderControls.FragmentAListener, ButtonControls.FragmentBListener – Bobby Apr 03 '14 at 18:47
  • Have you verified that the `ConnectThread` class gets called to initialize that variable **before** the `SendPacket()` method is called? – codeMagic Apr 03 '14 at 18:49
  • Yes, I added a Log and confirmed its called before Sendpacket. – Bobby Apr 03 '14 at 18:58
  • I posted more information on the connection. See the test code and logcat information. I am stupped on the null error – Bobby Apr 03 '14 at 21:34
  • Have you checked that it isn't `null` in the `run()` of `ConnectThread `? Also, is your `receiver` in your `Activity`? And what code do you use to call `SendPacket()`? – codeMagic Apr 03 '14 at 22:10
  • "Also, is your receiver in your Activity", no currently this is send only. I do receive confirmation on the paired device and that receiver is inside the activity and functioning properly. Code above see "Handler" Posted code on Sendpacket send from fragment listener. – Bobby Apr 03 '14 at 22:24
  • The receive is commented out but it is located in the activity mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); – Bobby Apr 03 '14 at 22:30
  • I have several app that use the bluetooth connection and all work fine so I have narrowed the issue to the connection portion of the code. This application uses a Class to connect and this might be why I cannot access the outputstream. SendPAcket is in the activity. There is an outputStream.write(buffer) inside the class but I am not sure how to access. The code has been posted above. – Bobby Apr 05 '14 at 00:23
  • Everything that I can think of. I am new to Android. Its just a matter of time before I get it solved and then it will probably be obvious. A direction on how to proceed would be helpful. – Bobby Apr 05 '14 at 15:08
  • Do work to manage the connection (in a separate thread) – Bobby Apr 05 '14 at 17:14
  • Comment out that initialization in the `Handler` and see if it doesn't crash. – codeMagic Apr 05 '14 at 17:33
  • // BluetoothDevice selectedDevice = devices.get(0); //ConnectThread connect = new ConnectThread(selectedDevice); // connect.start(); No connection is made to BT – Bobby Apr 05 '14 at 17:52
  • I did the comment out but this is needed to make the connection. No crash but no connection ether. – Bobby Apr 05 '14 at 17:54
  • I have found several other post that use the same connect and connected threads. http://stackoverflow.com/questions/16265504/uuid-bluetooth-and-android-devices. Is it possible that its my BT receiver that is getting corrupt during pairing. – Bobby Apr 06 '14 at 01:13
  • Set a breakpoint and check the socket you are trying to send in the handler. That must be null. – codeMagic Apr 06 '14 at 01:27
  • mmSocket is null. breakpoint set at mmOutputStream.write(buffer); – Bobby Apr 06 '14 at 01:41
  • Right but we knew tgs that from the logcat. It has to be something that happens before that. That's why I said check the socket you send to the Thread – codeMagic Apr 06 '14 at 01:48
  • Its time to scrap and go another direction. Thank you for all your help. – Bobby Apr 06 '14 at 01:52

0 Answers0