在Listview中显示配对设备(蓝牙api)

时间:2016-04-04 21:07:15

标签: android listview bluetooth

我想使用蓝牙API显示列表视图中的配对设备列表,但我不知道为什么它不起作用 我制作了类BluetoothDev,其中包含方法getDevice,它返回所有绑定dev的数组列表。

这是我的代码:

MainActivity.java

package com.example.fatma.listviewcst;

    import android.bluetooth.BluetoothAdapter;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ListView;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {
        ListView listView;
        BluetoothDev bluetoothDev;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);




            }

        @Override
        protected void onStart() {
            super.onStart();

            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (bluetoothAdapter == null) {
                Toast t = new Toast(this);
                   t.setText("Sorry your phone do not support Bluetooth");
               t.show();
            } else {
                if (!bluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent,1);}
            bluetoothDev=new BluetoothDev();
            listView=(ListView)findViewById(R.id.listView);

            devList listeDev=new devList(this,R.layout.item,bluetoothDev.getDevices(bluetoothAdapter));
            listView.setAdapter(listeDev);}
        }
    }

适配器

package com.example.fatma.listviewcst;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by FATMA on 02/04/2016.
 */
public class devList extends ArrayAdapter<BluetoothDevice>{
    Context context;
    int resource;


    public devList(Context context, int resource, List<BluetoothDevice> bluetoothDev) {
        super(context, resource, bluetoothDev);
        this.context=context;
        this.resource=resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //inflater charge le contenu d un fichier ds une vue
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        //false pour dire non pas attaché au root
        View view =inflater.inflate(resource,parent,false);
        TextView devName= (TextView) view.findViewById(R.id.devName);
        TextView devMac= (TextView) view.findViewById(R.id.devMac);

        devName.setText( getItem(position).getName());
        devMac.setText(getItem(position).getAddress());

        return view;
    }
}

BluetoothDev.java

package com.example.fatma.listviewcst;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import java.util.ArrayList;
import java.util.Set;

/**
 * Created by FATMA on 02/04/2016.
 */
public class BluetoothDev {


    public ArrayList<BluetoothDevice> getDevices(  BluetoothAdapter bluetoothAdapter) {
        ArrayList<BluetoothDevice> arrayListBluetooth = new ArrayList<BluetoothDevice>();




                Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
// If there are paired devices
                if (pairedDevices.size() > 0) {
                    // Loop through paired devices
                    for (BluetoothDevice device : pairedDevices) {
                        // Add the name and address to an array adapter to show in a ListView
                        arrayListBluetooth.add(device );
                    }
                }




            return arrayListBluetooth;
        }

    }

列出行Xml

    

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/devName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/devMac" />

</relativeLayout>

活动布局

 <?xml version="1.0" encoding="utf-8"?>
    <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="com.example.fatma.listviewcst.MainActivity">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您在列表项xml中使用小案例relativelayout。 这些观点区分大小写。 所以使用 RelativeLayout代替relativeLayout