【问题标题】:Connecting a Bluetooth Device连接蓝牙设备
【发布时间】:2018-03-29 10:41:08
【问题描述】:
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();
            }
        });

    }
}

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

看来我需要从设置“配对设备”中获取蓝牙设备。
我正在使用ConnectionChatService.java 类。

【问题讨论】:

    标签: java android bluetooth


    【解决方案1】:

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

    其实存储了一个蓝牙设备列表in a custom adapter

    从自定义类开始

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

    然后就可以使用它了,比如在点击监听中获取被点击位置的item

    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));
            }
        });
    

    【讨论】:

    • 谢谢它的工作!现在我尝试使用蓝牙类,我阅读了很多文档和教程,但根本不清楚。有很多方法可以做到。
    • 据我所知,与蓝牙套接字通信的方式只有一种
    • 我按照以下步骤操作:developer.android.com/guide/topics/connectivity/bluetooth.html 而且我不知道如何在 MainActivity 中与我的 bluetoothService 进行交互
    • 随时创建一个包含您的问题和问题的新帖子。这篇文章只是关于让适配器正常工作,所以请随时使用帖子旁边的复选标记接受它
    • 我的建议是尝试使用示例应用程序developer.android.com/samples/BluetoothChat/index.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 2020-01-27
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多