I've been following these two posts SPP Server and Client and this stackoverflow post. I have the server running on a Linux VM and the Android app running on a Samsung Galaxy S6. When I run the server code in Intellij, it says:
"Server Started. Waiting for clients to connect".
When I run the Android app, I get the following Alert box saying:
"Fatal Error. In OnResume()and an exception occurred during write: socket closed.
Check that the SPP UUID: 00001101-0000-1000-8000-00805F9B34FB exists on server. Press OK to exit.
Why is this happening and how can I resolve it so the server will connect and receive a string from the Android app?
SPP Server Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.bluetooth.*;
import javax.microedition.io.*;
public class SampleSPPServer {
//start server
private void startServer() throws IOException{
//Create a UUID for SPP
UUID uuid = new UUID("0000110100001000800000805F9B34FB", false);
//Create the servicve url
String connectionString = “btspp://localhost:” + uuid +”;name=Sample
SPP Server”;
//open server url
StreamConnectionNotifier streamConnNotifier =
(StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println(“\nServer Started. Waiting for clients to connect…”);
StreamConnection connection=streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println(“Remote device address: “+dev.getBluetoothAddress());
System.out.println(“Remote device name: “+dev.getFriendlyName(true));
//read string from spp client
InputStream inStream=connection.openInputStream();
BufferedReader bReader=new BufferedReader(new
InputStreamReader(inStream));
String lineRead=bReader.readLine();
System.out.println(lineRead);
//send response to spp client
OutputStream outStream=connection.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
pWriter.write(“Response String from SPP Server\r\n”);
pWriter.flush();
pWriter.close();
streamConnNotifier.close();
}
public static void main(String[] args) throws IOException {
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println(“Address: “+localDevice.getBluetoothAddress());
System.out.println(“Name: “+localDevice.getFriendlyName());
SampleSPPServer sampleSPPServer=new SampleSPPServer();
sampleSPPServer.startServer();
}
}
Android Client Code:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.UUID;
public class BluetoothClient extends AppCompatActivity {
TextView out;
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
// Well known SPP UUID
private static final UUID MY_UUID =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// Insert your server's MAC address
private static String address = "00:10:60:AA:B9:B2";
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_client);
out = (TextView) findViewById(R.id.out);
out.append("\n...In onCreate()...");
btAdapter = BluetoothAdapter.getDefaultAdapter();
CheckBTState();
}
public void onStart() {
super.onStart();
out.append("\n...In onStart()...");
}
public void onResume() {
super.onResume();
out.append("\n...In onResume...\n...Attempting client connect...");
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
// A MAC address, which we got above.
// A Service ID or UUID. In this case we are using the
// UUID for SPP.
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
AlertBox("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
}
// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
try {
btSocket.connect();
out.append("\n...Connection established and data link opened...");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
AlertBox("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
out.append("\n...Sending message to server...");
String message = "Hello from Android.\n";
out.append("\n\n...The message that we will send to the server is: "+message);
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
byte[] msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
if (address.equals("00:00:00:00:00:00"))
msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
AlertBox("Fatal Error", msg);
}
}
public void onPause() {
super.onPause();
//out.append("\n...Hello\n");
InputStream inStream;
try {
inStream = btSocket.getInputStream();
BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
String lineRead=bReader.readLine();
out.append("\n..."+lineRead+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.append("\n...In onPause()...");
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
AlertBox("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
}
}
try {
btSocket.close();
} catch (IOException e2) {
AlertBox("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
}
public void onStop() {
super.onStop();
out.append("\n...In onStop()...");
}
public void onDestroy() {
super.onDestroy();
out.append("\n...In onDestroy()...");
}
private void CheckBTState() {
// Check for Bluetooth support and then check to make sure it is turned on
// Emulator doesn't support Bluetooth and will return null
if(btAdapter==null) {
AlertBox("Fatal Error", "Bluetooth Not supported. Aborting.");
} else {
if (btAdapter.isEnabled()) {
out.append("\n...Bluetooth is enabled...");
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
public void AlertBox( String title, String message ){
new AlertDialog.Builder(this)
.setTitle( title )
.setMessage( message + " Press OK to exit." )
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).show();
}
}
Android Studio Logcat:
android.view.WindowLeaked: Activity com.example.toby.btclientapp.BluetoothClient has leaked window com.android.internal.policy.PhoneWindow$DecorView{b73d40d V.E...... R.....I. 0,0-1368,1249} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:569)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:350)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
at com.example.toby.btclientapp.BluetoothClient.AlertBox(BluetoothClient.java:181)
at com.example.toby.btclientapp.BluetoothClient.onResume(BluetoothClient.java:107)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1286)
at android.app.Activity.performResume(Activity.java:6987)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4145)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3361)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-26 13:09:21.574 3857-3857/? E/WindowManager: android.view.WindowLeaked: Activity com.example.toby.btclientapp.BluetoothClient has leaked window com.android.internal.policy.PhoneWindow$DecorView{9723488 V.E...... R....... 0,0-1368,1249} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:569)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:350)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
at com.example.toby.btclientapp.BluetoothClient.AlertBox(BluetoothClient.java:181)
at com.example.toby.btclientapp.BluetoothClient.onResume(BluetoothClient.java:107)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1286)
at android.app.Activity.performResume(Activity.java:6987)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4145)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4250)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
07-26 13:09:21.574 3857-3857/? E/WindowManager: android.view.WindowLeaked: Activity com.example.toby.btclientapp.BluetoothClient has leaked window com.android.internal.policy.PhoneWindow$DecorView{cea821b V.E...... R....... 0,0-1368,799} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:569)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:350)
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:955)
at com.example.toby.btclientapp.BluetoothClient.AlertBox(BluetoothClient.java:181)
at com.example.toby.btclientapp.BluetoothClient.onPause(BluetoothClient.java:135)
at android.app.Activity.performPause(Activity.java:7033)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1339)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4577)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4550)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4525)
at android.app.ActivityThread.access$1300(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1813)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I needed to change the MAC address of the Bluetooth adapter. It works now.
Related
This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 5 years ago.
I want to call a POST request, to send some geo information from my android device to my server.
My server use PHP and I want use a php script to save all incoming post requests in my database. My php script works fine when I tried it with curl, but when I want to send some information from my android device I get some network errors.
Here is my error log
12-11 12:08:02.871 10241-10241/local.example.markus.geoapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: local.example.markus.geoapp, PID: 10241
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
at java.net.InetAddress.getAllByName(InetAddress.java:787)
at com.android.okhttp.Dns$1.lookup(Dns.java:39)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.getOutputStream(TrackedHttpURLConnection.java:288)
at com.android.tools.profiler.support.network.httpurl.HttpURLConnection$.getOutputStream(HttpURLConnection$.java:212)
at local.example.markus.geoapp.MapsListener.sendPost(MapsListener.java:121)
at local.example.markus.geoapp.MapsListener.onLocationChanged(MapsListener.java:77)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:291)
at android.location.LocationManager$ListenerTransport.-wrap0(Unknown Source:0)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:236)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Here is my java code
package local.example.markus.geoapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
/**
* Created by markus on 04.12.17.
*/
public class MapsListener implements LocationListener {
// Member Variablen
private GoogleMap googleMap;
// Konstruktor
public MapsListener() {
}
// Getter und Setter
public GoogleMap getGoogleMap() {
return googleMap;
}
public void setGoogleMap(GoogleMap googleMap) {
this.googleMap = googleMap;
}
// Interface Methods
#Override
public void onLocationChanged(Location location) {
// Print new Latitide and Logtitude into log
Log.d("INFO", "New Location! Latitude: '" + location.getLatitude() + "', '" + location.getLongitude() + "'");
// Define new Latitude and Logtitude object
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
// Add a new Marker to the Map
this.googleMap.addMarker(new MarkerOptions().position(latLng).title("Lat:" + location.getLatitude() + ", Lng: " + location.getLongitude()));
// Build ca,era position
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(0) // Sets the orientation of the camera to north
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
// Animate camera to zoom to the new position with defined settings
this.googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Log.d("Device ID", this.getDeviceId());
this.sendPost(location);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
private String getDeviceId() {
if (Build.VERSION.SDK_INT <= 25) {
return Build.SERIAL;
} else {
return Build.getSerial();
}
}
private void sendPost(Location location) {
URL url = null;
HttpURLConnection httpURLConnection = null;
OutputStream outputStream = null;
try{
url = new URL("http://example.local");
httpURLConnection = (HttpURLConnection) url.openConnection();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
/*httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("d", this.getDeviceId());
httpURLConnection.setRequestProperty("lat", Double.toString(location.getLatitude()));
httpURLConnection.setRequestProperty("lon", Double.toString(location.getLongitude()));
httpURLConnection.setRequestProperty("t", timestamp.toString());
httpURLConnection.setDoOutput(true);*/
outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(Double.toString(location.getLongitude()));
writer.flush();
writer.close();
outputStream.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
How I can send some information about POST requests to my php script on http://example.local/gps.php?
The post Sending POST data in Android does not work.
The post Sending POST data in Android does not work.
The above should work absolutely fine else ans would have been not in accepted and upvoted state.
The error what you're getting obvious since missed how the network call is made in that ans
You missed below one.
public class CallAPI extends AsyncTask {
In android or other platforms most of the platforms also network call ui thread is not allowed. In Android AsyncTask is one way making network call off the ui thread.
This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask.
public class HttpPost extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
URL url = null;
HttpURLConnection httpURLConnection = null;
OutputStream outputStream = null;
try{
url = new URL("http://example.local");
httpURLConnection = (HttpURLConnection) url.openConnection();
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
/*httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("d", this.getDeviceId());
httpURLConnection.setRequestProperty("lat", Double.toString(location.getLatitude()));
httpURLConnection.setRequestProperty("lon", Double.toString(location.getLongitude()));
httpURLConnection.setRequestProperty("t", timestamp.toString());
httpURLConnection.setDoOutput(true);*/
outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(Double.toString(location.getLongitude()));
writer.flush();
writer.close();
outputStream.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onPostExecute(String result) {
//What you want to do with the result
//Call a callback function for instance
//You can also delete this method if you dont expect a result
}
}
My team and I are currently trying to get our android app to send a signal to our arduino with a bluetooth shell on it. The signal doesn't need to be meaningful in anyway only that the arduino knows a signal has been sent. I have seen allot of online material on this, but none of it seems to coincide and none of it seems to work for me.
My current code: (we only want to send a signal when onRecieve() is called)
package com.example.alarmquiz2;
import android.provider.Settings.Secure;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.UUID;
import android.telephony.TelephonyManager;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import android.bluetooth.BluetoothClass.Device;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.widget.Toast;
import android.content.Intent;
import android.content.BroadcastReceiver;
public class AlarmReceiver
extends BroadcastReceiver
{
Sound s = new Sound();
private BluetoothAdapter blue;
private Context contexxt;
private Device arduino;
private BluetoothSocket btSocket;
private TelephonyManager tManager;
private UUID uuid;
private OutputStream outStream;
private InputStream inStream;
private static String address = "00:14:03:18:42:19";
public void onReceive(Context context, Intent intent)
{
TelephonyManager tManager =
(TelephonyManager)context
.getSystemService(Context.TELEPHONY_SERVICE);
uuid = UUID.fromString(tmanager.getDeviceID());
contexxt = context;
this.CheckBt();
this.Connect();
this.writeData("meh");
if (!s.isPlaying())
{
s.setSound(context);
s.startSound();
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
else if (s.isPlaying())
{
s.stopSound();
Intent i = new Intent(context, SecondscreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
private void CheckBt()
{
blue = BluetoothAdapter.getDefaultAdapter();
if (!blue.isEnabled())
{
Toast
.makeText(contexxt, "Bluetooth Disabled !", Toast.LENGTH_SHORT)
.show();
/*
* It tests if the bluetooth is enabled or not, if not the app will
* show a message.
*/
}
if (blue == null)
{
Toast.makeText(contexxt, "Bluetooth null !", Toast.LENGTH_SHORT)
.show();
}
}
public void Connect()
{
BluetoothDevice device = blue.getRemoteDevice(address);
Log.d("", "Connecting to ... " + device);
blue.cancelDiscovery();
try
{
btSocket = device.createRfcommSocketToServiceRecord(uuid);
/*
* Here is the part the connection is made, by asking the device to create a
* RfcommSocket (Unsecure socket I guess), It map a port for us or something
* like that
*/
btSocket.connect();
Log.d("", "Connection made.");
}
catch (IOException e)
{
try
{
btSocket.close();
}
catch (IOException e2)
{
Log.d("", "Unable to end the connection");
}
Log.d("", "Socket creation failed");
}
/*
* this is a method used to read what the Arduino says for example when
* you write Serial.print("Hello world.") in your Arduino code
*/
}
private void writeData(String data)
{
try
{
outStream = btSocket.getOutputStream();
}
catch (IOException e)
{
Log.d("", "Bug BEFORE Sending stuff", e);
}
String message = data;
/* In my example, I put a button that invoke this method and send a string to it */
byte[] msgBuffer = message.getBytes();
try
{
outStream.write(msgBuffer);
}
catch (IOException e)
{
Log.d("", "Bug while sending stuff", e);
}
}
}
Ive also give myself all the required permissions in my manifest. The problem I am presently getting on my friends phone is that the "getDeviceID()" is returning a 14 digit number as opposed to the "00000000-0000-0000-0000-000000000000" format. Any suggestions, scoldings, or advice would be most welcome.
This:
uuid = UUID.fromString(tmanager.getDeviceID());
...
btSocket = device.createRfcommSocketToServiceRecord(uuid);
is most certainly not what you want to do.
What makes you think that the "device ID" of the phone(?) is in some way related to the UUID which is used to identify a certain bluetooth service at the other device?
Btw, did you read the docs?
You definitely need to find out which UUID you have to use to connect to the specific service the destination device provides on its BT interface. A list of well-known, standard UUIDs can be found here for example.
Many devices provide the "serial port profile" (SPP) for basic, stream-oriented data exchange. You may want to try that first.
Here's another source which may help.
I am new to android + bluetooth. I am trying to write a simple rfcomm client app which talks to a rfcomm server running on a embedded platfrom. When my application starts it show a listview of paired devices and on clicking one of thoes devices it jumps to a second activity. In this activity I opens a bluetooth socket and connects to it. As soon as I jump to a new activity my UI sort of hangs. This new activity has two buttons. Onclick of these buttons I send data to bluetooth server. But my UI kind of hangs in this activity, once I click a button it takes ages to respond and it generates a error message.
My codes looks like this:
package com.example.bluetoothapp2;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
// Return Intent extra
public static String EXTRA_DEVICE_ADDRESS = "device_address";
public int REQUEST_ENABLE_BT = 1;// Just defining a flag
//Define a local bluetooth adapter variable to get id of default Adapter
public BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//Adapter for LIST ITEMS FOR PAIRED DEVICES
public ArrayAdapter<String> mPairedDevicesArrayAdapter;
//Array to store list data for paired device
ArrayList<String> itemPairedArray = new ArrayList<String> ();
//Adapter for LIST ITEMS FOR NEW DEVICES
public ArrayAdapter<String> mNewDevicesArrayAdapter;
//Array to store list data for paired device
ArrayList<String> itemNewDvcArray = new ArrayList<String> ();
//RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter=0;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
//GetListId from Layout
ListView pairedListView = (ListView) findViewById(R.id.listPairedDvc);
ListView newListView = (ListView) findViewById(R.id.listAvailableDvc);
//Bind the list with listview in layout
mPairedDevicesArrayAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,itemPairedArray);
mNewDevicesArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,itemNewDvcArray);
//Set the adapter to list view (bind a adapter to listview in layout)
pairedListView.setAdapter(mPairedDevicesArrayAdapter);
newListView.setAdapter(mNewDevicesArrayAdapter);
pairedListView.setOnItemClickListener(mDeviceClickListener);
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
this.registerReceiver(mReceiver, filter);
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
// Get a set of currently paired devices
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices, add each one to the ArrayAdapter
if (pairedDevices.size() > 0)
{
/*findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);*/
for (BluetoothDevice device : pairedDevices)
{
itemPairedArray.add(device.getName() + " => "+ device.getAddress());
}
}
else
{
itemPairedArray.add("noDevices");
}
//Check if device has bluetooth
if(mBluetoothAdapter == null)
{
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
//Check if it is enabled or not
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v)
{
//itemPairedArray.add("Clicked : "+clickCounter++); //Just for debugging
//itemNewDvcArray.add("Clicked : "+clickCounter++); //Just for debugging
//itemNewDvcArray.add("NoDevice");
startDiscovery() ;
//mPairedDevicesArrayAdapter.notifyDataSetChanged();
mNewDevicesArrayAdapter.notifyDataSetChanged();
}
//method to start discovery
private void startDiscovery()
{
// TODO Auto-generated method stub
// If we're already discovering, stop it
if (mBluetoothAdapter.isDiscovering())
{
mBluetoothAdapter.cancelDiscovery();
}
// Request discover from BluetoothAdapter
mBluetoothAdapter.startDiscovery();
}
protected void onDestroy()
{
super.onDestroy();
// Make sure we're not doing discovery anymore
if (mBluetoothAdapter != null)
{
mBluetoothAdapter.cancelDiscovery();
}
// Unregister broadcast listeners
this.unregisterReceiver(mReceiver);
}
// The BroadcastReceiver that listens for discovered devices and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED)
{
// Add the name and address to an array adapter to show in a ListView
itemNewDvcArray.add(device.getName() +" => "+ device.getAddress());
}
}
}
};
// The on-click listener for all devices in the ListViews
private OnItemClickListener mDeviceClickListener = new OnItemClickListener()
{
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3)
{
// Get the device MAC address, which is the last 17 chars in the View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
//Toast.makeText(getApplicationContext(), address, 0).show();
// Create the result Intent and include the MAC address
Intent intent = new Intent(MainActivity.this,ConnectActivity.class);
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
startActivity(intent);
}
};
}
package com.example.bluetoothapp2;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ConnectActivity extends Activity {
// Unique UUID for this application
public static final UUID MY_UUID_INSECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
String address;
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
public BluetoothSocket mmSocket;
public InputStream mmInStream;
public OutputStream mmOutStream;
String tag = "debugging";
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Handler mHandler = new Handler(){
#Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
Log.i(tag, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// DO something
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
//Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
String s = "successfully connected";
connectedThread.write(s.getBytes());
Log.i(tag, "connected");
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
String string = new String(readBuf);
//Toast.makeText(getApplicationContext(), string, 0).show();
break;
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
// Get the message from the intent
Intent intent = getIntent();
address = intent.getStringExtra(MainActivity.EXTRA_DEVICE_ADDRESS);
// Create the text view
TextView textView = (TextView)findViewById(R.id.addrs);
//textView.setTextSize(40);
textView.setText(address);
connect();
}
//method to start discovery
public void connect()
{
// Get the BluetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
//Toast.makeText(getApplicationContext(),"connecting", 0).show();
ConnectThread connect = new ConnectThread(device) ;
connect.start();
}
//Thread for establising a connection
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_INSECURE);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run()
{
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
//manageConnectedSocket(mmSocket);
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
{
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();
} 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 (true) {
try {
// Read from the InputStream
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
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 {
//a delay of 20ms occurs after each flush...
mmOutStream.write(bytes);
mmOutStream.flush();
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
//method to start discovery
public void ledOn(View v ) throws IOException
{
//Toast.makeText(getApplicationContext(),"LED ON", 0).show();
mmOutStream.write('o');
}
//method to start discovery
public void ledOff(View v ) throws IOException
{
//Toast.makeText(getApplicationContext(),"LED OFF", 0).show();
mmOutStream.write('f');
}
public void disconnectCntn(View v ) throws IOException
{
String msg = "Disconnect";
mmOutStream.write(msg.getBytes());
// close the connection
mmOutStream.close();
mmInStream.close();
// close the connection
if (mmSocket != null)
try {
mmSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
}
I get following error messages
08-19 13:09:55.833: D/AndroidRuntime(5221): Shutting down VM
08-19 13:09:55.834: W/dalvikvm(5221): threadid=1: thread exiting with uncaught exception (group=0x41cc29a8)
08-19 13:09:55.847: E/AndroidRuntime(5221): FATAL EXCEPTION: main
08-19 13:09:55.847: E/AndroidRuntime(5221): java.lang.IllegalStateException: Could not execute method of the activity
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.view.View$1.onClick(View.java:3606)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.view.View.performClick(View.java:4211)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.view.View$PerformClick.run(View.java:17446)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.os.Handler.handleCallback(Handler.java:725)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.os.Looper.loop(Looper.java:153)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.app.ActivityThread.main(ActivityThread.java:5299)
08-19 13:09:55.847: E/AndroidRuntime(5221): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 13:09:55.847: E/AndroidRuntime(5221): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 13:09:55.847: E/AndroidRuntime(5221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-19 13:09:55.847: E/AndroidRuntime(5221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-19 13:09:55.847: E/AndroidRuntime(5221): at dalvik.system.NativeStart.main(Native Method)
08-19 13:09:55.847: E/AndroidRuntime(5221): Caused by: java.lang.reflect.InvocationTargetException
08-19 13:09:55.847: E/AndroidRuntime(5221): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 13:09:55.847: E/AndroidRuntime(5221): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.view.View$1.onClick(View.java:3601)
08-19 13:09:55.847: E/AndroidRuntime(5221): ... 11 more
08-19 13:09:55.847: E/AndroidRuntime(5221): Caused by: java.io.IOException: [JSR82] write: write() failed.
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:702)
08-19 13:09:55.847: E/AndroidRuntime(5221): at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:56)
08-19 13:09:55.847: E/AndroidRuntime(5221): at com.example.bluetoothapp2.ConnectActivity.ledOn(ConnectActivity.java:213)
08-19 13:09:55.847: E/AndroidRuntime(5221): ... 14 more
Sorry for such a long post. Any idea why is the UI hangs and what could be causing the error.
Thanks
Tama
ConnectedThread is instantiated, however we have skipped the part which starts the thread, fancy calling start() on it? connectedThread.start() which will keep it listening on a socket for any data written.
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
//add this line
connectedThread.start()
//Toast.makeText(getApplicationContext(), "CONNECT", 0).show();
String s = "successfully connected";
connectedThread.write(s.getBytes());
Im trying to write a program using sockets having an android phone as the client and my PC as the server i found and slightly modified code for both sides and my server code works but i can't get the client activity to and i don't know acutally how to get the to programs to talk i want to basically be able to have either side input text and have it sent and set visable to the other i know how much of a stickler you guys are for questions so... How do i get these to communicate?
Client code (The emulator crashes before i can even begin to see the problem)
package com.example.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.*;
import java.net.*;
public class ClientActivity extends Activity {
private EditText serverIp;
private Button connectPhones;
private String serverIpAddress = "";
private boolean connected = false;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener((android.view.View.OnClickListener) connectListener);
}
private OnClickListener connectListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, 9999);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// where you issue the commands
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
the Server code
import java.io.*;
import java.net.*;
/**
* Simple server using Java Sockets.
* #author Jonathan Engelsma (http://www.cis.gvsu.edu/~engelsma)
*
*/
public class Server {
/**
* #param args
*/
public static void main(String[] args) {
try {
// First we create a server socket and bind it to port 9999.
ServerSocket myServerSocket = new ServerSocket(9999);
// wait for an incoming connection...
System.out.println("Server is waiting for an incoming connection on host="
+ InetAddress.getLocalHost().getCanonicalHostName()
+ " port=" + myServerSocket.getLocalPort());
Socket skt = myServerSocket.accept();
// ok, got a connection. Let's use java.io.* niceties to read and write from the connection.
BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
PrintStream myOutput = new PrintStream(skt.getOutputStream());
// attempt to read input from the stream.
String buf = myInput.readLine();
// if we got input, print it out and write a message back to the remote client..
if (buf != null) {
System.out.println("Server read: [" + buf + "]");
myOutput.print("got it");
}
// close the connection.
skt.close();
System.out.println("Server is exiting");
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("Whoops, something bad happened! I'm outta here.");
}
}
}
Are you using
<uses-permission android:name="android.permission.INTERNET" />
In the Android Manifest? That could cause your problem.
if you are using ICS or JB, then you are probably being restricted from opening network connections from within your main activity. you will get an opaque error message regarding network permissions.
i cant repost my answer to another quite similar StackOverflow question here (it is considered spamming by moderators), but you can CHECK IT OUT HERE.
i posted a functional send and receive socket connector client using asynctask there.
again i have got a problem with socket programming in Android. My Problem is Selector.select() returns zero that there are no SocketChannels ready for writing. And again the same code works in normal Java but does not work in Android. Here is my code:
package com.test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class WebSocketTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SocketChannel channel = null;
SocketAddress socketAdress = new InetSocketAddress("10.0.0.1", 8787);
try {
channel = SocketChannel.open();
} catch (IOException e) {
Log.e("ERROR", "channel open");
}
try {
channel.configureBlocking(false);
} catch (IOException e1) {
Log.e("ERROR", "channel blocking");
}
try {
channel.connect(socketAdress);
} catch (IOException e) {
Log.e("ERROR", "channel connect");
}
try {
while(!channel.finishConnect())
{
}
} catch (IOException e1) {
Log.e("ERROR", "channel finishConnect");
}
Selector selector = null;
try {
selector = Selector.open();
} catch (IOException e) {
Log.e("ERROR", "selector open");
}
try {
channel.register(selector, channel.validOps());
} catch (ClosedChannelException e) {
Log.e("ERROR", "channel register");
}
boolean i = true;
while(i)
{
int readyChannels = 0;
try {
readyChannels = selector.select();
} catch (IOException e) {
Log.e("ERROR", "selector select");
}
if(readyChannels > 0)
{
i = false;
}
}
}
}
In Java readyChannels = 1. In Android it is 0.
Can anyone help me?
Emulator sits behind a virtual router. You need to configure Network Redirections (port forwarding) to make a certain port on emulator visible to the outside network (including your computer).
There are several issues with this NIO code.
Instead of connecting and then spinning around finishConnect(), possibly forever, you should connect before going into non-blocking mode. At the moment you're just burning the CPU, flattening the battery, etc.
You should only register OP_WRITE when you have something to write. It is normally 'ready', so if you register the channel for it permanently your selection loop will just spin. The only time OP_WRITE isn't ready is when you have filled the socket send buffer.