Hey guys I am working on an app which should connect to bluetooth devices. But at the moment I'm struggling with the problem that it doesn't find any bluetooth devices in my app but in the bluetooth settings it finds some. (my device runs android 6.x)
My code:
public class MainActivity extends AppCompatActivity {
private ProgressDialog progressDialogBluetoothDiscovery;
private ListView bluetoothDevices;
private ArrayList<String> bluetoothNearbyDevices;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
bluetoothDevices = (ListView) findViewById(R.id.bluetoothDevicesList);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(bluetoothBroadcast, filter);
bluetoothNearbyDevices = new ArrayList<>();
final FloatingActionButton searchDevices = (FloatingActionButton) findViewById(R.id.search_devices);
searchDevices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchDevices();
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(bluetoothBroadcast);
}
private void searchDevices() {
if (mBluetoothAdapter == null) {
Toast.makeText(MainActivity.this, getResources().getString(R.string.bluetooth_not_supported), Toast.LENGTH_LONG).show();
return;
} else {
if (!mBluetoothAdapter.isEnabled()) {
Snackbar.make(findViewById(R.id.coordinatorLayout), getResources().getString(R.string.bluetooth_not_enabled), Snackbar.LENGTH_LONG).setAction(getResources().getString(R.string.activate_bluetooth), new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, MainActivity.CONTEXT_INCLUDE_CODE);
}
}).show();
} else {
scanForDevices();
}
}
}
private void scanForDevices() {
mBluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver bluetoothBroadcast = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) {
scanForDevices();
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
progressDialogBluetoothDiscovery = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.bluetooth_discovery_title), getResources().getString(R.string.bluetooth_discovery_message), true);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
progressDialogBluetoothDiscovery.dismiss();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, bluetoothNearbyDevices);
bluetoothDevices.setAdapter(arrayAdapter);
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Toast.makeText(MainActivity.this, deviceName, Toast.LENGTH_LONG).show();
bluetoothNearbyDevices.add(deviceName);
}
}
};
}
And my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
<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>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
So do you have any Idea why it doesn't find any device in my app?