brendan-myers / rpi3-wifi-conf-android Goto Github PK
View Code? Open in Web Editor NEWSimple Android application to configure wifi over bluetooth for a Raspberry Pi 3
License: MIT License
Simple Android application to configure wifi over bluetooth for a Raspberry Pi 3
License: MIT License
Hi Brendan,
I had an idea for a project which included setting the Wifi connection of an RPI3 through Bluetooth and was lucky enough to come across your post on a forum which led me here. I have been working with this and the Python code to extend the functionality and since I'm very new to Android Studio, I've been struggling on how to initiate a new worker thread on a button click like you did for your code. I have been able to do this on the GUI thread but I understand the wisdom of offloading it to a background thread. I tried simply naming a new thread workerThread2 but that does not seem to be the right way and my searching for help has not given me the answer that I need. I have posted the Java for where I currently am and would appreciate any help that you could give me.
package io.brendanmyers.rpiconf;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
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.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
BluetoothSocket mmSocket;
Spinner devicesSpinner;
Button refreshDevicesButton;
TextView ssidTextView;
TextView pskTextView;
Button startButton;
TextView messageTextView;
Button getSsidButton;
private DeviceAdapter adapter_devices;
final UUID uuid = UUID.fromString("815425a5-bfac-47bf-9321-c5ff980b5e11");
final byte delimiter = 33;
int readBufferPosition = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ssidTextView = (TextView) findViewById(R.id.ssid_text);
pskTextView = (TextView) findViewById(R.id.psk_text);
messageTextView = (TextView) findViewById(R.id.messages_text);
devicesSpinner = (Spinner) findViewById(R.id.devices_spinner);
refreshDevicesButton = (Button) findViewById(R.id.refresh_devices_button);
startButton = (Button) findViewById(R.id.start_button);
getSsidButton = (Button) findViewById(R.id.get_ssid_button);
refreshDevicesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
refreshDevices();
}
});
getSsidButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ssid = ssidTextView.getText().toString();
String psk = pskTextView.getText().toString();
BluetoothDevice device = (BluetoothDevice) devicesSpinner.getSelectedItem();
getSsid();
}
});
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ssid = ssidTextView.getText().toString();
String psk = pskTextView.getText().toString();
BluetoothDevice device = (BluetoothDevice) devicesSpinner.getSelectedItem();
(new Thread(new workerThread(ssid, psk, device))).start();
}
});
refreshDevices();
}
private void refreshDevices() {
adapter_devices = new DeviceAdapter(this, R.layout.spinner_devices, new ArrayList<BluetoothDevice>());
devicesSpinner.setAdapter(adapter_devices);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
adapter_devices.add(device);
}
}
}
private void getSsid() {
String control_value;
String ssid2 = ssidTextView.getText().toString();
String psk2 = pskTextView.getText().toString();
BluetoothDevice device2 = (BluetoothDevice) devicesSpinner.getSelectedItem();
clearOutput();
writeOutput("Connecting to RPI");
writeOutput("Device: " + device2.getName() + " - " + device2.getAddress());
try {
mmSocket = device2.createRfcommSocketToServiceRecord(uuid);
if (!mmSocket.isConnected()) {
mmSocket.connect();
Thread.sleep(1000);
}
control_value = "1";
writeOutput(control_value);
OutputStream mmOutputStream = mmSocket.getOutputStream();
final InputStream mmInputStream = mmSocket.getInputStream();
waitForResponse(mmInputStream, -1);
writeOutput("Connected.");
writeOutput("Requesting SSIDs");
mmOutputStream.write(control_value.getBytes());
mmOutputStream.flush();
waitForResponse(mmInputStream, -1);
mmSocket.close();
writeOutput("Success.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
writeOutput("Failed.");
}
writeOutput("Done.");
}
private void writeOutput(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String currentText = messageTextView.getText().toString();
messageTextView.setText(currentText + "\n" + text);
}
});
}
private void clearOutput() {
runOnUiThread(new Runnable() {
@Override
public void run() {
messageTextView.setText("");
}
});
}
/*
* TODO actually use the timeout
*/
private void waitForResponse(InputStream mmInputStream, long timeout) throws IOException {
int bytesAvailable;
while (true) {
bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
byte[] readBuffer = new byte[1024];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
writeOutput("Received:" + data);
return;
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
}
}
final class workerThread implements Runnable {
private String ssid;
private String psk;
private BluetoothDevice device;
private String control_value;
public workerThread(String ssid, String psk, BluetoothDevice device) {
this.ssid = ssid;
this.psk = psk;
this.device = device;
}
public void run() {
clearOutput2();
writeOutput2("Starting config update.");
writeOutput2("Device: " + device.getName() + " - " + device.getAddress());
try {
mmSocket = device.createRfcommSocketToServiceRecord(uuid);
if (!mmSocket.isConnected()) {
mmSocket.connect();
Thread.sleep(1000);
}
control_value = "2";
writeOutput2(control_value);
writeOutput2("Connected.");
OutputStream mmOutputStream = mmSocket.getOutputStream();
final InputStream mmInputStream = mmSocket.getInputStream();
waitForResponse2(mmInputStream, -1);
mmOutputStream.write(control_value.getBytes());
mmOutputStream.flush();
waitForResponse2(mmInputStream, -1);
writeOutput2("Sending SSID.");
mmOutputStream.write(ssid.getBytes());
mmOutputStream.flush();
waitForResponse(mmInputStream, -1);
writeOutput2("Sending PSK.");
mmOutputStream.write(psk.getBytes());
mmOutputStream.flush();
waitForResponse2(mmInputStream, -1);
mmSocket.close();
writeOutput2("Success.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
writeOutput2("Failed.");
}
writeOutput2("Done.");
}
}
private void writeOutput2(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String currentText = messageTextView.getText().toString();
messageTextView.setText(currentText + "\n" + text);
}
});
}
private void clearOutput2() {
runOnUiThread(new Runnable() {
@Override
public void run() {
messageTextView.setText("");
}
});
}
/*
* TODO actually use the timeout
*/
private void waitForResponse2(InputStream mmInputStream, long timeout) throws IOException {
int bytesAvailable;
while (true) {
bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
byte[] readBuffer = new byte[1024];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
writeOutput("Received:" + data);
return;
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
}
}
}
It's possible to develop the same thing but for a react native application.
I will pay you
thanks a lot
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.