BluetoothSPPLibrary icon indicating copy to clipboard operation
BluetoothSPPLibrary copied to clipboard

Failure delivering result onActivityResult

Open shenoudab opened this issue 9 years ago • 11 comments

Hi,

I got an Error and App Crash as following:

`public class BluetoothSPPActivity extends AppCompatActivity {

private BluetoothSPP bt;
public static int REQUEST_BLUETOOTH = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_spp);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); // fab.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) // .setAction("Action", null).show(); // } // });

    bt = new BluetoothSPP(getApplicationContext());
    final TextView receiveSerial = (TextView) findViewById(R.id.recevie_serial);

    bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
        public void onDataReceived(byte[] data, String message) {
            receiveSerial.setText("");
            receiveSerial.append(message);
        }
    });

    bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
        public void onDeviceConnected(String name, String address) {
            // Do something when successfully connected
        }

        public void onDeviceDisconnected() {
            // Do something when connection was disconnected
        }

        public void onDeviceConnectionFailed() {
            // Do something when connection failed
        }
    });

    bt.setBluetoothStateListener(new BluetoothSPP.BluetoothStateListener() {
        public void onServiceStateChanged(int state) {
            if (state == BluetoothState.STATE_CONNECTED) {
                // Do something when successfully connected
            } else if (state == BluetoothState.STATE_CONNECTING) {
                // Do something while connecting
            } else if (state == BluetoothState.STATE_LISTEN) {
                // Do something when device is waiting for connection
            } else if (state == BluetoothState.STATE_NONE) {
                // Do something when device don't have any connection
            }
        }
    });

    Intent intent = new Intent(getApplicationContext(), DeviceList.class);
    startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
        if (resultCode == Activity.RESULT_OK)
            bt.connect(data);
    } else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
        if (resultCode == Activity.RESULT_OK) {
            bt.setupService();
            bt.startService(BluetoothState.DEVICE_OTHER);
        } else {
            // Do something if user doesn't choose any device (Pressed back)
        }
    }
}

public void onStart() {
    super.onStart();
    if (!bt.isBluetoothEnabled()) {
        Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBT, REQUEST_BLUETOOTH);
    } else {
        // bt.startService(BluetoothState.DEVICE_OTHER);
    }
}

}`

Caused by: java.lang.NullPointerException at app.akexorcist.bluetotohspp.library.BluetoothSPP.connect(BluetoothSPP.java:230) at com._._*.BluetoothSPPActivity.onActivityResult(BluetoothSPPActivity.java:83) at android.app.Activity.dispatchActivityResult(Activity.java:5385)

Thanks

shenoudab avatar Mar 07 '16 11:03 shenoudab

Having exactly the same issue.

quant-eagle avatar Apr 16 '16 12:04 quant-eagle

I also have this problem

ClaudeXin avatar Jun 23 '16 11:06 ClaudeXin

same here

animalillo avatar Sep 28 '16 12:09 animalillo

As far as I remember the thing was to turn on the location service as well to make it work properly.

quant-eagle avatar Sep 28 '16 13:09 quant-eagle

I just switched to another lib that worked perfectly at first try

animalillo avatar Sep 30 '16 23:09 animalillo

@animalillo and what is that lib?

bboydflo avatar Oct 19 '16 10:10 bboydflo

I have the same issue

ghost avatar Oct 31 '16 01:10 ghost

In my case, the problem occurs because the mChatService object is null inside the method app.akexorcist.bluetotohspp.library.BluetoothSPP.connect(Intent data). The method app.akexorcist.bluetotohspp.library.BluetoothSPP.setupService() is responsible for instantiating the mChatService object.

ghost avatar Oct 31 '16 01:10 ghost

@bboydflo this is the library i'm using. https://github.com/MacroYau/Blue2Serial

animalillo avatar Nov 05 '16 13:11 animalillo

@animalillo Thanks. I will give it a try!

bboydflo avatar Nov 07 '16 11:11 bboydflo

@bboydflo it works?

ChristianHRezende avatar Mar 09 '18 20:03 ChristianHRezende