Code Monkey home page Code Monkey logo

rpi3-wifi-conf-android's People

Contributors

brendan-myers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rpi3-wifi-conf-android's Issues

Adding new worker Thread

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;
                }
            }
        }
    }
}

}

Wifi conf for react native

It's possible to develop the same thing but for a react native application.
I will pay you
thanks a lot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.