How to find Android Bluetooth version?

依然范特西╮ 提交于 2020-05-13 02:01:57

问题


I need to programatically find Android Bluetooth version on the phone. Can someone me tips how to do that?


回答1:


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. (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.




回答2:


<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




回答3:


IMHO, with android you can distinguish only presence of Bluetooth or Bluetooth_LE. But I am doubtful about android support on identifying Bluetooth versions (e.g. BT2.0, BT2.1+EDR, BT3.0 etc). The way to programatically identify the BT or BLE presence only could be:

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.




回答4:


You just try the following way to find bluetooth version.

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()

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()

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.



来源:https://stackoverflow.com/questions/9126512/how-to-find-android-bluetooth-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!