BluetoothSPPLibrary
BluetoothSPPLibrary copied to clipboard
Failure delivering result onActivityResult
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
Having exactly the same issue.
I also have this problem
same here
As far as I remember the thing was to turn on the location service as well to make it work properly.
I just switched to another lib that worked perfectly at first try
@animalillo and what is that lib?
I have the same issue
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.
@bboydflo this is the library i'm using. https://github.com/MacroYau/Blue2Serial
@animalillo Thanks. I will give it a try!
@bboydflo it works?