簡體   English   中英

如何找到Android藍牙版?

[英]How to find Android Bluetooth version?

我需要以編程方式在手機上找到Android藍牙版本。 有人可以提示如何做到這一點嗎?

<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

恕我直言,與Android你可以區分只有藍牙或Bluetooth_LE的存在。 但我懷疑Android支持識別藍牙版本(例如BT2.0,BT2.1 + EDR,BT3.0等)。 以編程方式識別BT或BLE存在的方式可能是:

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

此后,使用isBT或isBLE標志,可以指導app流程。

據我所知(我做了很多研究) ,沒有辦法找出你的Android藍牙設備的硬件版本 (4.0,4.2,5.0,...)

有些人聲稱他們有一個可以做到這一點的應用,但我從未見過一個有效的例子。 這些應用程序向您顯示了許多版本號,但沒有顯示藍牙硬件版本。

有些人想出了一個技巧,向您展示藍牙軟件的版本號,但這不是我們想知道的。

有一些技巧可以獲得藍牙設備的功能,但同樣,這不是我們想知道的。

您只需嘗試以下方式找到藍牙版本。

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

當您與藍牙交互時,這是核心編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM