【问题标题】:How to find Android Bluetooth version?如何找到安卓蓝牙版本?
【发布时间】:2012-02-03 09:38:43
【问题描述】:

我需要以编程方式在手机上查找 Android 蓝牙版本。谁能给我提示如何做到这一点?

【问题讨论】:

    标签: android bluetooth version


    【解决方案1】:

    据我所知(我做了很多研究)没有办法知道你的 Android 蓝牙设备的硬件版本是什么。 (4.0, 4.2, 5.0,...)

    有些人声称他们有一个可以做到这一点的应用程序,但我从未见过一个有效的例子。这些应用会显示很多版本号,但不会显示蓝牙硬件版本。

    有些人想出了一个技巧,可以显示蓝牙软件的版本号,但这不是我们想知道的。

    有一些技巧可以获得蓝牙设备的功能,但同样,这不是我们想知道的。

    【讨论】:

      【解决方案2】:

      恕我直言,使用 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 标志,可以引导应用程序流。

      【讨论】:

      • 如果我错了,请纠正我,但这些 systemFeatured 检查器是否用于检查使用功能是否设置为必需:
      • 告诉 Google Play 商店不要向使用不支持的设备的用户展示应用以 bt_le 为例。
      【解决方案3】:
      <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。你读过这个问题吗?
      【解决方案4】:

      您只需尝试以下方式查找蓝牙版本即可。

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

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

      【讨论】:

      • 它并没有真正帮助找出蓝牙手机支持哪个版本
      猜你喜欢
      • 2016-12-08
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 2011-07-20
      • 2012-12-16
      相关资源
      最近更新 更多