简体   繁体   English

如何找到Android蓝牙版?

[英]How to find Android Bluetooth version?

I need to programatically find Android Bluetooth version on the phone. 我需要以编程方式在手机上找到Android蓝牙版本。 Can someone me tips how to do that? 有人可以提示如何做到这一点吗?

<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 http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

IMHO, with android you can distinguish only presence of Bluetooth or Bluetooth_LE. 恕我直言,与Android你可以区分只有蓝牙或Bluetooth_LE的存在。 But I am doubtful about android support on identifying Bluetooth versions (eg BT2.0, BT2.1+EDR, BT3.0 etc). 但我怀疑Android支持识别蓝牙版本(例如BT2.0,BT2.1 + EDR,BT3.0等)。 The way to programatically identify the BT or BLE presence only could be: 以编程方式识别BT或BLE存在的方式可能是:

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

Thereafter, using isBT or isBLE flags, the app flow can be directed. 此后,使用isBT或isBLE标志,可以指导app流程。

As far as i know (and i did a lot of research) there is no way to find out what the hardware version is of your Android bluetooth device . 据我所知(我做了很多研究) ,没有办法找出你的Android蓝牙设备的硬件版本 (4.0, 4.2, 5.0,...) (4.0,4.2,5.0,...)

Some people claim they have an app that can do this, but i never saw a working example. 有些人声称他们有一个可以做到这一点的应用,但我从未见过一个有效的例子。 These apps show you a lot of version numbers but not the Bluetooth hardware version. 这些应用程序向您显示了许多版本号,但没有显示蓝牙硬件版本。

Some people come up with a trick that shows you the version number of the bluetooth software, but that is not what we want to know. 有些人想出了一个技巧,向您展示蓝牙软件的版本号,但这不是我们想知道的。

There are some tricks to get the capabilities of the bluetooth device, but again, that is not what we want to know. 有一些技巧可以获得蓝牙设备的功能,但同样,这不是我们想知道的。

You just try the following way to find bluetooth version. 您只需尝试以下方式找到蓝牙版本。

Androidmanifest.xml 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" />

Write coding in onCreate() 在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;
    }
}

Write coding in onResume() 在onResume()中编写代码

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

Adapter 适配器

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

This is core coding when you interact with bluetooth. 当您与蓝牙交互时,这是核心编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM