I searched for some websites and learned that the way to communicate between android device(phone) and PC via USB is to have an app implementing serversocket on the phone and another app implementing client socket on PC. I kinda fixed the phone side's app(no exception now), but I got an exception "Ljava.lang.StackTraceElement" trying to initiate a socket("localhost", 38300). Does anybody know what's going on with this and how I can fix it? I attached both side's of the code below.
My step to get it to run is as follows
: environment:
Samsung Android phone, Linux PC, Android Studio developer run on Linux
: connection step
open android studio
install one app on phone
install another app on pc android emulator
launch phone side app and click on button to attempt to wait for connection
at this step, adb devices will have two devices
adb -s emulator-5554 -s 1234567890(my phone) forward tcp:38300 tcp:38300
launch pc side android emulator app and click on the button to initiate client socket
NOTE: as you all can see that I can use android studio "Android Monitor" window to see both phone's and PC's app shell's log.
phone side
package com.example.seanhsu.androiddevice_serversocket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Scanner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidDevice_ServerSocket extends AppCompatActivity implements OnClickListener{
public static final String TAG = "Connection";
public static final int TIMEOUT = 10;
Intent i = null;
TextView tv = null;
private String connectionStatus = null;
private String socketData = null;
private Handler mHandler = null;
ServerSocket server = null;
String msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_device__server_socket);
// Set up click listeners for the buttons
View connectButton = findViewById(R.id.connect_button);
connectButton.setOnClickListener(this);
View onBtn = findViewById(R.id.hdmi_on);
onBtn.setOnClickListener(this);
View offBtn = findViewById(R.id.hdmi_off);
offBtn.setOnClickListener(this);
// i = new Intent(this, Connected.class);
mHandler = new Handler();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.connect_button:
tv = (TextView) findViewById(R.id.connection_text);
// initialize server socket in a new separate thread
new Thread(initializeConnection).start();
msg = "Attempting to connect...";
Log.e(TAG, "1 "+msg);
Toast.makeText(this, msg, Toast.LENGTH_SHORT/*msg.length()*/).show();
break;
case R.id.hdmi_on:
Log.e(TAG, "disconnect" + Globals.socketOut);
if (Globals.socketOut != null) {
Globals.socketOut.println("hdmiOn");
Globals.socketOut.flush();
msg = "disconnect hdmi on";
Log.e(TAG, "2 "+msg);
Toast.makeText(this, msg, Toast.LENGTH_SHORT/*msg.length()*/).show();
}
else
{
msg = "disconnect hdmi on is null";
Log.e(TAG, "3 "+msg);
Toast.makeText(this, msg, Toast.LENGTH_SHORT/*msg.length()*/).show();
}
break;
case R.id.hdmi_off:
Log.e(TAG, "disconnect" + Globals.socketOut);
if (Globals.socketOut != null) {
Globals.socketOut.println("hdmiOff!!");
Globals.socketOut.flush();
msg = "disconnect hdmi off";
Log.e(TAG, "4 "+msg);
Toast.makeText(this, msg, Toast.LENGTH_SHORT/*msg.length()*/).show();
}
else
{
msg = "disconnect hdmi off is null";
Log.e(TAG, "5 "+msg);
Toast.makeText(this, msg, Toast.LENGTH_SHORT/*msg.length()*/).show();
}
break;
}
}
private Runnable initializeConnection = new Thread() {
public void run() {
Socket client = null;
// initialize server socket
try {
server = new ServerSocket(38300);
server.setSoTimeout(TIMEOUT * 5000);
// attempt to accept a connection
client = server.accept();
Globals.socketIn = new Scanner(client.getInputStream());
Globals.socketOut = new PrintWriter(client.getOutputStream(),
true);
// Globals.socketIn.
} catch (SocketTimeoutException e) {
// print out TIMEOUT
Log.e(TAG, "aaa=== " + e);
msg = "Connection has timed out! Please try again";
Log.e(TAG, "6 "+msg);
//Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();//method error, this causes program terminated
mHandler.post(showConnectionStatus);
} catch (IOException e) {
Log.e(TAG, "bbb=== " + e);
msg = "IO Exception";
Log.e(TAG, "7 "+msg);
//Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();//method error, this causes program terminated
} finally {
// close the server socket
try {
if (server != null)
server.close();
} catch (IOException ec) {
Log.e(TAG, "ccc=== " + ec);
Log.e(TAG, "Cannot close server socket" + ec);
msg = "Cannot close server socket";
Log.e(TAG, "8 "+msg);
//Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();//method error, this causes program terminated
}
}
if (client != null) {
Globals.connected = true;
// print out success
connectionStatus = "Connection was succesful!";
Log.e(TAG, "9 "+connectionStatus);
mHandler.post(showConnectionStatus);
while (Globals.socketIn.hasNext()) {
socketData = Globals.socketIn.next();
mHandler.post(socketStatus);
}
// startActivity(i);
}
}
};
/**
* Pops up a "toast" to indicate the connection status
*/
private Runnable showConnectionStatus = new Runnable() {
public void run() {
Log.e(TAG, "10 "+connectionStatus);
Toast.makeText(getBaseContext(), connectionStatus,
Toast.LENGTH_SHORT).show();
}
};
private Runnable socketStatus = new Runnable() {
public void run() {
TextView tv = (TextView) findViewById(R.id.connection_text);
tv.setText(socketData);
}
};
public static class Globals {
public static boolean connected;
public static Scanner socketIn;
public static PrintWriter socketOut;
}
}
PC side
package com.example.seanhsu.pchost_clientsocket_withactivity;
import android.support.v7.app.AppCompatActivity;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Scanner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class PCHost_ClientSocket_with_Activity extends AppCompatActivity implements OnClickListener{
Socket socket;
PrintWriter out;
Scanner sc;
String msg;
private String TAG = "PCHost_ClientSocket_withActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pchost__client_socket_with_);
View connectButton = findViewById(R.id.connect_button);
connectButton.setOnClickListener(this);
//when the emulator start running, it's already in the emulator shell, so it cann't execute adb command
//execAdb();
}
public void onClick(View v) {
//cannot run on main thread
//need to run on another thread, otherwise it will get android.os.NetworkOnMainThreadException exception
new Thread(initializeConnection).start();
//initializeConnection();
/*while(sc.hasNext()) {
System.out.println(System.currentTimeMillis() + " / " + sc.nextLine());
Log.e(TAG, "12 "+System.currentTimeMillis() + " / " + sc.nextLine());
}*/
}
//cannot run on main thread
//need to run on another thread, otherwise it will get android.os.NetworkOnMainThreadException exception
private Runnable initializeConnection = new Thread() {
public void run() {
msg = "initializeConnection";
Log.e(TAG, msg);
//Create socket connection
try{
Log.e(TAG, "============0============");
socket = new Socket("localhost", 38300);
Log.e(TAG, "============1============");
out = new PrintWriter(socket.getOutputStream(), true);
Log.e(TAG, "============2============");
//in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
sc=new Scanner(socket.getInputStream());
Log.e(TAG, "============3============");
// add a shutdown hook to close the socket if system crashes or exists unexpectedly
Thread closeSocketOnShutdown = new Thread() {
public void run() {
try {
Log.e(TAG, "============4============");
socket.close();
msg = "closeSocketOnShutdown socket close";
Log.e(TAG, msg);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e(TAG, "============5============");
e.printStackTrace();
msg = "closeSocketOnShutdown IOException";
Log.e(TAG, msg);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
}
}
};
Log.e(TAG, "============6============");
Runtime.getRuntime().addShutdownHook(closeSocketOnShutdown);
Log.e(TAG, "============7============");
} catch (UnknownHostException e) {
//Print.fatalError(“Socket connection problem (Unknown host)”+e.getStackTrace());
msg = "Socket connection problem (Unknown host)"+e.getStackTrace();
Log.e(TAG, msg);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
//Print.fatalError(“Could not initialize I/O on socket “+e.getStackTrace());
msg = "Could not initialize I/O on socket "+e.getStackTrace();
Log.e(TAG, msg);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
}
}
};
}
I keep getting exception:
Could not initialize I/O on socket [Ljava.lang.StackTraceElement;#14710f5
Originally I also has IO exception on phone side app, but I add <uses-permission android:name="android.permission.INTERNET"/> in the manifest, it's OK now. Therefore, I also add <uses-permission android:name="android.permission.INTERNET"/> in PC side app's manifest, but still can't fix this problem.
Related
I need your help!
First: I am from austria , so my english is not so good. So I apologize for the mistakes I will make!
This is my first project I try with Android Studio. I am a newbie in programming. I have not much skill in Arduino and Android program language, but I need it for my bachelor project, so I have to learn it!
I worked with a tutorial video, which was in spanish, so I have no clue what that guy was talking about, but I understood the code all in all.
My problem is, when I start the app on my phone, the first screen (paired devices) works fine. But when I press on the paired device...
This following error occurs :
Capturing and displaying logcat messages from application. This
behavior can be disabled in the "Logcat output" section of the
"Debugger" settings page. D/OpenGLRenderer:
ProgramCache.generateProgram: 103079215104 D/AndroidRuntime: Shutting
down VM E/AndroidRuntime: FATAL EXCEPTION: main
Process: bachelor_projekt.bluetoothcontroller, PID: 11061
java.lang.RuntimeException: Unable to resume activity
{bachelor_projekt.bluetoothcontroller/bachelor_projekt.bluetoothcontroller.UserInterface}:
java.lang.IllegalArgumentException: null is not a valid Bluetooth
address
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3506)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.IllegalArgumentException: null is not a valid Bluetooth address
at android.bluetooth.BluetoothDevice.(BluetoothDevice.java:668)
at android.bluetooth.BluetoothAdapter.getRemoteDevice(BluetoothAdapter.java:553)
at bachelor_projekt.bluetoothcontroller.UserInterface.onResume(UserInterface.java:122)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1272)
at android.app.Activity.performResume(Activity.java:6917)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3477)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
I/Process: Sending signal. PID: 11061 SIG: 9 Application terminated.
After that message, the app shuts down :
I divided my app in:
Bluetooth - for the bluetooth connection
Here is the code:
package bachelor_projekt.bluetoothcontroller;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Set;
public class Bluetooth extends AppCompatActivity {
// Cleaning of the Logcat (Systemlog)
private static final String TAG = "Bluetooth";
// Declaration of ListView
ListView IdList;
// String which will be sended to the main frame
public static String EXTRA_DEVICE_ADDRESS = "device_address";
// Declaration of the fields
private BluetoothAdapter myBluetooth;
private ArrayAdapter<String> myPairedDevicesArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
}
#Override
public void onResume()
{
super.onResume();
//----------------------------
VerificationBT();
// Initialze the array which keeps the bluetooth devices
myPairedDevicesArray=new ArrayAdapter<String>(this, R.layout.device_name);
IdList = findViewById(R.id.idList);
IdList.setAdapter(myPairedDevicesArray);
IdList.setOnItemClickListener(myDeviceClickListener);
// Get local default bluetooth adapter
myBluetooth = BluetoothAdapter.getDefaultAdapter();
// Includes the bluetooth member which is paired with the device
Set<BluetoothDevice> pairedDevices =myBluetooth.getBondedDevices();
// Pair with an already in the array included device.
if (pairedDevices.size()>0)
{
for (BluetoothDevice device : pairedDevices){
myPairedDevicesArray.add(device.getName() + "\n" + device.getAddress());
}
}
}
// Configuration for the "on click" ability for the liste
private AdapterView.OnItemClickListener myDeviceClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView av , View v, int arg2, long arg3) {
// Detect the MAC-Adress of the device ( last 17 caracters)
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// 1 Try to connect if MAC adress is the same
Intent i = new Intent(Bluetooth.this, UserInterface.class);
startActivity(i);
}
};
private void VerificationBT(){
// Checks if the device has bluetooth and if it is activated
myBluetooth=BluetoothAdapter.getDefaultAdapter();
if(myBluetooth==null) {
Toast.makeText(getBaseContext(), "Device doesn't have bluetooth", Toast.LENGTH_SHORT).show();
} else{
if (myBluetooth.isEnabled()) {
Log.d(TAG, "...Bluetooth Avtivated...");
} else{
// Ask the user to activate bluetooth
Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBt,1);
}
}
}
}
The second part is the user interface - Here should be the actual project which is just for start (3 Buttons, 1 for LED on, 1 for LED off, 1 for disconnect).
Here is the code:
package bachelor_projekt.bluetoothcontroller;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
public class UserInterface extends AppCompatActivity {
Button IdLedON, IdLedOFF, IdDisconnect;
TextView IdBuffer;
//-------------------------------------------------
Handler bluetoothIn;
final int handlerState = 0;
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private StringBuilder DataStringIN = new StringBuilder();
private ConnectedThread MyConnection;
// Special service - SPP UUID
private static final UUID BTMODULE_UUID = UUID.fromString
("00001101-0000-1000-8000-00211300ACCD"); //805F9B34FB
// String direction of the MAC
private static String address = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_interface);
// Connection between inferface and variables
IdLedON = (Button) findViewById(R.id.IdLedON);
IdLedOFF = (Button) findViewById(R.id.IdLedOFF);
IdDisconnect = (Button) findViewById(R.id.IdDisconnect);
IdBuffer = findViewById(R.id.IdBuffer);
bluetoothIn = new Handler(){
public void handleMessage(android.os.Message msg){
if (msg.what == handlerState) {
String readMessage = (String) msg.obj;
DataStringIN.append(readMessage);
int endOfLineIndex = DataStringIN.indexOf("#");
if (endOfLineIndex > 0) {
String dataInPrint = DataStringIN.substring(0, endOfLineIndex);
IdBuffer.setText("Data: " + dataInPrint);
DataStringIN.delete(0, DataStringIN.length());
}
}
}
};
btAdapter = BluetoothAdapter.getDefaultAdapter();
VerificationBT();
IdLedON.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyConnection.write("1");
}
});
IdLedOFF.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyConnection.write("0");
}
});
IdDisconnect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (btSocket != null)
{
try {
btSocket.close();
} catch (IOException e) {
Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();;}
}
finish();
}
});
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException
{
// Creates a save Connection for the device
return device.createRfcommSocketToServiceRecord(BTMODULE_UUID);
}
#Override
public void onResume()
{
super.onResume();
// Receives MAC Adress Direction out of DeviceListActivity via intent
Intent intent = getIntent();
// Receives MAC Adress Direction out of DeviceListActivity via EXTRA
address = intent.getStringExtra(Bluetooth.EXTRA_DEVICE_ADDRESS);
// adjust MAC Adress
BluetoothDevice device = btAdapter.getRemoteDevice(address);
try
{
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
Toast.makeText(getBaseContext(), "Error while Connection with device accured ", Toast.LENGTH_SHORT).show();
}
// Creates a connection with bluetooth
try
{
btSocket.connect();
} catch (IOException e){
try{
btSocket.close();
} catch (IOException e2){}
}
MyConnection = new ConnectedThread(btSocket);
MyConnection.start();
}
#Override
public void onPause()
{
super.onPause();
try
{
// If you leave the application there can be no access to the bluetooth adapter
btSocket.close();
} catch (IOException e2){}
}
// Check if bluetooth device is available and connect with it
private void VerificationBT()
{
if (btAdapter == null) {
Toast.makeText(getBaseContext(), "Device doesn't support bluetooth", Toast.LENGTH_LONG);
} else {
Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBt, 1);
}
}
// class for making a connection
private class ConnectedThread extends Thread
{
private final InputStream myInStream;
private final OutputStream myOutStream;
public ConnectedThread(BluetoothSocket socket)
{
InputStream tmpIn=null;
OutputStream tmpOut=null;
try
{
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {}
myInStream=tmpIn;
myOutStream=tmpOut;
}
public void run()
{
byte[] buffer = new byte[256];
int bytes;
// Device stays in the "try to connect" mode
while(true) {
try {
bytes = myInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
} catch (IOException e) {
break;
}
}
}
public void write(String input)
{
try{
myOutStream.write(input.getBytes());
} catch (IOException e)
{
// If it is not possible to send data
Toast.makeText(getBaseContext(), " Connection failed", Toast.LENGTH_LONG).show();
finish();
}
}
}
}
So if you need for information, just tell me.
Hopefully someone of you understands my problem and can help me :)
Best wishes
Semi
Seems like there is an error in the code.
This error:
android.app.ActivityThread.handleResumeActivity
Appeared way too many times which means and activity froze or crashed and then tried to restart but kept on failing so many times it just gave up in the end.
Also, it looks like your using a VM to test the code so try using it with a real device.
If you cant find out the problem try writing the code from the start again or run it through a bug interpreter.
Best of luck,
Paul!
I am programming an android app to receive a packet that is being broadcast on the broadcast address of a network (This has been tested and the packet does get broadcast and gets received in the "UDP Sender/Receiver" application as well.) I cannot get my app to pick it up and tell me that it exists. The devices are on the same network and the code for the sending device is working and proprietary. Here is the basic DatagramSocket code for the app.
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
final TextView txt = (TextView)findViewById(R.id.txt1);
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
}
if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
}}
Any help is appreciated, thank you!!
Updated code:
package com.ti.cc3x.android;
import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class buttonListener extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listener);
WifiManager wifi = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock lock = wifi.createMulticastLock("Log_Tag");
final TextView txt = (TextView) findViewById(R.id.txt1);
lock.acquire();
new Thread( new Runnable(){
public void run(){
try {
String text = null;
int server_port = 12356;
byte[] message = new byte[66];
DatagramPacket p = new DatagramPacket(message, message.length);
DatagramSocket s = new DatagramSocket(server_port);
//while(text == null){
s.receive(p);
text = new String(message, 0, p.getLength());
txt.setText("Messed up.");
//}
//if(text != null){
Toast.makeText(buttonListener.this, text, Toast.LENGTH_LONG).show();
txt.setText("Received");
s.close();
//}
}
catch (SocketException se) {
se.printStackTrace();
Toast.makeText(buttonListener.this, "Socket Error", Toast.LENGTH_LONG).show();
txt.setText("Socket Error");
}
catch (IOException ioe) {
ioe.printStackTrace();
Toast.makeText(buttonListener.this, "Network Error", Toast.LENGTH_LONG).show();
txt.setText("Network Error");
}
}
}).start();
lock.release();
}
}
In your answer you mention that the test packets you are sending are being broadcast. Have you tried to see if you can receive the packet if you send it directly to the IP address of your device instead of broadcasting it? There is a chance that your socket is working fine but just not receiving broadcast packets. By default, the Android Wi-Fi stack filters out multicast packets in order to conserve power. If you can receive a packet sent directly to your IP address then that means all you have to do is enable the receiving of multicast packets by acquiring a MulticastLock, which you can find more information on here: Android device not receiving multicast package
If you are still unable to receive a direct packet then there is probably another issue at play, but I would check this first.
package br.com.buceta;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "bluetooth1";
Button btnOn, btnOff;
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
// SPP UUID service
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// MAC-address of Bluetooth module (you must edit this line)
private static String address = "00:15:FF:F2:19:5F";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); (error)
btnOn = (Button) findViewById(R.id.btnOn); (error)
btnOff = (Button) findViewById(R.id.btnOff); (error)
btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();
btnOn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendData("1");
Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();
}
});
btnOff.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendData("0");
Toast.makeText(getBaseContext(), "Turn off LED", Toast.LENGTH_SHORT).show();
}
});
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
if(Build.VERSION.SDK_INT >= 10){
try {
final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
return (BluetoothSocket) m.invoke(device, MY_UUID);
} catch (Exception e) {
Log.e(TAG, "Could not create Insecure RFComm Connection",e);
}
}
return device.createRfcommSocketToServiceRecord(MY_UUID);
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "...onResume - try 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 = createBluetoothSocket(device);
} catch (IOException e1) {
errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.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.
Log.d(TAG, "...Connecting...");
try {
btSocket.connect();
Log.d(TAG, "...Connection ok...");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
Log.d(TAG, "...Create Socket...");
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
}
}
#Override
public void onPause() {
super.onPause();
Log.d(TAG, "...In onPause()...");
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");
}
}
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
}
}
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) {
errorExit("Fatal Error", "Bluetooth not support");
} else {
if (btAdapter.isEnabled()) {
Log.d(TAG, "...Bluetooth ON...");
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
private void errorExit(String title, String message){
Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
finish();
}
private void sendData(String message) {
byte[] msgBuffer = message.getBytes();
Log.d(TAG, "...Send data: " + message + "...");
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 35 in the java code";
msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
errorExit("Fatal Error", msg);
}
}
}
There is no error in R.layout.activity_main, but the code can't recognize the buttons i have declared in activity_main.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/btnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/btn_OFF" />
<Button
android:id="#+id/btnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnOff"
android:layout_centerHorizontal="true"
android:text="#string/btn_ON" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:alpha="0.5"
android:src="#drawable/cxemnet_logo" />
</RelativeLayout>
Ps.: I solved the 'R cannot be resolved as a type' error by Cleaning > Building project and then this error appeared.
You have to declare your buttons. Either you declare them in your method
...
setContentView(R.layout.activity_main);
Button btnOn = findViewById(R.id.btnOn);
Button btnOff = findViewById(R.id.btnOff);
...
or as a local field
public class Classname {
private Button btnOn;
private Button btnOff;
...
}
Delete gen folder from project and then clean up the whole project also remove your import statement used for r.class used in activity class.
Check your manifest file. You probably forgot to declare something. Usually an Activity.
Eclipse added the "R" import automatically. Unless you are trying to reference resources from another package, it is unnecessary and can be deleted.
For example, if you have a package com.package2 but your manifest has a package name of com.package1 then any class in com.package2 will need:
import com.package1.R;
In your case, it appears to be added automatically. Delete it (don't use "organize imports") and "clean"
Android won't generate R file properly if there is any error in one of the XML files. Maybe the error is in one of them (i.e. the layout file): have you closed all tags? Is there any unsupported attribute?
Try also to organize imports (in windows the shortcut is CTRL+MAIUSC+O [it's a vocal, not zero], in Mac is CMD+MAIUSC+O)
There are two different instances of my program not being about to connect to a BluetoothServerSocket.
One of the instances has only 1 UUID generated randomly before it initiates scan mode with SCAN_MODE_CONNECTABLE_DISCOVERABLE and before using the same generated UUID to create a BluetoothServerSocket for listening. The other intance generates a random UUID just before a BluetoothDevice tries to connect with the UUID just generated.
Each of the instances cannot complete the Bluetooth connection. Throughout the entire program, I put many Logs just to try and see why it wouldn't be able to connect.
Below is the code for the first instance. (Generate 1 random UUID at the launch of the app.) If anyone likes to download my Eclipse project just to take a look, here's the link of the project from MediaFire. As for the second instance, uncommenting the C-style comments in the code below will reveal it.
I expected the results would be to have a successful connection between two devices. The successful connection connects a device to a listening socket, by using a generated UUID. The observed results show it is unlikely.
As far as I know, the only way to obtain a UUID is to obtain it from UUID.randomUUID(). If there are other ways, please post a comment below, and I'll check it. But for now, it's not the answer I wanted.
Thanks in advance.
package o.p;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
* The purpose of this app is to create a server-only Bluetooth connection that
* only accepts incoming Bluetooth connections. It is used only for testing
* one device that needs Bluetooth tweaking in order to consistently work
* correctly on the other device. In short, 2 devices are required.
* */
public class Main extends Activity implements View.OnClickListener {
//Button
private Button acceptButton;
private Button scanButton;
//Bluetooth stuffs.
private BluetoothAdapter btAdapter;
private BluetoothServerSocket serverSocket;
private BluetoothSocket socket;
private BluetoothDevice targetDevice;
private final UUID uuid = UUID.randomUUID();
/*private UUID randomUUID;*/
//Accepting thread.
private class Accept implements Runnable {
private BluetoothServerSocket socket;
private BluetoothSocket result = null;
public Accept(BluetoothServerSocket s) {
socket = s;
result = null;
}
#Override
public void run() {
try {
Log.d("DEBUG", "Accepting.");
result = socket.accept();
Log.d("DEBUG", "Closing server socket.");
socket.close();
}
catch (IOException e) {
Log.d("DEBUG - onClick(), case Accept", "Unable to accept incoming connection.");
}
}
public BluetoothSocket getSocket() {
while (result == null);
return result;
}
}
//Connecting thread.
private class Connecting implements Runnable {
private BluetoothDevice device;
public Connecting(BluetoothDevice d) {
device = d;
}
#Override
public void run() {
try {
/*Log.d("DEBUG", "Generating a new random UUID.");
randomUUID = UUID.randomUUID();*/
Log.d("DEBUG", "Obtaining a socket.");
BluetoothSocket s = device.createRfcommSocketToServiceRecord(uuid);
Log.d("DEBUG", "Cancelling discovery, if it's still discovering.");
if (btAdapter.isDiscovering())
btAdapter.cancelDiscovery();
Log.d("DEBUG", "Connecting to listening socket with UUID: " + uuid.toString());
s.connect();
}
catch (IOException e) {
Log.d("DEBUG - Connecting.run()", "Unable to connect to the listening socket.");
}
}
}
//Thread executor
private ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
//BroadcastReceiver for accepting Bluetooth
private BroadcastReceiver receiver;
#Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
init();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_accept:
Log.d("DEBUG", "Pressing the Accept button.");
Accept acceptThread = new Accept(serverSocket);
Connecting connectThread = new Connecting(targetDevice);
if (serverSocket != null) {
executor.execute(acceptThread);
executor.execute(connectThread);
socket = acceptThread.getSocket();
}
else {
Log.d("DEBUG", "Server socket isn't ready.");
Toast.makeText(this, "Server socket isn't ready.", Toast.LENGTH_LONG).show();
}
break;
case R.id.button_scan:
if (btAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Log.d("DEBUG", "Initiating discovery scan mode.");
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
this.startActivity(discoverableIntent);
Toast.makeText(this, "Being discovered...", Toast.LENGTH_LONG).show();
}
if (btAdapter.isDiscovering()) {
Toast.makeText(this, "Re-scanning...", Toast.LENGTH_SHORT).show();
Log.d("DEBUG", "Re-scanning.");
btAdapter.cancelDiscovery();
}
Log.d("DEBUG", "Scanning.");
Toast.makeText(this, "Scanning...", Toast.LENGTH_LONG).show();
btAdapter.startDiscovery();
break;
}
}
private void init() {
Log.d("DEBUG", "Initializing.");
Log.d("DEBUG", "Button initializing.");
acceptButton = (Button) findViewById(R.id.button_accept);
acceptButton.setOnClickListener(this);
scanButton = (Button) findViewById(R.id.button_scan);
scanButton.setOnClickListener(this);
Log.d("DEBUG", "Registering BroadcastReceiver.");
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
Log.d("DEBUG", "Device has been found.");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d("DEBUG", "Obtained a device from Intent.");
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
Log.d("DEBUG", "Removing paired device.");
try {
Method m = device.getClass().getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
Log.d("DEBUG", "Removed " + device);
}
catch (NoSuchMethodException e) {
Log.e("ERROR - DeviceReceiver.onReceive()", "", e);
}
catch (IllegalArgumentException e) {
Log.e("ERROR - DeviceReceiver.onReceive()", "", e);
}
catch (IllegalAccessException e) {
Log.e("ERROR - DeviceReceiver.onReceive()", "", e);
}
catch (InvocationTargetException e) {
Log.e("ERROR - DeviceReceiver.onReceive()", "", e);
}
}
else {
Log.d("DEBUG", "Obtaining remote device's address.");
btAdapter.getRemoteDevice(device.getAddress());
try {
serverSocket = btAdapter.listenUsingRfcommWithServiceRecord(device.getName(), uuid);
Log.d("DEBUG", "Listening to " + device.getName() + "...");
}
catch (IOException e) {
Log.d("DEBUG - onReceive()", "Unable to create a server socket after receiving a broadcast.", e);
serverSocket = null;
Log.d("DEBUG", "Server socket is set to null.");
}
}
targetDevice = device;
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d("DEBUG", "Scanning finished.");
}
}
};
Log.d("DEBUG", "Creating Bluetooth Adapter.");
btAdapter = BluetoothAdapter.getDefaultAdapter();
try {
Log.d("DEBUG", "Creating a server socket for listening using UUID: " + uuid.toString());
serverSocket = btAdapter.listenUsingRfcommWithServiceRecord("server", uuid);
}
catch (IOException e) {
Log.d("DEBUG - init()", "Error in creating a server socket from uuid.");
}
}
#Override
public void onResume() {
super.onResume();
//TODO: Not done with the receivers.
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
}
#Override
public void onPause() {
//TODO: Complete this one. Same for onResume().
super.onPause();
unregisterReceiver(receiver);
}
}
To be able to connect the UUIDs should match.
On the Server side what you are doing is correct i.e generating a ramdom UUID and listening on it,
But the client needs to connect using the same UUID that the server is listening on.
The way to get it will be from your client use the fetchUuidsWithSdp() on the Server BluetoothDevice object and use the obtained UUID to connect to the server.
I am trying to build an Android application that will interface with an external GPS receiver via the Bluetooth Serial Port Profile (SPP). I am using a Nexus One running 2.3.3. I have managed to get my application to receive data from GPS, but I have two issues: 1) When I connect to the device, it only works some of the time. Sometimes the connection just times out, other times it says the device is busy or in use. 2) I haven't been able to figure out how to send data back to the device, which is probably an issue of how I'm using the streams since the incoming stream is a blocking call.
I moved just the relevant code to a new Android application for testing, which is the following:
/res/layout/main.xml (two buttons and a textview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="#+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Connect"></Button>
<Button android:id="#+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Message"></Button>
<TextView android:id="#+id/textStatus" android:textSize="24sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Status Goes Here" />
</LinearLayout>
/src/com.example.bluetoothspp/MainActivity.java
package com.example.bluetoothspp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.SocketTimeoutException;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final String BTAG = "BTThread";
static final int MSG_BT_GOT_DATA = 1;
static final int MSG_BT_STATUS_MSG = 2;
static final int MSG_BT_FINISHED = 99;
Button btnStart, btnSend;
TextView textStatus;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothDevice btdevice = null;
Thread bThread;
BluetoothSocket bsocket;
InputStream bis = null; //Bluetooth input stream
OutputStream bos = null; //Bluetooth output stream
private String MACAddress = "00:01:95:06:1F:32";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart = (Button)findViewById(R.id.btnStart);
btnSend = (Button)findViewById(R.id.btnSend);
textStatus = (TextView)findViewById(R.id.textStatus);
btnStart.setOnClickListener(btnStartListener);
btnSend.setOnClickListener(btnSendListener);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
private OnClickListener btnStartListener = new OnClickListener() {
public void onClick(View v){
if(btnStart.getText().equals("Connect")){
Log.i(TAG, "Connect button pressed");
if (mBluetoothAdapter == null) { //No adapter. Fail
Log.e(TAG, "getDefaultAdapter returned null");
textStatus.setText("getDefaultAdapter returned null");
} else {
if (!mBluetoothAdapter.isEnabled()) { //Bluetooth disabled
Log.e(TAG, "Bluetooth is Disabled");
textStatus.setText("Bluetooth is Disabled");
} else {
Log.i(TAG, "Connecting to Device: " + MACAddress);
btdevice = mBluetoothAdapter.getRemoteDevice(MACAddress);
Log.i(TAG, "Device: " + btdevice.getName());
Log.i(TAG, "Trying to Connect...");
textStatus.setText("Trying to Connect...");
Log.i(TAG, "Starting Thread");
try {
bThread = new Thread(new BluetoothClient(btdevice, true));
bThread.start();
} catch (IOException e) {
Log.e(TAG, "Could not create thread for bluetooth: " + e);
textStatus.setText("Could not create thread for bluetooth...");
}
btnStart.setText("Disconnect");
}
}
} else {
Log.i(TAG, "Disconnect button pressed");
btnStart.setText("Connect");
}
}
};
private OnClickListener btnSendListener = new OnClickListener() {
public void onClick(View v){
textStatus.setText("Sending Message to Thread.");
SendDataToBluetooth("something\r\n");
}
};
public class BluetoothClient implements Runnable {
public BluetoothClient(BluetoothDevice device, boolean IsAnHTCDevice) throws IOException {
if (IsAnHTCDevice) {
//This is a workaround for HTC devices, but it likes to throw an IOException "Connection timed out"
try {
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
bsocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
} catch (Exception e) {
Log.e(BTAG, "Error at HTC/createRfcommSocket: " + e);
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(MSG_BT_STATUS_MSG, "MethodException: " + e));
}
} else {
//This is the normal method, but on a Nexus One it almost always throws an IOException "Service discovery failed" message
try {
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
bsocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (Exception e) {
Log.e(BTAG, "Error at createRfcommSocketToServiceRecord: " + e);
e.printStackTrace();
handler.sendMessage(handler.obtainMessage(MSG_BT_STATUS_MSG, "MethodException: " + e));
}
}
}
public void run() {
try {
Log.i(BTAG, "Cancelling Discovery");
mBluetoothAdapter.cancelDiscovery();
Log.i(BTAG, "Connecting to Socket");
bsocket.connect();
bis = bsocket.getInputStream();
bos = bsocket.getOutputStream();
Log.i(BTAG, "Socket created, streams assigned");
handler.sendMessage(handler.obtainMessage(MSG_BT_STATUS_MSG, "Device Connected"));
Log.i(BTAG, "Waiting for data...");
byte[] buffer = new byte[4096];
int read = bis.read(buffer, 0, 4096); // This is blocking
Log.i(BTAG, "Getting data...");
while (read != -1) {
byte[] tempdata = new byte[read];
System.arraycopy(buffer, 0, tempdata, 0, read);
handler.sendMessage(handler.obtainMessage(MSG_BT_GOT_DATA, tempdata));
read = bis.read(buffer, 0, 4096); // This is blocking
}
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
Log.i(BTAG, "Finished");
handler.sendMessage(handler.obtainMessage(MSG_BT_FINISHED));
}
}
}
public void SendDataToBluetooth(String cmd) { // You run this from the main thread.
try {
if (bsocket != null) {
bos.write(cmd.getBytes());
}
} catch (Exception e) {
Log.e("SendDataToBluetooth", "Message send failed. Caught an exception: " + e);
}
}
public Handler handler = new Handler() { // Handler for data coming from the network and bluetooth sockets
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_BT_GOT_DATA:
Log.i("handleMessage", "MSG_BT_GOT_DATA: " + (String) msg.obj);
textStatus.setText((String) msg.obj);
break;
case MSG_BT_STATUS_MSG:
Log.i("handleMessage", "MSG_BT_STATUS_MSG: " + (String) msg.obj);
textStatus.setText((String) msg.obj);
break;
case MSG_BT_FINISHED:
Log.i("handleMessage", "MSG_BT_FINISHED");
btnStart.setText("Connect");
break;
default:
super.handleMessage(msg);
}
}
};
#Override
protected void onDestroy() {
super.onDestroy();
if (bThread != null) { // If the thread is currently running, close the socket and interrupt it.
Log.i(BTAG, "Killing BT Thread");
try {
bis.close();
bos.close();
bsocket.close();
bsocket = null;
} catch (IOException e) {
Log.e(BTAG, "IOException");
e.printStackTrace();
} catch (Exception e) {
Log.e(BTAG, "Exception");
e.printStackTrace();
}
try {
Thread moribund = bThread;
bThread = null;
moribund.interrupt();
} catch (Exception e) {}
Log.i(BTAG, "BT Thread Killed");
}
}
}
I found that using the normal "bsocket = device.createRfcommSocketToServiceRecord(MY_UUID);" method would usually result in a "Service discovery failed" message for me, so I also tried the "bsocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));" method. That works more often, but likes to time out when I try to connect.
What am I doing wrong here?
Try listening to the incoming data and writing to the device in separate threads. This way you are separating blocking calls.
Did you have a look at Bluetooth chat sample? The sample uses the similar threading technique.
If you are targeting 2.3 and up (which is currently installed on over 50% of android devices out there) you can use the createInsecureRfcommSocketToServiceRecord method to communicate with the device which will surely make it better and more connectable.