Autore Topic: Invio/Ricezione stringhe tramite Bluetooth.  (Letto 975 volte)

Offline MaxPower

  • Nuovo arrivato
  • *
  • Post: 2
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Galaxy note 3
  • Sistema operativo:
    windows 8.1
Invio/Ricezione stringhe tramite Bluetooth.
« il: 27 Agosto 2014, 12:51:53 CEST »
0
Salve,
Sto creando una applicazione android il cui scopo è di inviare stringhe tramite il bluetooth.
Ho creato una applicazione server, la quale è in ascolto per ricevere la richiesta di connessione da parte del client.
La connessione va a buon fine (seguendo la guida ufficiale android) ma quando tento di inviare, dall'applicazione client, una write all'applicazione server, il client crasha.

Questa classe è quella di cui creo l'oggetto dopo aver effettuato la connessione.
E contenuto anche il metodo write che dovrebbe scrivere sulla socket, ma come già detto l'app crasha quando invoco il metodo write.

Codice (Java): [Seleziona]
package com.example.exo;

import java.io.IOException;
import java.io.InputStream;
//import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.widget.Toast;

public class Connected extends Activity {

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                // TODO Auto-generated method stub
        }

        private  BluetoothSocket mmSocket;
        private  InputStream mmInStream; //per parte server
        private OutputStream mmOutStream;

        public Connected(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 = socket.getInputStream();
                        mmOutStream = socket.getOutputStream();
                } catch (IOException e) { }

                mmInStream = tmpIn;
                mmOutStream = tmpOut;
               
        }
       
        public BluetoothSocket getSocket(){
               
                return mmSocket;
        }
       
       
         public void write(String value) {
                try {
                    mmOutStream.write(value.getBytes());
                } catch (IOException e) { }
            }
         

        /* Call this from the main activity to shutdown the connection */
        public void cancel(BluetoothSocket mmSocket) {
                try {
                        mmSocket.close();
                } catch (IOException e) { }
        }
}


Il log cat mostra degli errori dopo l'avvenuto click sul bottone che invoca il metodo write.

Codice: [Seleziona]
08-27 14:47:12.846: W/ApplicationPackageManager(18566): getCSCPackageItemText()
08-27 14:47:12.846: I/PersonaManager(18566): getPersonaService() name persona_policy
08-27 14:47:12.926: D/AbsListView(18566): Get MotionRecognitionManager
08-27 14:47:12.976: D/AbsListView(18566): onVisibilityChanged() is called, visibility : 4
08-27 14:47:12.976: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:12.986: D/AbsListView(18566): onVisibilityChanged() is called, visibility : 0
08-27 14:47:12.986: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:12.996: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:13.016: I/Adreno-EGL(18566): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
08-27 14:47:13.016: I/Adreno-EGL(18566): OpenGL ES Shader Compiler Version: E031.24.00.08+13
08-27 14:47:13.016: I/Adreno-EGL(18566): Build Date: 03/28/14 Fri
08-27 14:47:13.016: I/Adreno-EGL(18566): Local Branch: 0328_AU200_patches
08-27 14:47:13.016: I/Adreno-EGL(18566): Remote Branch:
08-27 14:47:13.016: I/Adreno-EGL(18566): Local Patches:
08-27 14:47:13.016: I/Adreno-EGL(18566): Reconstruct Branch:
08-27 14:47:13.056: D/OpenGLRenderer(18566): Enabling debug mode 0
08-27 14:47:13.056: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:13.096: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:13.116: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:15.746: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:15.876: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:16.626: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:18.496: D/BluetoothUtils(18566): isSocketAllowedBySecurityPolicy start : device null
08-27 14:47:18.496: D/BluetoothSocket(18566): GlobalConfig.GLOBALCONFIG_BT_IT_POLICY_FEATURE = true
08-27 14:47:18.496: W/BluetoothAdapter(18566): getBluetoothService() called with no BluetoothManagerCallback
08-27 14:47:18.506: D/BluetoothSocket(18566): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}
08-27 14:47:22.006: I/Choreographer(18566): Skipped 212 frames!  The application may be doing too much work on its main thread.
08-27 14:47:22.026: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:22.036: W/ApplicationPackageManager(18566): getCSCPackageItemText()
08-27 14:47:22.066: D/ProgressBar(18566): setProgressDrawable drawableHeight = 36
08-27 14:47:22.086: D/AbsSeekBar(18566): AbsSeekBar Constructor: misSeebarAnimationAvailable = true
08-27 14:47:22.086: D/ProgressBar(18566): setProgressDrawable drawableHeight = 36
08-27 14:47:22.096: D/AbsSeekBar(18566): AbsSeekBar Constructor: misSeebarAnimationAvailable = true
08-27 14:47:22.106: D/ProgressBar(18566): setProgressDrawable drawableHeight = 36
08-27 14:47:22.116: D/AbsSeekBar(18566): AbsSeekBar Constructor: misSeebarAnimationAvailable = true
08-27 14:47:22.116: D/ProgressBar(18566): setProgressDrawable drawableHeight = 36
08-27 14:47:22.126: D/AbsSeekBar(18566): AbsSeekBar Constructor: misSeebarAnimationAvailable = true
08-27 14:47:22.136: D/ProgressBar(18566): setProgressDrawable drawableHeight = 36
08-27 14:47:22.136: D/AbsSeekBar(18566): AbsSeekBar Constructor: misSeebarAnimationAvailable = true
08-27 14:47:22.146: D/ProgressBar(18566): setProgressDrawable drawableHeight = 36
08-27 14:47:22.156: D/AbsSeekBar(18566): AbsSeekBar Constructor: misSeebarAnimationAvailable = true
08-27 14:47:22.166: D/BluetoothUtils(18566): isSocketAllowedBySecurityPolicy start : device null
08-27 14:47:22.166: D/BluetoothSocket(18566): GlobalConfig.GLOBALCONFIG_BT_IT_POLICY_FEATURE = true
08-27 14:47:22.166: W/BluetoothAdapter(18566): getBluetoothService() called with no BluetoothManagerCallback
08-27 14:47:22.176: D/BluetoothSocket(18566): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[65]}
08-27 14:47:22.456: D/ProgressBar(18566): updateDrawableBounds: left = 0
08-27 14:47:22.456: D/ProgressBar(18566): updateDrawableBounds: top = 0
08-27 14:47:22.456: D/ProgressBar(18566): updateDrawableBounds: right = 672
08-27 14:47:22.456: D/ProgressBar(18566): updateDrawableBounds: bottom = 96
08-27 14:47:22.456: D/ProgressBar(18566): updateDrawableBounds: mProgressDrawable.setBounds()
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: left = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: top = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: right = 672
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: bottom = 96
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: mProgressDrawable.setBounds()
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: left = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: top = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: right = 672
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: bottom = 96
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: mProgressDrawable.setBounds()
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: left = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: top = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: right = 672
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: bottom = 96
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: mProgressDrawable.setBounds()
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: left = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: top = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: right = 672
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: bottom = 96
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: mProgressDrawable.setBounds()
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: left = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: top = 0
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: right = 672
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: bottom = 96
08-27 14:47:22.466: D/ProgressBar(18566): updateDrawableBounds: mProgressDrawable.setBounds()
08-27 14:47:22.596: D/AbsListView(18566): onVisibilityChanged() is called, visibility : 4
08-27 14:47:22.596: D/AbsListView(18566): unregisterIRListener() is called
08-27 14:47:26.456: D/AndroidRuntime(18566): Shutting down VM
08-27 14:47:26.456: W/dalvikvm(18566): threadid=1: thread exiting with uncaught exception (group=0x41761da0)
08-27 14:47:26.456: E/AndroidRuntime(18566): FATAL EXCEPTION: main
08-27 14:47:26.456: E/AndroidRuntime(18566): Process: com.example.exo, PID: 18566
08-27 14:47:26.456: E/AndroidRuntime(18566): java.lang.NullPointerException
08-27 14:47:26.456: E/AndroidRuntime(18566):         at com.example.exo.Connected.write(Connected.java:52)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at com.example.exo.Filters$14.onProgressChanged(Filters.java:225)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.widget.SeekBar.onProgressRefresh(SeekBar.java:96)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:684)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.widget.ProgressBar.refreshProgress(ProgressBar.java:709)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.widget.ProgressBar.setProgress(ProgressBar.java:799)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.widget.ProgressBar.setProgress(ProgressBar.java:748)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at com.example.exo.Filters$2.onClick(Filters.java:81)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.view.View.performClick(View.java:4630)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.view.View$PerformClick.run(View.java:19339)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.os.Handler.handleCallback(Handler.java:733)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.os.Handler.dispatchMessage(Handler.java:95)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.os.Looper.loop(Looper.java:157)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at android.app.ActivityThread.main(ActivityThread.java:5335)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at java.lang.reflect.Method.invoke(Method.java:515)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-27 14:47:26.456: E/AndroidRuntime(18566):         at dalvik.system.NativeStart.main(Native Method)



Non ho idea di come risolvere il problema quindi chiedo il vostro aiuto.
Grazie.



Offline bradipao

  • Moderatore globale
  • Utente storico
  • *****
  • Post: 4043
  • keep it simple
  • Respect: +567
    • Github
    • Google+
    • bradipao
    • Mostra profilo
  • Dispositivo Android:
    Nexus 5
  • Play Store ID:
    Bradipao
  • Sistema operativo:
    W7
Re:Invio/Ricezione stringhe tramite Bluetooth.
« Risposta #1 il: 27 Agosto 2014, 15:11:17 CEST »
0
Il logcat ti dice che almeno uno tra mmOutStream e value è NULL.

A parte trovare la causa, io farei precedere la riga incriminata da un controllo per il NULL, cosa che va sempre bene.
NON rispondo a domande nei messaggi privati
Bradipao @ Play Store

Offline MaxPower

  • Nuovo arrivato
  • *
  • Post: 2
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Galaxy note 3
  • Sistema operativo:
    windows 8.1
Re:Invio/Ricezione stringhe tramite Bluetooth.
« Risposta #2 il: 28 Agosto 2014, 10:41:18 CEST »
0
Grazie per la risposta il problema era proprio il valore null dell'outputstream!