Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Myers committed Jul 6, 2016
1 parent bcdf4ed commit fa131a2
Show file tree
Hide file tree
Showing 28 changed files with 799 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ gen/
out/

# Gradle files
.gradle
.gradle/
build/
/build

# Local configuration file (sdk path, etc)
local.properties
/local.properties

# Proguard folder generated by Eclipse
proguard/
Expand All @@ -31,10 +34,15 @@ proguard/

# Android Studio captures folder
captures/
/captures

# Intellij
*.iml
.idea/
.idea/workspace.xml
/.idea/libraries

# Keystore files
*.jks

.DS_Store
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# rpi3-wifi-conf-android
Simple Android application to configure wifi over bluetooth for a Raspberry Pi 3

Used with [this Python script](https://github.com/brendan-myers/rpi3-wifi-conf) running on the target Rasperry Pi.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "io.skygrid.bluetoothtest"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\Brendan\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.brendanmyers.rpiconf">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
59 changes: 59 additions & 0 deletions app/src/main/java/io/brendanmyers/rpiconf/DeviceAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.brendanmyers.rpiconf;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class DeviceAdapter extends ArrayAdapter<BluetoothDevice> {

private LayoutInflater inflater;
private ArrayList<BluetoothDevice> devices;

public DeviceAdapter(Context context, int resource, ArrayList<BluetoothDevice> devices) {
super(context, resource, devices);

this.devices = devices;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public BluetoothDevice getItem(int position) {
return super.getItem(position);
}

@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner_devices_padded, parent, false);

BluetoothDevice device = devices.get(position);

TextView name = (TextView)row.findViewById(R.id.name_label);
TextView address = (TextView)row.findViewById(R.id.address_label);

name.setText(device.getName());
address.setText(device.getAddress());

return row;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner_devices, parent, false);

BluetoothDevice device = devices.get(position);

TextView name = (TextView)row.findViewById(R.id.name_label);
TextView address = (TextView)row.findViewById(R.id.address_label);

name.setText(device.getName());
address.setText(device.getAddress());

return row;
}
}
200 changes: 200 additions & 0 deletions app/src/main/java/io/brendanmyers/rpiconf/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
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;

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

refreshDevicesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
refreshDevices();
}
});

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

final class workerThread implements Runnable {
private String ssid;
private String psk;
private BluetoothDevice device;

public workerThread(String ssid, String psk, BluetoothDevice device) {
this.ssid = ssid;
this.psk = psk;
this.device = device;
}

public void run() {
clearOutput();
writeOutput("Starting config update.");

writeOutput("Device: " + device.getName() + " - " + device.getAddress());

try {
mmSocket = device.createRfcommSocketToServiceRecord(uuid);
if (!mmSocket.isConnected()) {
mmSocket.connect();
Thread.sleep(1000);
}

writeOutput("Connected.");

OutputStream mmOutputStream = mmSocket.getOutputStream();
final InputStream mmInputStream = mmSocket.getInputStream();

waitForResponse(mmInputStream, -1);

writeOutput("Sending SSID.");

mmOutputStream.write(ssid.getBytes());
mmOutputStream.flush();
waitForResponse(mmInputStream, -1);

writeOutput("Sending PSK.");

mmOutputStream.write(psk.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;
}
}
}
}
}
}
Loading

0 comments on commit fa131a2

Please sign in to comment.