Anyone have experience in Android/java/BlueTooth programming?

Cap'n Jack

Final Approach
PoA Supporter
Joined
Jun 25, 2006
Messages
8,909
Location
Nebraska
Display Name

Display name:
Cap'n Jack
I'm trying to scan for BlueTooth devices near me. The phone's built-in BlueTooth scanning sees my iPad, but my code doesn't- it finds no devices. A breakpoint in my BroadcastReceiver doesn't even get triggered.

I'm wondering if I have a scope problem? It doesn't crash, but it doesn't do anything either.

Thanks for any assistance!

Code:
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private static final int REQEST_ENABLE_BT = 1;
private String strData;
private BluetoothAdapter mBluetoothAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
strData = "";

//Register for broadcasts when a device is discovered
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}


@Override
protected void onDestroy() {
super.onDestroy();

//unregister ACTION_FOUND receiver
this.unregisterReceiver(mReceiver);
}

public void FindDevices(View view) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
System.exit(0);
}

if (!mBluetoothAdapter.isEnabled()) {
Intent enableBTintent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBTintent, REQEST_ENABLE_BT);
}

//look for other devices
if (mBluetoothAdapter.startDiscovery()) {
Toast.makeText(getApplicationContext(), "Looking...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Discovery failed", Toast.LENGTH_SHORT).show();
}


}

// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
TextView textView = findViewById(R.id.editText);
strData = strData + deviceName + " " + deviceHardwareAddress + "\n";
textView.setText(strData);
}
}
};
}
 
Have you posted on forums like StackOverflow?

I'm not a java programmer but when I write code I'll embed lots of debug outputs so I can watch the code run (this assumes you don't have a good debug tool that lets you step through the code and watch its execution and variable values as they're set). When you say the code doesn't seem to do anything, are you getting the 'Looking...' and/or 'Failed' messages or just nothing at all? If nothing at all, this is where more debug output lines can help. Ex: 'Code starting'. 'Module <whatever> started'. 'Var <whatever> set'. 'Module <whatever exiting>, etc.

Doug
 
Doug-

Thanks for the reply. I do use breakpoints. The BroadCastReceiver is never run. Part of the reason is that BT devices need to be in a discoverable mode or they won't respond. Even when I put one in such a mode, it isn't found.

I've used similar code from other places and it fails in the same manner, so I'm wondering if something in the Android OS has changed.
 
Your best bet is to use Android coding forums or StackOverflow. Java makes my head hurt...best of luck getting this to work
 
Back
Top