BluetoothAdapter.getDefaultAdapter();返回null

时间:2015-05-05 20:25:46

标签: android bluetooth initialization adapter

我开始开发一款应用程序,通过蓝牙与arduino设备进行通信。

我用

初始化bt适配器
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

问题是btAdapter返回null,

txt_status.append("\nAdapter " + btAdapter);

它就像设备没有蓝牙适配器,这不是我的情况。

有什么想法吗?我没有运气,四处寻找。

谢谢,费德里科

活动的完整代码:

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);
    }
}

1 个答案:

答案 0 :(得分:2)

替换您的代码行,

@Override
protected void onCreate(Bundle savedInstanceState) {
...
...
BluetoothAdapter btAdapter =  BluetoothAdapter.getDefaultAdapter();

btAdapter =  BluetoothAdapter.getDefaultAdapter();

全部关于局部变量和类级别变量。

您正在将onCreate中的蓝牙适配器作为本地变量并在onCreate()之外访问未初始化的类级别变量,并始终保持为空。