如何查找Android Bluetooth版本?

10

我需要通过编程在手机上找到Android蓝牙版本。有人可以给我一些提示吗?

4个回答

6
据我所知(并且我做了很多研究),目前没有办法找出您的 Android 蓝牙设备的硬件版本(4.0、4.2、5.0 等)。
有些人声称他们有一个可以做到这一点的应用程序,但我从未见过真正有效的例子。这些应用会显示许多版本号,但不包括蓝牙硬件版本。
有些人想出了一个技巧,可以显示蓝牙软件的版本号,但这不是我们想要知道的。
有一些技巧可以获取蓝牙设备的功能,但再次强调,这也不是我们想要知道的。

2

我个人认为,对于 Android 系统而言,您只能区分蓝牙或低功耗蓝牙的存在。但是,我对于 Android 是否支持识别蓝牙版本(例如 BT2.0、BT2.1+EDR、BT3.0 等)表示怀疑。

通过编程的方式仅能识别蓝牙或低功耗蓝牙的存在,具体方法如下:

PackageManager pm = getActivity().getPackageManager();
boolean isBT = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
boolean isBLE = pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);

随后,可以使用isBT或isBLE标志来指导应用程序流程。

1
请纠正我,这些systemFeatured检查器不是用于检查uses-feature是否设置为必需的吗:<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>。 - Deko
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> - user10186832

-1
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
    finish();
}

http://developer.android.com/guide/topics/connectivity/bluetooth-le.html


这是一个非常随意的回答,而且你将BLE列为强制项在那个清单中也是不正确的。你读懂问题了吗? - Sinapse

-3

您可以尝试以下方法来查找蓝牙版本。

Androidmanifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="false" />

在onCreate()中编写代码

public void onCreate(Bundle savedInstanceState) {

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        //  finish();
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
        // finish();
        return;
    }
}

在onResume()中编写代码

protected void onResume() {
    mLeDeviceListAdapter = new LeDeviceListAdapter();
    setListAdapter(mLeDeviceListAdapter);
}

适配器

// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;

    private LayoutInflater mInflator;

    public LeDeviceListAdapter() {
        super();
        //mLeDevices = new ArrayList<BluetoothDevice>();

        mInflator = DeviceScanActivity.this.getLayoutInflater();
    }

    public void addDevice(BluetoothDeviceModel device, int rssiValue, String address) {

       Log.d("TAG", "map size is : " + mapBluetoothDevice.size());
    }



    public List<BluetoothDevice> getDevice(int position) {
        return mLeDevices.get(position);
    }

    public void clear() {
        mLeDevices.clear();
    }

    @Override
    public int getCount() {
        return mLeDevices.size();
    }

    @Override
    public Object getItem(int i) {

        return mLeDevices.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
            viewHolder.deviceDistance = (TextView) view.findViewById(R.id.device_distance);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        BluetoothDevice device = mLeDevices.get(i);

        final String deviceName = device.getName();

        if (deviceName != null && deviceName.length() > 0)
            viewHolder.deviceName.setText(deviceName);
        else
            viewHolder.deviceName.setText(R.string.unknown_device);

        viewHolder.deviceRssi.setText("Version : " + device.getVersion());
        viewHolder.deviceAddress.setText(device.getDevice().getBluetoothAddress());

        }

        viewHolder.deviceDistance.setText("Distance : " + String.valueOf(distance));
        return view;
    }

这是与蓝牙交互时的核心编码。


它并没有真正帮助我们找出手机支持哪个版本的蓝牙。 - Ewoks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接