Android:如何获取蓝牙设备的更多信息?
创始人
2024-10-13 15:31:54
0

要获取蓝牙设备的更多信息,可以使用Android的BluetoothDevice类提供的方法。以下是一个示例代码,演示如何获取蓝牙设备的名称、地址和支持的服务UUID列表:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView deviceNameTextView;
    private TextView deviceAddressTextView;
    private TextView deviceUuidTextView;

    private BluetoothAdapter bluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        deviceNameTextView = findViewById(R.id.device_name_text_view);
        deviceAddressTextView = findViewById(R.id.device_address_text_view);
        deviceUuidTextView = findViewById(R.id.device_uuid_text_view);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // 检查设备是否支持蓝牙
        if (bluetoothAdapter == null) {
            // 如果不支持蓝牙,做相应处理
            return;
        }

        // 通过Intent获取设备的地址
        Intent intent = getIntent();
        String deviceAddress = intent.getStringExtra("device_address");

        // 根据设备地址获取BluetoothDevice对象
        BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress);

        // 获取设备名称并显示在TextView上
        String deviceName = device.getName();
        deviceNameTextView.setText("设备名称:" + deviceName);

        // 获取设备地址并显示在TextView上
        String deviceAddress = device.getAddress();
        deviceAddressTextView.setText("设备地址:" + deviceAddress);

        // 获取设备支持的服务UUID列表并显示在TextView上
        StringBuilder uuidsBuilder = new StringBuilder();
        for (ParcelUuid uuid : device.getUuids()) {
            uuidsBuilder.append(uuid.toString()).append("\n");
        }
        deviceUuidTextView.setText("支持的服务UUID列表:\n" + uuidsBuilder.toString());
    }
}

在上述示例代码中,我们通过BluetoothAdapter类获取默认的BluetoothAdapter实例,并通过getRemoteDevice()方法根据设备地址获取BluetoothDevice对象。然后,我们可以使用BluetoothDevice对象的方法获取设备的名称、地址和支持的服务UUID列表,并将它们显示在TextView上。

相关内容

热门资讯

记者揭秘!wepoker辅助分... 1、记者揭秘!wepoker辅助分析器,拱趴大菠萝有什么挂,科技教程(有挂技巧)。2、拱趴大菠萝有什...
今日焦点!拱趴大菠萝挂,xpo... 今日焦点!拱趴大菠萝挂,xpoker辅助工具,扑克教程(有挂辅助)是一款可以让一直输的玩家,快速成为...
一起来讨论!wepoker怎么... 1、一起来讨论!wepoker怎么发冤家牌,哈糖大菠萝怎么开挂,透明挂教程(有挂软件)(UU pok...
2024教程!wepoker作... 2024教程!wepoker作弊视频,哈糖大菠萝挂法,可靠教程(有挂教程)1、玩家可以在哈糖大菠萝挂...
推荐一款!微扑克微乐辅助,we... 推荐一款!微扑克微乐辅助,wejoker私人辅助软件,科技教程(有挂教程);致您一封信;亲爱wejo...
玩家必看教程!wepoker买... 玩家必看教程!wepoker买钻石有用吗,werplan怎么作弊,AI教程(有挂教程);致您一封信;...
如何分辨真伪!wepoker怎... 此外,数据分析德州(wepoker怎么设置透视)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
必备教程!xpoker辅助神器... 必备教程!xpoker辅助神器,wepoker辅助器有哪些功能,科技教程(有挂方法);致您一封信;亲...
科技通报!aapoker破解侠... 科技通报!aapoker破解侠是真的吗,epoker透视,介绍教程(有挂神器)关于aapoker破解...
推荐十款!约局吧透视挂下载,约... 推荐十款!约局吧透视挂下载,约局吧德州真的有透视挂吗,扑克教程(有挂神器)准备好在约局吧德州真的有透...