Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid bluetooth in a service stops sending data after 561K
    primarykey
    data
    text
    <p>I wrote a service to send sensor data over bluetooth on android. Although I got no errors my C# client stops getting data after I sent exactly 561K data. At this moment it seems like my android continues to send data but my client doesn't get any. After a while, android also stops sending data. I tried different configurations. My program always stops sending data after "Service->Server". I don't get any errors but it stops sending. Here is android program.</p> <pre><code>@Override public synchronized int onStartCommand(Intent intent, int flags, int startId) { Log.i(EXTRA_MESSAGE,"onStartCommand"); if(isRunning) Log.e(EXTRA_MESSAGE, "I am already running"); else { isRunning = true; BluetoothDevice selectedDevice = (BluetoothDevice) intent.getParcelableExtra(EXTRA_MESSAGE); if(selectedDevice == null) {Log.i(EXTRA_MESSAGE,"null it is "); return -1;} connect = new ConnectThread(selectedDevice); connect.start(); mHandler =new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch(msg.what){ case SUCCESS_CONNECT: connected = new ConnectedThread((BluetoothSocket) msg.obj); Toast.makeText(getApplicationContext(), "Connected", 0).show(); //connected.write(("Connected").getBytes()); Log.i(EXTRA_MESSAGE, "we are connected"); isConnected = true; break; case MESSAGE_READ: Toast.makeText(getApplicationContext(), ((byte[]) msg.obj).toString(), 0).show(); break; } } }; mSensor = (SensorManager) getSystemService(SENSOR_SERVICE); sSensor = mSensor.getDefaultSensor(Sensor.TYPE_ORIENTATION); mSensor.registerListener(this, sSensor,SensorManager.SENSOR_DELAY_NORMAL); // sSensor = mSensor.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); // mSensor.registerListener(this, sSensor,SensorManager.SENSOR_DELAY_NORMAL); sSensor = mSensor.getDefaultSensor(Sensor.TYPE_GYROSCOPE); mSensor.registerListener(this, sSensor,SensorManager.SENSOR_DELAY_NORMAL); } return super.onStartCommand(intent, flags, startId); } @Override @Override public void onSensorChanged(SensorEvent event) { Log.i(EXTRA_MESSAGE, "Sensor data arrived"); if(isConnected ) { String toSend = Integer.toString(event.sensor.getType())+ ":" + Long.toString(event.timestamp)+ ":"; for(float f : event.values){ toSend = toSend + Float.toString(f)+":"; } // connected.write(toSend.getBytes()); } } private class ConnectThread extends Thread { private final BluetoothSocket mmSocket; private final BluetoothDevice mmDevice; public ConnectThread(BluetoothDevice device) { // Use a temporary object that is later assigned to mmSocket, // because mmSocket is final BluetoothSocket tmp = null; mmDevice = device; // Get a BluetoothSocket to connect with the given BluetoothDevice try { // MY_UUID is the app's UUID string, also used by the server code tmp = device.createRfcommSocketToServiceRecord(MY_UUID); Log.i(EXTRA_MESSAGE,"connectThread started successfully"); } catch (IOException e) { } mmSocket = tmp; } public void run() { // Cancel discovery because it will slow down the connection try { // Connect the device through the socket. This will block // until it succeeds or throws an exception mmSocket.connect(); Log.i(EXTRA_MESSAGE,"connectThread connect successfully"); } catch (IOException connectException) { // Unable to connect; close the socket and get out Log.i(EXTRA_MESSAGE,"connectThread connect exception"); try { mmSocket.close(); } catch (IOException closeException) { } } // Do work to manage the connection (in a separate thread) mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget(); } /** Will cancel an in-progress connection, and close the socket */ public void cancel() { try { mmSocket.close(); } catch (IOException e) { } } } private class ConnectedThread extends Thread { private final BluetoothSocket mmSocket; private final InputStream mmInStream; private final OutputStream mmOutStream; public boolean shouldContinue = true; int nBytes =0; 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 = socket.getInputStream(); tmpOut = socket.getOutputStream(); Log.i(EXTRA_MESSAGE,"connectedThread sockets"); } catch (IOException e) { } mmInStream = tmpIn; mmOutStream = tmpOut; } public void run() { byte[] buffer; // buffer store for the stream int bytes; // bytes returned from read() // Keep listening to the InputStream until an exception occurs while(shouldContinue) { try { // Read from the InputStream buffer = new byte[1024]; bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI activity Log.e(EXTRA_MESSAGE, " We read"); mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); } catch (IOException e) { break; } } } /* Call this from the main activity to send data to the remote device */ public void write(byte[] bytes) { try { Log.i(EXTRA_MESSAGE,"Service-&gt;Server"); mmOutStream.write(bytes); nBytes += bytes.length; Log.i(EXTRA_MESSAGE,"ok" + String.valueOf(nBytes )); } catch (IOException e) { Log.i(EXTRA_MESSAGE,"exception"); } } /* Call this from the main activity to shutdown the connection */ public void cancel() { try { shouldContinue = false; mmSocket.close(); } catch (IOException e) { } } } </code></pre> <p>Also my c# thread is as follows</p> <pre><code> public void ServerConnectThread() { serverStarted = true; int counter = 0; updateUI("Server started, waiting for clients"); BluetoothListener blueListener = new BluetoothListener(mUUID); blueListener.Start(); BluetoothClient conn = blueListener.AcceptBluetoothClient(); updateUI("Client has connected"); Stream mStream = conn.GetStream(); while (true) { try { //handle server connection byte[] received = new byte[1024]; mStream.Read(received, 0, received.Length); counter += Encoding.ASCII.GetString(received).Length; String[] fields = Encoding.ASCII.GetString(received).Split(':'); double[] data = new double[3]; for (int i = 2; i &lt; 5; i++) data[i-2] = double.Parse(fields[i]); //mSource.notifyObserver(Int16.Parse(fields[0]), data); updateUI(counter.ToString() + " "+ fields[2]+ ":" + fields[3] + ":" + fields[4]); byte[] sent = Encoding.ASCII.GetBytes("Hello World"); mStream.Write(sent, 0, sent.Length); } catch (IOException exception)`enter code here` { updateUI("Client has disconnected!!!!"); } } } </code></pre> <p>One final thing is I've found thousands of 561K android program which sounded a little interesting.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload