2015-05-05 119 views
2

我開始開發一個應用程序,通過藍牙與arduino設備進行通信。BluetoothAdapter.getDefaultAdapter();返回null

我初始化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); 
    } 
} 
+0

在應用清單中,我添加了這兩行 Benz

+0

您的設備上是否啓用了藍牙?它似乎與網絡接口相同,只有激活和連接的設備(WiFi或手機)才返回,如果沒有啓用,則返回「null」。 – GiantTree

+0

是的,它已被激活。順便使用這段代碼: String mydeviceaddress = btAdapter.getAddress(); String mydevicename = btAdapter.getName(); 它返回我的藍牙地址和mac代碼,所以它的工作。 真正的問題是,此代碼如果(!btAdapter.isEnabled()) { startActivityForResult(turnOn,BLUETOOTH_ON); } 返回一個異常(空指針),我正在調查原因 – Benz

回答

2

替換您的代碼行,

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

btAdapter = BluetoothAdapter.getDefaultAdapter(); 

它的所有關於局部變量和Class level v良莠不齊。

您正在將onCreate中的藍牙適配器作爲本地變量訪問,並在onCreate()之外訪問未初始化且始終爲空的類級別變量。

+1

是的,yep yep ...現在它的工作!謝了哥們!! – Benz

+0

@Benz - 如果它解決了你的問題,你可以接受和upvote答案。 :) – user370305

+0

愚蠢的錯誤... – Lunatikul