使用 Java 类编写的蓝牙测试代码,用于搜索和显示可连接蓝牙的名称与id,并利用 View 显示出来,搜索使用TextView,显示可连接蓝牙的名称与id使用ListView。类名为 BluetoothTest
时间: 2024-02-24 19:59:58 浏览: 82
好的,让我为您编写这个Java类。以下是BluetoothTest类的代码:
```java
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class BluetoothTest extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private List<String> deviceList = new ArrayList<>();
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_test);
// 获取BluetoothAdapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show();
finish();
}
// 初始化TextView和ListView
TextView textView = findViewById(R.id.textView);
listView = findViewById(R.id.listView);
// 设置ListView的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = deviceList.get(i);
String[] deviceInfo = item.split("\n");
String name = deviceInfo[0];
String address = deviceInfo[1];
Intent intent = new Intent();
intent.putExtra("name", name);
intent.putExtra("address", address);
setResult(RESULT_OK, intent);
finish();
}
});
// 搜索蓝牙设备
if (!bluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);
} else {
searchDevices();
}
}
// 搜索蓝牙设备
private void searchDevices() {
deviceList.clear();
bluetoothAdapter.startDiscovery();
Toast.makeText(this, "正在搜索蓝牙设备", Toast.LENGTH_SHORT).show();
// 设置广播接收器,接收搜索到的蓝牙设备
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
}
// 广播接收器,接收搜索到的蓝牙设备
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
String address = device.getAddress();
if (name == null) {
name = "未知设备";
}
String item = name + "\n" + address;
deviceList.add(item);
// 更新ListView
ArrayAdapter<String> adapter = new ArrayAdapter<>(BluetoothTest.this, android.R.layout.simple_list_item_1, deviceList);
listView.setAdapter(adapter);
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
```
在此代码中,我们使用了BluetoothAdapter类来搜索蓝牙设备。我们还使用了ListView和ArrayAdapter类来显示可连接蓝牙的名称与id。我们还使用了BroadcastReceiver类来接收搜索到的蓝牙设备。在ListView中,当用户点击某个蓝牙设备时,我们将其名称和id作为Intent的Extra数据返回给调用此Activity的Activity。请注意,这只是一个简单的示例,您可以根据您的需求进行修改。
阅读全文