繁体   English   中英

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

[英]Search for Bluetooth devices and display using android programming

嘿伙计们这可能是一个愚蠢的问题,但我有点困惑。 所以我需要帮助,并且是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>

提前致谢。

由于这是一种Android resource而不是您创建的resource ,因此请在其后附加android前缀

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM