I'm trying to write code for an android app that uses Bluetooth. I can access the devices I've already paired with just fine but I am unable to discover new devices. I've looked on stack overflow but there doesn't seem to be an answer directly for me. Here is the .java, .xml, and what it looks like.
public class Discover extends AppCompatActivity {
private ArrayList<connectableDevices> found = new ArrayList<connectableDevices>(); // appendable name array
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); // BT adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discover);
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
if (mBluetoothAdapter.isDiscovering()){
Toast.makeText(this, "Locating nearby devices...", Toast.LENGTH_LONG).show();
}
BTAdapter BTadapter = new BTAdapter(this, found);
ListView deviceList2 = (ListView) findViewById(R.id.discoverList);
deviceList2.setAdapter(BTadapter);
}
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
found.add(new connectableDevices(deviceName, deviceHardwareAddress));
//MAC.add(deviceHardwareAddress);
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
//...
mBluetoothAdapter.cancelDiscovery();
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(mReceiver);
}
}
And the layout is
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rperdomo.btaccelerometerdata.Discover">
<TextView
android:id="@+id/textView3"
android:layout_width="274dp"
android:layout_height="44dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:text="Found Devices"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/discoverList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/discoverList"
android:layout_width="395dp"
android:layout_height="454dp"
android:layout_marginBottom="40dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
App output
