Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. @Override
  2. protected void onHandleIntent(Intent intent) {
  3. device = (BluetoothDevice) intent.getExtras().get("device");
  4.  
  5. if (device != null) {
  6. Log.v("DeviceManager", "Beginning attempts to communicate to: " + device.getName());
  7. try {
  8. BluetoothSocket socket = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
  9. outputStream = socket.getOutputStream();
  10. inputStream = socket.getInputStream();
  11.  
  12. while (Utils.appIsInForeground && device != null) {
  13. if (outputStream == null){
  14. Log.e("DeviceManager", "Looks like the outputStream is null...");
  15. if (inputStream == null){
  16. Log.e("DeviceManager", "And input stream is null, are you even connected to the device?");
  17. }else{
  18. Log.e("DeviceManager", "Although strangely, input stream has been set.");
  19. }
  20. }else{
  21. String myString = "This is a string!";
  22. byte[] myByteArray = myString.getBytes("UTF-8");
  23. //Crashes here:
  24. outputStream.write(myByteArray);
  25. }
  26. Thread.sleep(1000);
  27. }
  28.  
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. Log.e("DeviceManager", "Can't create a socket to the bluetooth device.");
  32. } catch (InterruptedException e) {
  33. e.printStackTrace();
  34. Log.e("DeviceManager", "The sleep thread was interrupted.");
  35. } catch (NullPointerException e) {
  36. e.printStackTrace();
  37. Log.e("DeviceManager", "A null pointer exception, this is probably not good.");
  38. }
  39. }else {
  40. Log.e("DeviceManager", "The Device Manager has been passed a null object instead of a device.");
  41. }
  42. }
  43.  
  44. 03-23 15:55:12.134 3042-3171/com.name.app W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
  45. 03-23 15:55:12.135 3042-3171/com.name.app W/System.err: at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:553)
  46. 03-23 15:55:12.136 3042-3171/com.name.app W/System.err: at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
  47. 03-23 15:55:12.136 3042-3171/com.name.app W/System.err: at java.io.OutputStream.write(OutputStream.java:75)
  48. 03-23 15:55:12.137 3042-3171/com.name.app W/System.err: at com.name.app.service.DeviceManager.onHandleIntent(DeviceManager.java:73)
  49. 03-23 15:55:12.138 3042-3171/com.name.app W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:67)
  50. 03-23 15:55:12.138 3042-3171/com.name.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
  51. 03-23 15:55:12.139 3042-3171/com.name.app W/System.err: at android.os.Looper.loop(Looper.java:154)
  52. 03-23 15:55:12.139 3042-3171/com.name.app W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
  53.  
  54. myByteArray = {byte[17]@4799}
  55. this = {DeviceManager@4698}
  56. intent = {Intent@4705} "Intent { cmp=com.name.app/.service.DeviceManager (has extras) }"
  57. socket = {BluetoothSocket@4706}
  58. myString = "This is a string!"
  59. myByteArray = {byte[17]@4799}
  60. outputStream = {BluetoothOutputStream@4743}
  61.  
  62. outputStream.write(myByteArray, 0, myByteArray.length);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement