Skip to content

Commit

Permalink
Merge pull request #3 from ahahn61/multi_device_aha
Browse files Browse the repository at this point in the history
Multi device aha
  • Loading branch information
vitas authored Mar 10, 2021
2 parents 9ec91a5 + 0dfe029 commit 6f6f414
Show file tree
Hide file tree
Showing 73 changed files with 2,320 additions and 1,191 deletions.
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
app/build
.gradle
gradle/
gradle/*
!gradle/wrapper/gradle-wrapper.jar
!gradle/wrapper/gradle-wrapper.properties
*.iml
.idea
.idea/workspace.xml
.idea/tasks.xml
.idea/datasources.xml
.idea/dataSources.ids
*.swp
local.properties
.DS_Store
Expand All @@ -20,3 +18,4 @@ out/
*.jks
*.keystore
signing.properties

12 changes: 8 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
versionCode 2
versionName "1.2"
}
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "com.pitchgauge.j9pr.pitchgauge"
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 30
versionCode 2
versionName "1.1"
versionName "1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a:clrMap xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" bg1="lt1" tx1="dk1" bg2="lt2" tx2="dk2" accent1="accent1" accent2="accent2" accent3="accent3" accent4="accent4" accent5="accent5" accent6="accent6" hlink="hlink" folHlink="folHlink"/>
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
<xml xmlns:o="urn:schemas-microsoft-com:office:office">
<o:MainFile HRef="::Make-your-own-BlueTooth-RC-Throwmeter-V2.0.htm"/>
<o:File HRef="themedata.xml"/>
<o:File HRef="image001.jpg"/>
<o:File HRef="image002.png"/>
<o:File HRef="image003.png"/>
<o:File HRef="image004.png"/>
<o:File HRef="image005.jpg"/>
<o:File HRef="image006.png"/>
<o:File HRef="image007.jpg"/>
<o:File HRef="image008.png"/>
<o:MainFile HRef="../Make-your-own-BlueTooth-RC-Throwmeter-V2.0.htm"/>
<o:File HRef="themedata.thmx"/>
<o:File HRef="colorschememapping.xml"/>
<o:File HRef="image009.png"/>
<o:File HRef="image001.png"/>
<o:File HRef="image018.jpg"/>
<o:File HRef="image002.jpg"/>
<o:File HRef="image006.jpg"/>
<o:File HRef="image003.jpg"/>
<o:File HRef="image007.png"/>
<o:File HRef="image004.jpg"/>
<o:File HRef="image010.png"/>
<o:File HRef="image005.jpg"/>
<o:File HRef="image011.png"/>
<o:File HRef="image012.png"/>
<o:File HRef="image013.png"/>
<o:File HRef="image014.png"/>
<o:File HRef="image008.jpg"/>
<o:File HRef="image025.jpg"/>
<o:File HRef="image012.jpg"/>
<o:File HRef="image015.png"/>
<o:File HRef="image016.png"/>
<o:File HRef="image016.jpg"/>
<o:File HRef="image017.png"/>
<o:File HRef="image018.png"/>
<o:File HRef="image019.png"/>
<o:File HRef="image019.jpg"/>
<o:File HRef="image020.png"/>
<o:File HRef="image026.jpg"/>
<o:File HRef="image021.png"/>
<o:File HRef="image022.png"/>
<o:File HRef="image027.jpg"/>
<o:File HRef="image024.png"/>
<o:File HRef="image028.jpg"/>
<o:File HRef="image042.png"/>
<o:File HRef="image038.jpg"/>
<o:File HRef="image044.png"/>
<o:File HRef="image039.jpg"/>
<o:File HRef="filelist.xml"/>
</xml>
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Binary file not shown.
2,300 changes: 1,379 additions & 921 deletions app/src/main/assets/Make-your-own-BlueTooth-RC-Throwmeter-V2.0.htm

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class BluetoothBaseActivity extends AppCompatActivity {
protected Handler mHandler ;
protected boolean mIsBound;
protected BluetoothPipe mBluetoothPipe;
protected ArrayList<DeviceTag> mDeviceTags = new ArrayList<>();;
protected ArrayList<DeviceTag> mDeviceTags = new ArrayList<>();

protected boolean autoStart;

protected ServiceConnection serviceConnection = new ServiceConnection() {
Expand Down Expand Up @@ -136,7 +137,7 @@ else if(state == BluetoothState.STATE_NONE)
mBluetoothPipe.setOnDataReceivedListener(new BluetoothPipe.OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {

Log.d(TAG, "Record Message: "+ message + "\n");
Log.d(TAG, "Record Message: " + message + "\n");

}
});
Expand All @@ -147,7 +148,7 @@ public void onDeviceConnected(DeviceTag deviceTag) {
mDeviceTags.add(deviceTag);

Toast.makeText(getApplicationContext()
, "Connected " + deviceTag.getName()+ " (" +(deviceTag.getPos()+1)+")"
, "Connected " + deviceTag.getName() + " (" + (deviceTag.getPos() + 1) + ")"
, Toast.LENGTH_SHORT).show();

SerialPortOpen();
Expand Down Expand Up @@ -238,7 +239,7 @@ private void connectDevicesFromKeyring() {
mDeviceTags = devices;
connectDevicesTags();
} else {
//TODO
//TODO AHa: no action, only message displayed
Log.d(TAG, "no saved devices found. goto selection activity");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ public void stopService() {
if (mBluetoothService != null) {
isServiceRunning = false;
mBluetoothService.stop();
mBluetoothService = null; // AHa: I guess, mBluetoothService is not set to null on destroy?
}
new Handler().postDelayed(new Runnable() {
public void run() {
if (mBluetoothService != null) {
isServiceRunning = false;
mBluetoothService.stop();
}
}
}, 500);
// TODO AHa: check, is not needed
// new Handler().postDelayed(new Runnable() {
// public void run() {
// if (mBluetoothService != null) {
// isServiceRunning = false;
// mBluetoothService.stop();
// }
// }
// }, 1000);
}

public void disconnect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@

public class BluetoothPreferences {


public static final String LIST_INFO = "list_info";

// fetch tag from list of tags
public static DeviceTag getTag(int inhash, ArrayList<DeviceTag> l) {
DeviceTag result = new DeviceTag();
for (DeviceTag sTag: l) {
if (sTag.hashCode() == inhash) {
return sTag;
}
}
return new DeviceTag();
}

public static void setKeyrings(Context context, ArrayList<DeviceTag> arrPackage) {
final SharedPreferences defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
Expand Down Expand Up @@ -56,6 +57,8 @@ public class BluetoothService extends Service {
private List<String> mDeviceAddresses;
private List<String> mDeviceNames;
private List<ConnectedThread> mConnThreads;
private static ArrayList<ConnectedThread> activeConnectedThreads=new ArrayList<ConnectedThread>();

private List<BluetoothSocket> mSockets;
private List<float[]> mfDatas;
private List<Queue<Byte>> mQueueBuffers;
Expand Down Expand Up @@ -105,7 +108,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {

@Override
public void onDestroy() {
this.stop();
super.onDestroy();
}

Expand Down Expand Up @@ -172,12 +174,14 @@ public void run() {
}

public void cancel() {
try {
this.mmServerSocket.close();
mmServerSocket = null;
if (mmServerSocket != null) {
try {
this.mmServerSocket.close();
mmServerSocket = null;

} catch (IOException e) {
}
} catch (IOException e) {
}
}
}

public void kill() {
Expand All @@ -203,7 +207,7 @@ public ConnectThread(BluetoothDevice device, UUID uuid, int pos) {
}

public void run() {
setName("ConnectThread");
setName("ConnectThread" + position);

//mAdapter.cancelDiscovery();
try {
Expand Down Expand Up @@ -243,9 +247,13 @@ class ConnectedThread extends Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private final BluetoothSocket mmSocket;
private boolean isRunningConnectedThread = true;
private int position;

public ConnectedThread(BluetoothSocket socket) {
public ConnectedThread(BluetoothSocket socket, int pos) {
activeConnectedThreads.add(this);
this.mmSocket = socket;
this.position = pos;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
Expand All @@ -259,7 +267,9 @@ public ConnectedThread(BluetoothSocket socket) {

public void run() {
byte[] tempInputBuffer = new byte[1024];
while (true) {
setName("ConnectedThread" + position);

while (isRunningConnectedThread) {
try {
int acceptedLen = this.mmInStream.read(tempInputBuffer);
if (acceptedLen > 0) {
Expand All @@ -275,6 +285,10 @@ public void run() {
return;
}
}
try {
this.mmSocket.close();
} catch (IOException e) {
}
}

public void write(byte[] buffer) {
Expand All @@ -286,10 +300,7 @@ public void write(byte[] buffer) {
}

public void cancel() {
try {
this.mmSocket.close();
} catch (IOException e) {
}
isRunningConnectedThread = false;
}
}

Expand Down Expand Up @@ -399,7 +410,7 @@ public synchronized void connect(BluetoothDevice device, int pos) {

if (getPosIndexOfDevice(device) == -1) {

Log.d(TAG, "connect to: " + device.getName()+ " Address: " + device.getAddress());
Log.d(TAG, "BluetoothService connect(): connect to: " + device.getName() + " Address: " + device.getAddress());

if (isDeviceConnectedAtPos(pos) && mState == BluetoothState.STATE_CONNECTED) {
Log.d(TAG, "Already connected to: " + device.getName()+ " Address: " + device.getAddress()+ " ignore");
Expand All @@ -426,7 +437,7 @@ public synchronized void connect(BluetoothDevice device, int pos) {
uuid = device.getUuids()[0].getUuid();
}

Log.d(TAG, "device UUID===" + uuid);
Log.d(TAG, "BluetoothService connect(): Connect device UUID===" + uuid);

ConnectThread mConnectThread = new ConnectThread(device, uuid, pos);
mConnectThread.start();
Expand All @@ -450,12 +461,14 @@ public synchronized void connected(BluetoothSocket socket, BluetoothDevice devic
this.mConnectThread.cancel();
this.mConnectThread = null;
}

// TODO AHa: why kill mAcceptThread so early, exit from blocking accept()
if (this.mAcceptThread != null) {
this.mAcceptThread.cancel();
this.mAcceptThread.kill();
this.mAcceptThread = null;
}
this.mConnectedThread = new ConnectedThread(socket);
this.mConnectedThread = new ConnectedThread(socket, pos);
mConnThreads.set(pos, mConnectedThread);
this.mConnectedThread.start();

Expand All @@ -471,23 +484,27 @@ public synchronized void connected(BluetoothSocket socket, BluetoothDevice devic
}

private void stopping() {
if (this.mConnectedThread != null) {
this.mConnectedThread.cancel();
this.mConnectedThread = null;

// stop all ConnectedThreads
Iterator<ConnectedThread> itr = activeConnectedThreads.iterator();
while (itr.hasNext()) {
ConnectedThread t = itr.next();
if (t.isAlive()) {
t.cancel();
Log.d(TAG, "BTService.stopped() id=" + t.getId() + " t=" + t.toString());
}
itr.remove();
}

if (this.mAcceptThread != null) {
this.mAcceptThread.cancel();
this.mAcceptThread = null;
}

for (int i = 0; i < MAX_SENSOR_COUNT; i++) {
mDeviceNames.set(i, null);
mDeviceAddresses.set(i, null);
mSockets.set(i, null);

if (mConnThreads.get(i) != null) {
mConnThreads.get(i).cancel();
mConnThreads.set(i, null);
}
}

if (mAdapter != null){
Expand Down Expand Up @@ -540,6 +557,7 @@ private void connectionLost(BluetoothDevice device) {
mHandler.sendMessage(msg);
}

// TODO AHa: restart not working as expected, connectionLost is called on destroy/stop also
BluetoothService.this.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ public class BluetoothState {
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;

// status of data reception from WIT
public static final int WIT_IDLE = 0;
public static final int WIT_DATA_ARRIVING = 1;

}
Loading

0 comments on commit 6f6f414

Please sign in to comment.