gpt4 book ai didi

java - 连接蓝牙设备

转载 作者:行者123 更新时间:2023-11-29 02:31:07 25 4
gpt4 key购买 nike

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() 获取它,但我只设法获取了名称或地址。

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

最佳答案

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

实际存储了一个BluetoothDevice列表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));
}
});

关于java - 连接蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49553990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com