Skip to content
Snippets Groups Projects
Commit 0482272f authored by Harrion VENIER's avatar Harrion VENIER
Browse files

modification thread add sendActivity

parent 3f73805a
Branches
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -73,5 +73,18 @@ public class GenerateActivity extends AppCompatActivity {
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
request = new Intent(GenerateActivity.this, SendActivity.class);
startActivityForResult(request,15);
}
});
}
}
\ No newline at end of file
package com.example.qrcode;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.example.qrcode.thread.ClientClass;
import java.util.Set;
public class SendActivity extends AppCompatActivity {
public static final int STATE_LISTENING = 1;
public static final int STATE_CONNECTING=2;
public static final int STATE_CONNECTED=3;
public static final int STATE_CONNECTION_FAILED=4;
public static final int STATE_MESSAGE_RECEIVED=5;
private static final int REQUEST_ENABLE_BT = 456;
private static final int REQUEST_ENABLE_LOCATION = 457;
Button bluetoohButton, appButton;
BluetoothAdapter bluetoothAdapter = null;
BluetoothDevice[] btArray;
SendActivity myApp = this;
ListView listView;
String test ="yolo";
byte[] msg;
private Handler handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what)
{
case STATE_LISTENING:
Toast.makeText(getApplicationContext(), "Listening", Toast.LENGTH_SHORT).show();
break;
case STATE_CONNECTING:
Toast.makeText(getApplicationContext(), "Connecting", Toast.LENGTH_SHORT).show();
break;
case STATE_CONNECTED:
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
break;
case STATE_CONNECTION_FAILED:
Toast.makeText(getApplicationContext(), "Connection Failed", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
listView=(ListView) findViewById(R.id.listView);
bluetoohButton = (Button) findViewById(R.id.bluetoothButton);
appButton = (Button) findViewById(R.id.appButton);
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled()) {
// Demande à activer l'interface bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
appButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
bluetoohButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Set<BluetoothDevice> bt=bluetoothAdapter.getBondedDevices();
String[] strings=new String[bt.size()];
btArray=new BluetoothDevice[bt.size()];
int index=0;
if( bt.size()>0)
{
for(BluetoothDevice device : bt)
{
btArray[index]= device;
strings[index]=device.getName();
index++;
}
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(myApp.getApplicationContext(),android.R.layout.simple_list_item_1,strings);
listView.setAdapter(arrayAdapter);
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ClientClass clientClass=new ClientClass(btArray[i], handler);
System.out.println(btArray[i]);
clientClass.start();
//test
clientClass.sendReceive.write(test.getBytes());
//clientClass.sendReceive.write(msg);
}
});
}
}
......@@ -5,58 +5,29 @@ import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;
import com.example.qrcode.SendActivity;
import java.util.UUID;
import java.io.IOException;
public class ClientClass extends Thread
{
private BluetoothDevice device;
private BluetoothDevice _device;
private BluetoothSocket socket;
static final int STATE_LISTENING = 1;
static final int STATE_CONNECTING=2;
static final int STATE_CONNECTED=3;
static final int STATE_CONNECTION_FAILED=4;
static final int STATE_MESSAGE_RECEIVED=5;
private static final int REQUEST_ENABLE_BT = 456;
private static final int REQUEST_ENABLE_LOCATION = 457;
private TextView showCountTextView, msg_box;
static String msg = null;
private static final UUID MY_UUID=UUID.fromString("8ce255c0-223a-11e0-ac64-0803450c9a66");
private SendReceive sendReceive;
private Handler handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
public SendReceive sendReceive;
private Handler _handler;
switch (msg.what)
{
case STATE_LISTENING:
showCountTextView.setText("Listening");
break;
case STATE_CONNECTING:
showCountTextView.setText("Connecting");
break;
case STATE_CONNECTED:
showCountTextView.setText("Connected");
break;
case STATE_CONNECTION_FAILED:
showCountTextView.setText("Connection Failed");
break;
case STATE_MESSAGE_RECEIVED:
byte[] readBuff= (byte[]) msg.obj;
String tempMsg=new String(readBuff,0,msg.arg1);
msg_box.setText(tempMsg);
break;
}
return true;
}
});
public ClientClass (BluetoothDevice device1, TextView showCountTextView, TextView msg_box)
public ClientClass (BluetoothDevice device, Handler handler)
{
device=device1;
this.msg_box = msg_box;
this.showCountTextView = showCountTextView;
_device=device;
_handler = handler;
try {
socket=device.createRfcommSocketToServiceRecord(MY_UUID);
socket=_device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
e.printStackTrace();
}
......@@ -67,17 +38,17 @@ public class ClientClass extends Thread
try {
socket.connect();
Message message=Message.obtain();
message.what=STATE_CONNECTED;
handler.sendMessage(message);
message.what=SendActivity.STATE_CONNECTED;
_handler.sendMessage(message);
sendReceive=new SendReceive(socket, showCountTextView, msg_box);
sendReceive=new SendReceive(socket, _handler);
sendReceive.start();
} catch (IOException e) {
e.printStackTrace();
Message message=Message.obtain();
message.what=STATE_CONNECTION_FAILED;
handler.sendMessage(message);
message.what= SendActivity.STATE_CONNECTION_FAILED;
_handler.sendMessage(message);
}
}
}
\ No newline at end of file
......@@ -5,6 +5,8 @@ import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import com.example.qrcode.SendActivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
......@@ -15,49 +17,17 @@ public class SendReceive extends Thread
private InputStream inputStream;
private OutputStream outputStream;
private TextView showCountTextView, msg_box;
static final int STATE_LISTENING = 1;
static final int STATE_CONNECTING=2;
static final int STATE_CONNECTED=3;
static final int STATE_CONNECTION_FAILED=4;
static final int STATE_MESSAGE_RECEIVED=5;
private static final int REQUEST_ENABLE_BT = 456;
private static final int REQUEST_ENABLE_LOCATION = 457;
private Handler handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
static String msg = null;
switch (msg.what)
{
case STATE_LISTENING:
showCountTextView.setText("Listening");
break;
case STATE_CONNECTING:
showCountTextView.setText("Connecting");
break;
case STATE_CONNECTED:
showCountTextView.setText("Connected");
break;
case STATE_CONNECTION_FAILED:
showCountTextView.setText("Connection Failed");
break;
case STATE_MESSAGE_RECEIVED:
byte[] readBuff= (byte[]) msg.obj;
String tempMsg=new String(readBuff,0,msg.arg1);
msg_box.setText(tempMsg);
break;
}
return true;
}
});
private Handler _handler;
public SendReceive (BluetoothSocket socket, TextView showCountTextView, TextView msg_box)
public SendReceive (BluetoothSocket socket, Handler handler)
{
bluetoothSocket=socket;
InputStream tempIn=null;
OutputStream tempOut=null;
this.msg_box = msg_box;
this.showCountTextView = showCountTextView;
_handler = handler;
try {
tempIn=bluetoothSocket.getInputStream();
......@@ -79,7 +49,7 @@ public class SendReceive extends Thread
{
try {
bytes=inputStream.read(buffer);
handler.obtainMessage(STATE_MESSAGE_RECEIVED,bytes,-1,buffer).sendToTarget();
_handler.obtainMessage(SendActivity.STATE_MESSAGE_RECEIVED,bytes,-1,buffer).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bluetoothButton"
android:layout_width="135dp"
android:layout_height="48dp"
android:layout_marginStart="145dp"
android:layout_marginTop="134dp"
android:layout_marginEnd="145dp"
android:layout_marginBottom="54dp"
android:text="@string/bluetooth"
app:layout_constraintBottom_toTopOf="@+id/appButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listView" />
<ListView
android:id="@+id/listView"
android:layout_width="404dp"
android:layout_height="252dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/appButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="54dp"
android:layout_marginEnd="162dp"
android:layout_marginBottom="190dp"
android:text="App"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bluetoothButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -10,4 +10,5 @@
<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
<string name="title_activity_generateqr">GenerateqrActivity</string>
<string name="bluetooth">Bluetooth</string>
</resources>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment