Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Fix on isConnected and disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
kido1611 committed Aug 11, 2016
1 parent 96ff27c commit b5166e9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -30,7 +27,7 @@ public class ArduinoConnect {
private BluetoothSocket mConnectedSocket = null;
private BluetoothDevice mConnectedDevice = null;

private ArduinoConnectDialog mDialog;
private DialogConnect mDialog;

int sleepTime = 100;

Expand Down Expand Up @@ -135,8 +132,8 @@ public ArduinoConnect(Activity activity, FragmentManager fragmentManager, Arduin

private void init(){
mBLAdapter = BluetoothAdapter.getDefaultAdapter();
mDialog = new ArduinoConnectDialog();
mDialog.setCallback(new ArduinoConnectDialog.BluetoothDeviceCallback() {
mDialog = new DialogConnect();
mDialog.setCallback(new DialogConnect.BluetoothDeviceCallback() {
@Override
public void onConnected(BluetoothDevice device, BluetoothSocket socket) {
disconnected();
Expand Down Expand Up @@ -173,7 +170,7 @@ public void setCallback(ArduinoConnectCallback callback){
public void showDialog(){
if(mBLAdapter.isEnabled()) {
if (mDialog != null)
mDialog.show(mFragmentManager, "ArduinoConnectDialog");
mDialog.show(mFragmentManager, "DialogConnect");
}else{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
mContext.startActivityForResult(enableBtIntent, 0);
Expand All @@ -185,16 +182,17 @@ public void hideDialog(){
}

public boolean isConnected(){
return mConnectedSocket!=null;
return mConnectedSocket!=null && mConnectedSocket.isConnected();
}

public void disconnected(){
if(mConnectedSocket!=null && mConnectedSocket.isConnected()) {
mHandler.obtainMessage(ARDUINO_MSG_DISCONNECTED).sendToTarget();
try {
mConnectedSocket.close();
} catch (IOException e) {
e.printStackTrace();
}finally(){
mHandler.obtainMessage(ARDUINO_MSG_DISCONNECTED).sendToTarget();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
/**
* Created by kido1611 on 5/29/16.
*/
public class ArduinoConnectDialog extends DialogFragment {
public class DialogConnect extends DialogFragment {

public ArduinoConnectDialog(){
public DialogConnect(){

}

Expand Down Expand Up @@ -225,12 +225,12 @@ public void onReceive(Context context, Intent intent) {

class ConnectArduino extends Thread{

ArduinoConnectDialog.BluetoothDeviceCallback callback;
DialogConnect.BluetoothDeviceCallback callback;

private BluetoothSocket mSocket;
private BluetoothDevice mDevice;

public ConnectArduino(BluetoothDevice device, ArduinoConnectDialog.BluetoothDeviceCallback cb){
public ConnectArduino(BluetoothDevice device, DialogConnect.BluetoothDeviceCallback cb){
this.callback = cb;
try {
mSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
Expand Down

0 comments on commit b5166e9

Please sign in to comment.