【发布时间】:2012-02-03 09:38:43
【问题描述】:
我需要以编程方式在手机上查找 Android 蓝牙版本。谁能给我提示如何做到这一点?
【问题讨论】:
我需要以编程方式在手机上查找 Android 蓝牙版本。谁能给我提示如何做到这一点?
【问题讨论】:
据我所知(我做了很多研究)没有办法知道你的 Android 蓝牙设备的硬件版本是什么。 (4.0, 4.2, 5.0,...)
有些人声称他们有一个可以做到这一点的应用程序,但我从未见过一个有效的例子。这些应用会显示很多版本号,但不会显示蓝牙硬件版本。
有些人想出了一个技巧,可以显示蓝牙软件的版本号,但这不是我们想知道的。
有一些技巧可以获得蓝牙设备的功能,但同样,这不是我们想知道的。
【讨论】:
恕我直言,使用 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 标志,可以引导应用程序流。
【讨论】:
<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
【讨论】:
您只需尝试以下方式查找蓝牙版本即可。
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;
}
这是您与蓝牙交互时的核心代码。
【讨论】: