BluetoothSPPLibrary
BluetoothSPPLibrary copied to clipboard
Unable to connect
Help me ardiuno connecting Unable to connect ???
package mn.sesame.sesamezalguur1;
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.content.Intent; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast;
import app.akexorcist.bluetotohspp.library.BluetoothSPP; import app.akexorcist.bluetotohspp.library.BluetoothState; import app.akexorcist.bluetotohspp.library.DeviceList;
public class MainActivity extends AppCompatActivity {
private String TAG = getClass().getSimpleName();
private RelativeLayout rel;
private LinearLayout lin;
private Button blueToothBtn;
private TextView heregleeTv;
private TextView heregleeTv1;
private TextView tv1;
private TextView tv2;
private boolean onAndOff = false;
BluetoothSPP bt;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!onAndOff) {
tv2.setText("Унтраах");
onAndOff = true;
} else {
tv2.setText("Асаах");
onAndOff = false;
}
}
});
}
private void init() {
bt = new BluetoothSPP(this);
bt.startService(BluetoothState.DEVICE_OTHER);
blueToothBtn = (Button) findViewById(R.id.main_bluetoorh_btn_id);
rel = (RelativeLayout) findViewById(R.id.main_rel_id);
lin = (LinearLayout) findViewById(R.id.main_lin_id);
rel.setVisibility(View.GONE);
lin.setVisibility(View.GONE);
tv1 = (TextView) findViewById(R.id.view_item1_tv_id);
tv2 = (TextView) findViewById(R.id.view_item1_tv1_id);
heregleeTv = (TextView) findViewById(R.id.main_hereglee_tv_id);
heregleeTv1 = (TextView) findViewById(R.id.main_hereglee_tv1_id);
tv1.setTypeface(Util.typeface(getApplicationContext()));
tv2.setTypeface(Util.typeface(getApplicationContext()));
heregleeTv.setTypeface(Util.typeface(getApplicationContext()));
heregleeTv1.setTypeface(Util.typeface(getApplicationContext()));
blueToothBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
bt.disconnect();
} else {
Intent intent = new Intent(getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
bt.setBluetoothStateListener(new BluetoothSPP.BluetoothStateListener() {
public void onServiceStateChanged(int state) {
if (state == BluetoothState.STATE_CONNECTED) {
// Do something when successfully connected
} else if (state == BluetoothState.STATE_CONNECTING) {
// Do something while connecting
} else if (state == BluetoothState.STATE_LISTEN) {
// Do something when device is waiting for connection
} else if (state == BluetoothState.STATE_NONE) {
// Do something when device don't have any connection
}
}
});
bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
Toast.makeText(getApplicationContext(), "Connected to " + name + "\n" + address, Toast.LENGTH_SHORT).show();
}
public void onDeviceDisconnected() {
Toast.makeText(getApplicationContext(), "Connection lost", Toast.LENGTH_SHORT).show();
}
public void onDeviceConnectionFailed() {
Toast.makeText(getApplicationContext(), "Unable to connect", Toast.LENGTH_SHORT).show();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if (resultCode == Activity.RESULT_OK)
bt.connect(data);
} else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
//setup();
} else {
Toast.makeText(getApplicationContext()
, "Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
finish();
}
}
}
public void onDestroy() {
super.onDestroy();
bt.stopService();
}
public void onStart() {
super.onStart();
if (!bt.isBluetoothEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
} else {
if (!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
// setup();
}
}
}
}
did u get any solution? same thing happens with me.
did u get any solution? same thing happens with me.
did u get any solution? same thing happens with me.
did u solved this problem?
No, I used HC-06, HC-06 example everywhere
On Tue, Apr 10, 2018 at 6:26 AM, divid3d [email protected] wrote:
did u solved this problem?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/akexorcist/Android-BluetoothSPPLibrary/issues/55#issuecomment-379913003, or mute the thread https://github.com/notifications/unsubscribe-auth/AFsCUF79BJOwL10MqFzmmwv741n1ZA5eks5tm-AYgaJpZM4IuBZa .