I am having trouble with this one function that should be continuously running freezing my android app. The function is run, here is the full class (apologies its a bit long):
package com.example.bluetooth_app;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.TextView;
import Bt_tests.Bluetooth_tests;
import Threading.alreadyConnectedThread;
import com.example.bluetooth_app.*;
public class Bluetooth {
private Constants constants;
private BluetoothAdapter mBluetoothAdapter;
private Activity activity; //Activity to store main window's activity
private ArrayAdapter<String> pDevices; //Array adapter for storing already paired devices
private ArrayAdapter<String> nDevices; //Array adapter for storing newly discovered devices
private IntentFilter filter; //Filter for catching bluetooth device actions
private Button sButton; //Scan button
private Button pButton; //Paired Button
private ListView lvBox; //listview box
private BluetoothSocket btSocket;
private BluetoothServerSocket btServSocket;
private BluetoothDevice mDevice;
private TextView tView;
private InputStream iStream;
private OutputStream oStream;
private Integer state = 0;
private Chat btChat;
private Bluetooth_tests btTests;
private alreadyConnectedThread acThread;
/**
* default constructor, basic initializations
*/
public Bluetooth(Activity activity) {
btSocket = null;
btTests = new Bluetooth_tests(activity);
constants = new Constants();
this.activity = activity; //Set class activity to the activity passed to it by the main activity window
//btChat = new Chat();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
tView = (TextView) activity.findViewById(R.id.textView1);
sButton = (Button) activity.findViewById(R.id.scanButton); //sButton = scan button
sButton.setOnClickListener(new View.OnClickListener() { //Listener to check if button is pressed
public void onClick(View v) { //If button is pressed start discovering and hide button
sButton.setVisibility(constants.INVISIBLE); //Make button invisible
startDiscovering();
}
});
pButton = (Button) activity.findViewById(R.id.pairedButton); //pButton = pairedButton
pButton.setOnClickListener(new View.OnClickListener() { //Listener to check if button is pressed
public void onClick(View v) { //If button is pressed find all paired devices and populate listView/run tests on threads
pairedDevices();
}
});
//Setup array adapter for already paired devices
lvBox = (ListView) activity.findViewById(R.id.deviceList);
pDevices = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1);
lvBox.setAdapter(pDevices);
nDevices = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1);
lvBox.setAdapter(nDevices);
lvBox.setClickable(true);
lvBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lvBox.getItemAtPosition(position); //Object that has been selected
String parts[] = o.toString().split("\n"); //parts[0] = name, [1] = MAC address
mDevice = mBluetoothAdapter.getRemoteDevice(parts[1]);
try {
btSocket = mDevice.createInsecureRfcommSocketToServiceRecord(constants.INSECURE_UUID);
} catch(IOException e) {
//TODO: something
}
if(btSocket != null) {
state = 1;
tView.setText("connected 2");
run();
}
//btChat.connect(mBluetoothAdapter.getRemoteDevice(parts[1])); ignore this for now
}
});
// Register for broadcasts when a device is discovered
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
activity.registerReceiver(mReceiver, filter);
// Register for broadcasts when discovery has finished
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
activity.registerReceiver(mReceiver, filter);
//btChat.start();
run();
}
public void run() {
//state 0 = listening
//state 1 = connected
while(state == 0) {
if(mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
if(tView.getText() != "listening") {
tView.setText("listening");
}
try {
btServSocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord
(constants.NAME_INSECURE, constants.INSECURE_UUID);
} catch(IOException e) {
//TODO: something
tView.setText("damn error");
}
try {
btSocket = btServSocket.accept();
} catch(IOException e) {
//TODO: something
tView.setText("didnt accept");
}
if(btSocket != null) {
state = 1;
tView.setText("btSocket created");
run();
}
}
}
public void write() {
}
public void read() {
}
/**
* Check if bluetooth is enabled, if not enable it
*/
public void getAdapter() {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(enableBtIntent, 1);
}
}
/**
* Check if device is bluetooth compatible
*/
public boolean isCompat() {
if(mBluetoothAdapter == null) {
return false; //TODO: better error handling
} else {
return true;
}
}
/**
* Close some shit so we do not eat up resources
*/
public void destroy() {
if(mBluetoothAdapter != null) {
mBluetoothAdapter.cancelDiscovery(); //cancel discovering devices
}
activity.unregisterReceiver(mReceiver); //unregister receiver
}
/**
* Start discovering devices with bluetooth adapter
*/
public void startDiscovering() {
if(mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
}
public void pairedDevices() {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
pDevices.clear();
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
pDevices.add(device.getName() + "\n" + device.getAddress());
}
}
}
/**
* The BroadcastReceiver that listens for discovered devices
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
nDevices.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
sButton.setVisibility(constants.VISIBLE); //Make button visible again
if (nDevices.getCount() == 0) {
//TODO: none found do something
nDevices.add("No devices found");
}
}
}
};
public void timeToPanic() { //When its time to panic, its time to panic
System.exit(1); //Force close the app
}
}
I am not really sure why it is freezing. Should I add a delay to the end of the run function?
No comments:
Post a Comment