I am writing an app on Android Studio.
I communicate from an Android device to an arduino board via Bluetooth.
For now everything works but i am starting a new Activity and i need to stop the actual BT connection. so i want to call a stop method.
The problem is that it crash when i call it.
here is the code
public class BtInterface {
private BluetoothDevice device = null;
private BluetoothSocket socket = null;
private InputStream receiveStream = null;
private OutputStream sendStream = null;
String GlobalBuff="";
String Right_Buff="";
private ReceiverThread receiverThread;
Handler handler;
public BtInterface(Handler hstatus, Handler h,String Device_Name) {
Set<BluetoothDevice> setpairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
BluetoothDevice[] pairedDevices = (BluetoothDevice[]) setpairedDevices.toArray(new BluetoothDevice[setpairedDevices.size()]);
for(int i=0;i<pairedDevices.length;i++) {
if(pairedDevices[i].getName().contains(Device_Name)) {
device = pairedDevices[i];
try {
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
receiveStream = socket.getInputStream();
sendStream = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
handler = hstatus;
receiverThread = new ReceiverThread(h);
}
public void sendData(String data) {
sendData(data, false);
}
public void sendData(String data, boolean deleteScheduledData) {
try {
sendStream.write(data.getBytes());
sendStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void connect() {
new Thread() {
#Override public void run() {
try {
socket.connect();
Message msg = handler.obtainMessage();
msg.arg1 = 1;
handler.sendMessage(msg);
receiverThread.start();
} catch (IOException e) {
Log.v("N", "Connection Failed : " + e.getMessage());
e.printStackTrace();
}
}
}.start();
}
public void close() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
socket=null; //???
}
public BluetoothDevice getDevice() {
return device;
}
private class ReceiverThread extends Thread {
Handler handler;
ReceiverThread(Handler h) {
handler = h;
}
#Override public void run() {
while(true) {
try {
if(receiveStream.available() > 0) {
byte buffer[] = new byte[1000];
int k = receiveStream.read(buffer, 0, 1000);
if(k > 0) {
byte rawdata[] = new byte[k];
for(int i=0;i<k;i++)
rawdata[i] = buffer[i];
String data = new String(rawdata);
GlobalBuff= GlobalBuff+data;
Right_Buff= GlobalBuff.substring(GlobalBuff.length()-1,GlobalBuff.length());
if(Right_Buff.equals("\n")){
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("receivedData", GlobalBuff);
msg.setData(b);
handler.sendMessage(msg);
GlobalBuff="";
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
i try some extra code :
receiverThread.interrupt();
receiverThread=null;
if (receiveStream != null) {
try {receiveStream.close();} catch (Exception e) {}
receiveStream = null;
}
if (sendStream != null) {
try {sendStream.close();} catch (Exception e) {}
sendStream = null;
}
before closing but the result is the same , it crash.
The strange behavior is that it didn't crash immediately as it could happen with a type error or else ( i am talking of the behavior in debug mode...)
If anybody got an idea.
Googling this bring me to people with this issue but no solution that works for my case.
Thanks
UPDATE
what i found as a trace when it crash is that :
06-02 07:45:27.369 9025-9133/fr.icservice.sechage A/libc? Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 9133 (Thread-1436)
I also made a test on a sony Z3 phone under 5.0.2 (compare to my T210 samsung galaxy tab3 under 4.4.2)and it not crash..!
maybe it's a ROM bug?! or a android version problem...
This is a known problem (or bug?) on Android. If you close the Bluetooth socket and then access it later on, some devices will crash with a segmentation fault (like yours). A common workaround is to check socket.isConnected() before or to synchronize the access to close(), write(), read(), ... by setting a flag like closeWasCalled = true and prevent any further calls to methods of this socket in your code after a close() call.
The problem comes with Socket Input/Output. I faced this problem when disconnecting with peer bluetooth device.
Reason :
From code, we are trying to read() , write() from socket object/connection which is closed.
Solution :
Add checking socket.isConnected() before above operations
You can read more about this problem on Stack Overflow question : Here
Related
I'm trying to implement a way to listen to a client's connection event on the smartphone hotspot. I see that android.net.wifi.WIFI_HOTSPOT_CLIENTS_CHANGED is no longer avaible. How can i do this? I think that this is possible because the smartphone notify me when i client make a connection to the smartphone hotspot.
You can't use the Intent Action...You have to use a custom method, i'l suggest you create a background thread that checks/reads the I.P table (/proc/net/arp) constantly and update you...here's a snippet I've used.
Read i.p list table
public ArrayList<String> getConnectedDevices() {
ArrayList<String> arrayList = new ArrayList();
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
while (true) {
String readLine = bufferedReader.readLine();
if (readLine == null) {
break;
}
String[] split = readLine.split(" +");
if (split != null && split.length >= 4) {
arrayList.add(split[0]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return arrayList;
}
Create runnable to check
class CheckHotSpotConnection implements Runnable {
private CheckHotSpotConnection() {
}
public void run() {
int i = 0;
while (discoverClient()) {
i = getConnectedDevices().size();
if (i > 1) {
//client discovered
//disable client discovery to end thread
} else {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Start Thread
new Thread(new CheckHotSpotConnection()).start();
I would like to create a network application where some devices have to send a packet to the same another device. This device is an Android one. My idea is to broadcast the message to the network so that the device will get it. I have checked on the Internet and I have found that one solution might be the MulticastSocket. I've followed the tutorial from the javadoc and this is quite easy. I did it on my Android phone and on one computer. The problem I have now is the fact that I want this socket to be bound on port 80. Effectively, I get an error, more precisely an EACCES when I try to create the socket. Here is the code of my server :
public class MyServer extends Thread {
private int port;
private boolean isRunning = true;
private MulticastSocket socket;
private InetAddress group;
public MyServer(int port) {
this.port = port;
isRunning = true;
}
public void run() {
socket = null;
try {
socket = new MulticastSocket(80);
group = InetAddress.getByName("coucou");
socket.joinGroup(group);
} catch (IOException e) {
e.printStackTrace();
return;
}
while (isRunning) {
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
try {
socket.receive(packet);
Log.i("Server", "Packet received");
MyCipher rec = new MyCipher(Arrays.copyOfRange(packet.getData(), 0, packet.getLength()));
Receiver.getInstance().put(rec);
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
public void mustStop() {
this.notify();
isRunning = false;
}
}
Does someone have an idea how to fix it ? Furthermore, does someone know if the name of the group must be the ip of the server or might it be a "random" string ?
Thank you !
I'm making an app that needs to connect with a bluetooth device and get data from it... that device is set as master, so I needed to implement a Thread, where I listenUsingRfcommWithServiceRecord and wait for a connection from it:
public AcceptThread(Context context, String serverName, UUID myUUID) {
this.context = context;
BluetoothServerSocket tmp = null;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(serverName, myUUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}
Then on run I run the code socket = mmServerSocket.accept(5000) to wait until it starts pairing with the device:
public void run() {
BluetoothSocket socket = null;
while (true) {
try {
socket = mmServerSocket.accept(5000);
} catch (IOException e) {
Log.e(TAG,"IOException: " + e);
}
// If a connection was accepted
if (socket != null) {
// Manage the connection
ManageConnectedSocket manageConnectedSocket = new ManageConnectedSocket(socket);
manageConnectedSocket.run();
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "IOException: " + e);
}
break;
}
}
}
The Device asks for an authentication PIN, and I need to have an automatic procedure... for that I though of implementing a broadcast receiver to know when the device is asked to par with another device:
IntentFilter filter = new IntentFilter(ACTION_PAIRING_REQUEST);
context.registerReceiver(mPairReceiver, filter);
and receive it:
private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PAIRING_REQUEST.equals(action)) {
Log.e(TAG,"ACTION_PAIRING_REQUEST");
setBluetoothPairingPin(device);
}
}
};
In my setBluetoothPairingPin method I receive a BluetoothDevice object :
public void setBluetoothPairingPin(BluetoothDevice device) {
byte[] pinBytes = convertPinToBytes("0000");
try {
Log.e(TAG, "Try to set the PIN");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
Log.e(TAG, "Success to add the PIN.");
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, false);
Log.e(TAG, "Success to setPairingConfirmation.");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
The problem is that I can't know when my socket receives information, and consecutively, can't know what is my BluetoothDevice to set Pairing Pin before it's connected...
Can someone help me on how to surpass this? Or is there other way to put the pin authentication when I'm listenning from BluetoothServerSocket?
If I'm not explaining correctly, please let me know...
Thanks in advance
With the help from this and this, I was able to make work for me...
My confusion was with the method setBluetoothPairingPin that I couldn't understand that the ACTION_PAIRING_REQUEST is actually called when the device is being starting to pairing, and that is when the PIN is asked from the user... so invoking BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);, and changing a bit of the set pairing method I manage to make it work...
Here's my final code:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PAIRING_REQUEST.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String PIN = "0000";
byte[] pin = new byte[0];
try {
pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, PIN);
BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
I'm having issues with connecting. At first it works, than it does not, unless I unpair the devices.
I've gotten every possible exception that could happen, socket closed, pipe closed, connection refused, port already in use, etc.
I'm aware that there are issues with bluetooth on android pre 4.2 (https://code.google.com/p/android/issues/detail?id=37725).
Devices that I'm having problems with connecting these devices:
Htc one(android 4.2)
samsung galaxy s2(android 4.1.2)
nexus 4 (4.3)
samsung galaxy s4 (4.2)
Another minor issue is, that the paired devices are not stored (mostly on the nexus 4, and the sgs2).
Here is my code:
private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //this is the other one that I've tried: fa87c0d0-afac-11de-8a39-0800200c9a66");
private static final String NAME = "BluetoothConnector";
public void listenForConnection() throws IOException, BluetoothException {
//first close the socket if it is open
closeSocket();
BluetoothServerSocket mServerSocket = null;
try {
mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID_SECURE); //ioexception here!
} catch (IOException e) {
if (Build.VERSION.SDK_INT >= 9) {
try { //this is a stupid hack, http://stackoverflow.com/questions/6480480/rfcomm-connection-between-two-android-devices
Method m = mBluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
mServerSocket = (BluetoothServerSocket) m.invoke(mBluetoothAdapter, PORT);
} catch (Exception ex) {
Log.e(ex);
throw e;
}
} else {
throw e;
}
}
while (!isCancelled) {
try {
socket = mServerSocket.accept();
} catch (IOException e) {
if (socket != null) {
try {
socket.close();
} finally {
socket = null;
}
}
throw e;
}
if (socket == null) {
throw new BluetoothException("Socket connection connected, but null");
} else {
isConnected = true;
break; // everything is ok
}
}
}
public void connect(String address) throws IOException, BluetoothException {
mBluetoothAdapter.cancelDiscovery();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
socket = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
} catch (IOException e1) {
Log.e(e1);
if (Build.VERSION.SDK_INT >= 9) {
try {
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, PORT);
} catch (Exception e) {
Log.e(e);
throw e1;
}
} else {
throw e1;
}
}
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket.connect();
} catch (IOException e) {
Log.e(e);
// Close the socket
try {
socket.close();
} catch (IOException e2) {
Log.e(e2);
Log.wtf("unable to close() socket during connection failure");
}
throw e;
}
}
private void closeSocket() {
try {
if (socket != null) {
socket.close();
socket = null;
Log.d("Socket closed");
}
} catch (IOException e) {
Log.e(e);
Log.wtf("close() of connect socket failed");
}
}
I tried changing the uuid(random one also), tried looking at older sdk samples.
So what could be wrong here?
edit: trying to clarify: the problem usually comes up, when 2 devices that have been paired, connected, did some successful communication, get disconnected (by the user). After that, they can not be reconnected, unless they get rebooted, or unpaired manually.
You are trying to paired this manner:
private void TwitPairedDevice() {
buttonTwitPairDevice.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Set<BluetoothDevice> fetchPairedDevices=bluetooth.getBondedDevices();
Iterator<BluetoothDevice> iterator=fetchPairedDevices.iterator();
while(iterator.hasNext())
{
final BluetoothDevice pairBthDevice=iterator.next();
final String addressPairedDevice=pairBthDevice.getAddress();
AsyncTask<Integer, Void, Void> asynchPairDevice=new AsyncTask<Integer, Void, Void>() {
#Override
protected Void doInBackground(Integer... params) {
try {
socket=pairBthDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
};asynchPairDevice.execute();
}
}
});
}
Connect Pired Device:
private void FetchPairedDevices() {
Set<BluetoothDevice> pairedDevices=bluetooth.getBondedDevices();
for(BluetoothDevice pairedBthDevice:pairedDevices)
{
listPairedDevice.add(pairedBthDevice.getName());
}
listviewPairedDevice.setAdapter(adapterPairedDevice);
listviewPairedDevice.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Object listPairedName=arg0.getItemAtPosition(arg2);
String selectedPairedName=listPairedName.toString();
Set<BluetoothDevice> bthDeviceChecking=bluetooth.getBondedDevices();
for(final BluetoothDevice bthDevice:bthDeviceChecking)
{
if(bthDevice.getName().contains(selectedPairedName))
{
listPairDevice.clear();
listPairDevice.add(bthDevice);
final String addressPairedDevice=bthDevice.getAddress();
AsyncTask<Integer, Void, Void> asynTask=new AsyncTask<Integer,Void,Void>() {
#Override
protected Void doInBackground(Integer... params) {
try {
socket=bthDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
asynTask.execute(arg2);
}
}
}
});
}
It seems that at this point Bluetooth is broken on android.
There is no sure way of connecting 2 devices, that works all the time.
Some people are using an unofficial way to do it, but that does not work on all devices.
I did some in house testing, with the top 10 devices, that are on the market currently, so after around around 90 test runs, the hacked method worked 75% of the time, which is not good enough.
For example, the htc oneX will just handle incoming Bluetooth request, as a Bluetooth hands free device(it is connecting succesfully!), but makes messaging impossible.
After implementing full Bluetooth functionality, we decided to remove it from our app, and release it without it. We'll switch to wifi in some later release.
I have no idea what fails in this code because I have trouble reading the crash logs. We are not talking about a app-crash but a phone crash probably caused by either a deadlocked thread or a lock-up of some kind. Suggestions are welcomed!
Background:
When I initiate my connection a dialog shows and when I press the Back button the dialog freezes and after awhile the phone crashes...
Code:
This is the thread that handles the connection with the device. I have no issues connecting to a device at all. What I know is that mmSocket.connect() running when I press the back button. Think the problem lies there somewhere...
class ConnectThread extends Thread {
/**
*
*/
private Handler threadhandler;
private BluetoothDevice mmDevice;
private volatile BluetoothSocket mmSocket;
private Message toMain;
// private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public ConnectThread(Handler threadhandler, BluetoothDevice device) {
this.threadhandler = threadhandler;
this.mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
Method m = mmDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
}catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void run() {
Looper.prepare();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
toMain = threadhandler.obtainMessage();
toMain.arg1 = 1;
threadhandler.sendMessage(toMain);
} catch (SecurityException e) {
Log.e("SecurityExcep", "Oh noes" , e);
toMain = threadhandler.obtainMessage();
toMain.arg1 = 2;
threadhandler.sendMessage(toMain);
Log.w("MESSAGE", e.getMessage());
}catch (IOException e) {
//Bad connection, let's get the hell outta here
try {
Log.e("IOExcep", "Oh noes" , e);
Log.w("MESSAGE", e.getMessage());
mmSocket.close();
toMain = threadhandler.obtainMessage();
toMain.arg1 = 2;
toMain.obj = e.getMessage();
threadhandler.sendMessage(toMain);
return;
} catch (IOException e1) {
Log.e("IOExcep2", "Oh noes" , e);
}
}
try {
mmSocket.close();
} catch (IOException e) {
Log.d("CONNECT_CONSTRUCTOR", "Unable to close the socket", e);
}
toMain = threadhandler.obtainMessage();
toMain.arg1 = 3;
threadhandler.sendMessage(toMain);
Looper.loop();
return;
// Now it should be paired.. only thing to do now is let the user commit to the rest
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
Next code is a snippet from the dialog creator, the thread is called d:
(...)
case DIALOG_BT_ADDING:
search_dialog = new ProgressDialog(this);
search_dialog.setTitle(R.string.adding);
search_dialog.setMessage(res.getText(R.string.bluetooth_add_accept));
search_dialog.setIndeterminate(true);
search_dialog.setCancelable(true);
search_dialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.i("THREAD CONNECT", "Is it alive?: " + d.isAlive());
if(d != null && d.isAlive()){
d.cancel();
//d = null;
}
if(d2 != null && d2.isAlive()){
d2.cancel(false);
//d2 = null;
}
search_dialog.dismiss();
showDialog(DIALOG_NEW_DEVICE_FOUND);
}
});
return search_dialog;
(...)
Here is a snippet of the code executing the ConnectThread-class
private void connectBluetooth(boolean nextstage, IOException e1){
if(!nextstage){
showDialog(DIALOG_BT_ADDING);
d = new ConnectThread(threadhandler, selected_car.getDevice());
d.start();
}
else{
if(e1 != null){
d2 = new BluetoothCheckThread(checkthreadhandler,mBluetoothAdapter,
5000, car_bt, after_bt);
d2.start();
search_dialog.dismiss();
}
else{
showDialog(DIALOG_BT_ADDING_FAILED);
}
}
}
Hope you guys can help me!, thanks for any feedback
Okay, you're calling BluetoothSocket.close() from what looks like the UI thread. This is probably causing the "freeze".
When you say the phone "crashes" do you mean it reboots? If so is this a full reboot (screen goes back to what happens when you first turn the phone on) or a runtime restart (phone typically shows some sort of animation, on Nexus devices, its the four-color particle spray)? If its not a reboot, do you mean you just get a dialog allowing you to kill the app?
In either case you may want to get a reference to the Thread that is calling BluetoothSocket.connect() and call Thread.interrupt(). I'm not sure if a BluetoothSocket is interruptible, but let's hope. Then after interrupting, call close(), which probably shouldn't be called on the main thread.
try to use dialog.setOnKeyListener() in that on keyCode_BACK cancel the thread. try this will work.
It looks like you're calling connect and close on BluetoothSocket which is a "no no". It seems to cause a deadlock. See this link for more info.