使用Android编程搜索蓝牙设备并显示

时间:2014-03-25 17:30:03

标签: java android bluetooth

嘿,大家,这可能是一个愚蠢的问题,但我有点困惑。所以我需要一个帮助,我是android新手。这是我的代码,我尝试过 Main.java

    package com.example.bluetooth;

    import java.util.Set;
    import android.app.Activity;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
private Button onBtn;
private Button offBtn;
private Button listBtn;
private Button findBtn;
private TextView text;
private BluetoothAdapter myBluetoothAdapter;
private Set<BluetoothDevice> pairedDevices;
private ListView myListView;
private ArrayAdapter<String> BTArrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.text);
    onBtn = (Button) findViewById(R.id.turnOn);
    offBtn = (Button) findViewById(R.id.turnOff);
    listBtn = (Button) findViewById(R.id.paired);
    findBtn = (Button) findViewById(R.id.search);
    myListView = (ListView) findViewById(R.id.list1);
    BTArrayAdapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1);
    myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if(myBluetoothAdapter == null){

        onBtn.setEnabled(false);
        offBtn.setEnabled(false);
        listBtn.setEnabled(false);
        findBtn.setEnabled(false);
        text.setText("Status: not supported");
        Toast.makeText(getApplicationContext(), "Bluetooth is not supported on your device.", Toast.LENGTH_SHORT).show();
    }else{
        text = (TextView) findViewById(R.id.text);
        onBtn = (Button) findViewById(R.id.turnOn);
        onBtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                on(v);
            }
        });
        offBtn = (Button) findViewById(R.id.turnOff);
        offBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                off(v);
            }
        });
        listBtn = (Button) findViewById(R.id.paired);
        listBtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                list(v);
            }

        });
        findBtn = (Button) findViewById(R.id.search);
        findBtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                find(v);
            }

        });
        myListView = (ListView) findViewById(R.id.list1);
        BTArrayAdapter = new ArrayAdapter<String>(this,R.layout.activity_main);
    }
}

public void on(View view) {

    if (!myBluetoothAdapter.isEnabled()) {

        Intent turnOnIntent = new Intent(
                BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);

        Toast.makeText(getApplicationContext(), "Bluetooth turned on",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(), "Bluetooth is already on",

        Toast.LENGTH_LONG).show();
    }
}
public void onActivityResult(int requestCode,int resultCode, Intent data ){
    if(requestCode == REQUEST_ENABLE_BT){
        if(myBluetoothAdapter.isEnabled()){
            text.setText("Status : Connected");
        }else{
            text.setText("Status : Disconnected");
        }
    }
}
public void list(View view){
    pairedDevices = myBluetoothAdapter.getBondedDevices();
    for(BluetoothDevice device : pairedDevices)
    BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
    Toast.makeText(getApplicationContext(),"Show Paired Devices",
    Toast.LENGTH_SHORT).show();
}

final BroadcastReceiver bReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        BTArrayAdapter.notifyDataSetChanged();
    }
    }
};


public void find(View view) {
if (myBluetoothAdapter.isDiscovering()) {
    myBluetoothAdapter.cancelDiscovery();
}else {
    BTArrayAdapter.clear();
    myBluetoothAdapter.startDiscovery();
    registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));   
    }   
}


public void off(View v) {
    // TODO Auto-generated method stub
    myBluetoothAdapter.disable();
    text.setText("Bluetooth is disconnected");
    Toast.makeText(getApplicationContext(), "Bluetooth is disconnected", Toast.LENGTH_LONG).show();
}
    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

我收到simple_list_item_1的错误。我不知道如何解决这个问题。所以请帮我解决这个问题。 这是我的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView

    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Status"
    android:textAppearance="?android:attr/textAppearanceLarge"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="30dp" >
    <Button 
        android:id="@+id/turnOn"
        android:text="@string/on"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/turnOff"
        android:text="@string/off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="80dp" >
    <Button 
        android:id="@+id/paired"
        android:text="@string/paired"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/search"
        android:text="@string/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ListView 
        android:id="@+id/list1"
        android:layout_height="200dp"
        android:layout_width="fill_parent">

    </ListView>
</LinearLayout>

</RelativeLayout>

提前致谢。

1 个答案:

答案 0 :(得分:0)

由于这是Android resource,而不是您创建的那个,因此请将android前缀添加到其中

BTArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);