android - BluetoothAdapter.getDefaultAdapter();返回空

标签 android bluetooth initialization adapter

我开始开发一个通过蓝牙与 arduino 设备通信的应用程序。

我正在用

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

问题是 btAdapter 返回 null,

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

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

有什么想法吗?我正在四处寻找,但运气不佳。

谢谢,费德里科

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

最佳答案

替换你的代码行,

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

btAdapter =  BluetoothAdapter.getDefaultAdapter();

它都是关于局部变量和类级别变量。

您在 onCreate 中获取蓝牙适配器作为局部变量,并在 onCreate() 之外访问未初始化且始终保持为空的类级变量。

关于android - BluetoothAdapter.getDefaultAdapter();返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062616/

相关文章:

android通过web服务IIS或axis访问sql server?

javascript - 使用 Web Bluetooth API 时找不到移动设备

java - 指定情况下如何处理变量的初始化

c++ - 这是初始化我的图书馆的合理方式吗?

android - 如何在 fragment 中隐藏底部导航栏

android - 如何获取 SD 卡 ID/序列号?

linux - GATT 库直接读取特征值,无需遍历服务

java - 如何在初始化时定义 map 内容?

android - 没有标题/fragment 的单页 PreferenceActivity?

android - 如何在android中更改蓝牙适配器的扫描模式?