I'm starting to develop an app to communicate with an arduino device through bluetooth.
I'm initializing the bt adapter with
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
The problem is that btAdapter returns null,
txt_status.append("\nAdapter " + btAdapter);
Its like the device hasn't got a bluetooth adapter, which is not my case.
Any ideas? I'm searching around with no luck.
Thanks, Federico
Complete code of the activity:
package com.voice.benz.instaurentremote;
import android.bluetooth.*;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.bluetooth.BluetoothAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.content.Intent;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import java.util.Set;
import android.content.Context;
import android.util.Log;
public class bluetooth extends ActionBarActivity {
private TextView bluetoothPaired;
private TextView txt_status;
private BluetoothAdapter btAdapter;
private ListView listview_devices;
private Set<BluetoothDevice> dispositivi;
private ArrayAdapter<String> adapter = null;
private static final int BLUETOOTH_ON=1000;
private TextView txt_bluetoothStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
txt_bluetoothStatus = (TextView) findViewById(R.id.txt_bluetooth_status);
txt_status = (TextView) findViewById(R.id.txt_status);
listview_devices = (ListView) findViewById(R.id.listView_devices);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
listview_devices.setAdapter(adapter);
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null)
{
System.out.println ("Bluetooth non supportato");
}
else
{
System.out.println ("Bluetooth inizializzato");
}
if (btAdapter.isEnabled())
{
// Il bluetooth è già attivato
String mydeviceaddress = btAdapter.getAddress();
String mydevicename = btAdapter.getName();
String status = mydevicename + " : " + mydeviceaddress;
txt_bluetoothStatus.setText(status);
}
else
{
// Attiva bluetooth
}
}
public void attivaBluetooth (View view) {
if (btAdapter != null && !btAdapter.isEnabled())
{
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, BLUETOOTH_ON);
}
//else
//load();
}
public void disattivaBluetooth (View view)
{
if (btAdapter.isEnabled())
{
btAdapter.disable();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==BLUETOOTH_ON && resultCode==RESULT_OK)
{
load();
}
}
private void load()
{
dispositivi = btAdapter.getBondedDevices();
adapter.clear();
for(BluetoothDevice bt : dispositivi)
adapter.add(bt.getName());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_bluetooth, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Replace your code line,
@Override
protected void onCreate(Bundle savedInstanceState) {
...
...
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
with
btAdapter = BluetoothAdapter.getDefaultAdapter();
its all about local variable and Class level variable.
You are getting Bluetooth adapter in onCreate as local variable and accessing outside of onCreate()
the class level variable which is not initialized and always remain null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With