2016-02-25 75 views
0

我是新来的Android开发,我试图显示列表中广播蓝牙信号的设备列表。这个Android蓝牙代码有什么错误?

MainActivity.java

public class MainActivity extends ListActivity { 
    private BluetoothManager bManager; 
    private BluetoothAdapter bAdap; 
    private listViewAdapter lvAdap; 
    private Handler mHandler; 
    private boolean bScanning; 
    private static long SCAN_PERIOD = 10000; 
    private TextView tv; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     bManager = (BluetoothManager)getSystemService(BLUETOOTH_SERVICE); 
     bAdap = bManager.getAdapter(); 
     bScanning = false; 
     mHandler = new Handler(); 
     tv = (TextView)findViewById(R.id.textView); 
    lvAdap = new listViewAdapter(this); 
    ListView lv = (ListView)findViewById(R.id.listView); 
    lv.setAdapter(lvAdap); 
} 
@Override 
protected void onResume() { 
    super.onResume(); 
    if(!bAdap.isEnabled()) 
    { 
     Intent enableBT = new Intent(bAdap.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBT,1); 
    } 
    scanLeDevice(true); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // User chose not to enable Bluetooth. 
    if (requestCode == 1 && resultCode == Activity.RESULT_CANCELED) { 
     finish(); 
     return; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    scanLeDevice(false); 
    lvAdap.clear(); 
} 

/* 
@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, 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); 
} 
*/ 

//List adapter for displaying Bluetooth device name and address 
public class listViewAdapter extends BaseAdapter 
{ 
    private Context mContext; 
    private LayoutInflater mLayInflat; 
    private ArrayList<BluetoothDevice> bDeviceArray = new ArrayList<BluetoothDevice>(); 

    public listViewAdapter(Context context) 
    { 
     mContext = context; 
     mLayInflat = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public void addDevice(BluetoothDevice device) 
    { 
     if(!bDeviceArray.contains(device)) 
     { 
      bDeviceArray.add(device); 
     } 
    } 

    public BluetoothDevice getDevice(int position) 
    { 
     return bDeviceArray.get(position); 
    } 

    @Override 
    public int getCount() { 
     return bDeviceArray.size(); 
    } 

    public void clear() 
    { 
     bDeviceArray.clear(); 
    } 

    @Override 
    public Object getItem(int i) 
    { 
     return bDeviceArray.get(i); 
    } 

    @Override 
    public long getItemId(int i) 
    { 
     return i; 
    } 

    @Override 
    public View getView(int i, View convertView, ViewGroup parent) { 
     LinearLayout itemView; 

     if(convertView == null) 
     { 
      itemView = (LinearLayout)mLayInflat.inflate(R.layout.list,parent,false); 
     } 
     else 
     { 
      itemView = (LinearLayout)convertView; 
     } 
     TextView dNameText = (TextView)itemView.findViewById(R.id.device_name); 
     TextView dAddresstext = (TextView)itemViewfindViewById(R.id.device_address); 

     BluetoothDevice device = bDeviceArray.get(i); 
     final String deviceName = device.getName(); 
     if(deviceName != null || deviceName.length()>0) 
      dNameText.setText(deviceName); 
     else 
      dNameText.setText("UNKNOWN DEVICE"); 
     dAddresstext.setText(device.getAddress()); 
     return itemView; 
    } 
} 

private void scanLeDevice(final boolean enable) { 
    if (enable) { 
     // Stops scanning after a pre-defined scan period. 
     mHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       bScanning = false; 
       tv.setText("Stopped scanning!"); 
       bAdap.stopLeScan(mLeScanCallback); 
       invalidateOptionsMenu(); 
      } 
     }, SCAN_PERIOD); 
     tv.setText("Scanning..."); 
     bScanning = true; 
     bAdap.startLeScan(mLeScanCallback); 
    } else { 
     bScanning = false; 
     bAdap.stopLeScan(mLeScanCallback); 
    } 
    invalidateOptionsMenu(); 
} 

private BluetoothAdapter.LeScanCallback mLeScanCallback = 
     new BluetoothAdapter.LeScanCallback() { 

      @Override 
      public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         lvAdap.addDevice(device); 
         lvAdap.notifyDataSetChanged(); 
        } 
       }); 
      } 
     }; 

}

list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<TextView 
    android:id="@+id/device_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="24sp"/> 
<TextView 
    android:id="@+id/device_address" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:textSize="14sp"/> 
</LinearLayout> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.newP.inflatetest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Scanning..." 
     android:id="@+id/textView" /> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textView" 
     android:id="@+id/listView" /> 
</RelativeLayout> 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.newP.inflatetest"> 
    <uses-permission android:name="android.permission.BLUETOOTH"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> 
    <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" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Material.Light.DarkActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

我已经包括了所有必要的

进口。由于仿真器不支持蓝牙,因此我将其部署在手机中。

我不知道究竟是什么错误。每当我尝试启动应用程序时,该应用程序崩溃。

我通过DDMS得到了这个错误信息。

了java.lang.RuntimeException:无法启动活动 ComponentInfo {com.newP.inflatetest/com.newP.inflatetest.MainActivity}: 了java.lang.RuntimeException:你的内容必须有一个ListView的ID 属性是'android.R.id.list'

任何帮助将是伟大的。 谢谢。

+0

如果你开始从一个IDE中的应用程序,你可以查看原因在'LogCat'中崩溃。没有发布堆栈跟踪,我不确定你会得到多少帮助。 – Titus

+0

@Titus我已更新错误消息的问题。 – Illuminati0x5B

回答

0

看来,您使用的是ListActivity,并且您还没有定义其id设置为list在您使用的布局ListView元素。

尝试将public class MainActivity extends ListActivity更改为public class MainActivity extends Activity

或者,改变ListActivity元素的ID,以android:id="@+id/list",并在您的活动,取出lv变量的声明和简单的调用setListAdapter(lvAdap);

+0

谢谢。这确定了最初的崩溃。 – Illuminati0x5B

+0

但现在,应用程序在扫描时启动并崩溃。错误在getView方法中的'if(deviceName!= null || deviceName.length()> 0)'。我无法获得有关该错误的更多细节。 – Illuminati0x5B

+0

@ Illuminati0x5B用'if(deviceName!= null &&!deviceName.isEmpty())'替换它以修复异常。 – Titus