连接蓝牙设备

时间:2018-03-29 10:41:09

标签: java android bluetooth

package com.example.stef.bluetooth_version1000;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {
private int REQUEST_ENABLE_BT=1;
   private  ArrayList<String> devices;
   private List<BluetoothDevice> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Handler handler=null;

        /*Creation des objets Bluetooth*/
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device doesn't support Bluetooth
        }
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
        ListView lv=(ListView)findViewById(R.id.listview);

        final Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        ArrayList<String> devices = new ArrayList<>();
        for (BluetoothDevice bt : pairedDevices) {
            devices.add(bt.getName() + "\n" + bt.getAddress());
        }

        final ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, devices);
        lv.setAdapter(arrayAdapter);


        final BluetoothChatService bluetoothChatService= new BluetoothChatService(MainActivity.this,handler);


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                bluetoothChatService.start();
                bluetoothChatService.connect();
            }
        });

    }
}

我的功能连接需要一个BluetoothDevice参数 我希望能够从ListView onClick()获取它,但我只设法得到一个名称或地址。

我似乎需要从Set&#34;配对设备&#34;中获取蓝牙设备 我正在使用ConnectionChatService.java类。

1 个答案:

答案 0 :(得分:0)

你有一个只读字符串的原始适配器

实际存储BluetoothDevice in a custom adapter

列表

从自定义类开始

public class BluetoothArrayAdapter extends ArrayAdapter<BluetoothDevice> {
    // implement getView 

然后你可以使用它,例如,在点击监听器中获取点击位置的项目

final BluetoothArrayAdapter arrayAdapter = new BluetoothArrayAdapter(MainActivity.this, R.layout.your_bluetooth_layout);
arrayAdapter.addAll(mBluetoothAdapter.getBondedDevices());
lv.setAdapter(arrayAdapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            bluetoothChatService.start();
            bluetoothChatService.connect(arrayAdapter.getItem(i));
        }
    });