我正在学习android中的蓝牙。我遇到蓝牙LE我尝试通过遵循Android文档来实现它,它似乎不起作用。
我对蓝牙LE感到困惑。它与普通蓝牙有什么不同。我确实在文档中读到它用于连接到健身设备等等这是否意味着正常的蓝牙实现不允许我们连接到健身设备?
有人可以解释一下吗?提前谢谢:)
这是我为蓝牙LE编写的代码
MainActivity.java
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private BluetoothManager mBluetoothManager;
private LeDeviceListAdapter mLeDeviceListAdapter;
private boolean mScanning, mBluetoothEnable;
private Handler mHandler = new Handler();
private ListView mListView;
//Request code to enable Bluetooth
private static final int REQUEST_BT_ENABLE = 9990;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), mLeDeviceListAdapter.getNumberOfDevicesFound(), 0).show();
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLeDeviceListAdapter = new LeDeviceListAdapter(this);
mListView = (ListView) findViewById(R.id.listview);
mListView.setAdapter(mLeDeviceListAdapter);
//Check if the device supports BLE Devices
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_supported, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
}
//BluetoothManager is supported from Android 4.3 (API Level 18) and above ONLY.
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//Get the bluetooth adapter.
mBluetoothAdapter = mBluetoothManager.getAdapter();
}
@Override
protected void onResume() {
super.onResume();
if(!mBluetoothAdapter.isEnabled()){//bluetooth not enabled
mBluetoothEnable = false;
// Ensures Bluetooth is available on the device and it is enabled. If not, displays a dialog requesting user permission to enable Bluetooth.
makeSureBluetoothIsEnabled();
}else{//bluetooth is enabled
mBluetoothEnable = true;
}
scanLeDevices(mBluetoothEnable);
}
private void makeSureBluetoothIsEnabled(){
//If bluettoth is not enabled, enable it
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_BT_ENABLE);
}
private void scanLeDevices(boolean enable){
if(enable){
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
Toast.makeText(getApplicationContext(), "Stopped: "+mLeDeviceListAdapter.getNumberOfDevicesFound(), 0).show();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){ ///SUCCESSFULL
switch (requestCode) {
case REQUEST_BT_ENABLE:
mBluetoothEnable = true;
Toast.makeText(this, R.string.bt_enable, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
LeDeviceListAdapter.java
public class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter(Activity activity) {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = activity.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public int getNumberOfDevicesFound(){
return mLeDevices.size();
}
public 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 position, 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);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(position);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
static class ViewHolder{
TextView deviceAddress;
TextView deviceName;
}
}
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="19" />
<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="false"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
蓝牙LE代表低能耗。 它是自4.0以来蓝牙核心规范的一部分,也是您可以找到有关其工作原理的所有信息的地方。
所有规格均可在Bluetooth SIG(bluetooth.org)上找到
https://www.bluetooth.org/en-us/specification/adopted-specifications
如果您想连接健身设备,则需要BT 4.0或更新版本。
我会先阅读Vol。其中6个:https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439