BLE蓝牙开发.pdf
文本预览下载声明
获取蓝牙,查看是否支持,扫描蓝牙,显示扫描结果,点击某个蓝牙
后后跳转到MainActivity 进行连接
package com.bytereal.sensor6050demo.ui;
import com.bytereal.sensor6050demo.BaseApp;
import com.bytereal.sensor6050demo.Constans;
import com.bytereal.sensor6050demo.R;
import com.bytereal.sensor6050demo.util.StringUtil;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
public class PeripheralActivity extends Activity implements OnItemClickListener, OnClickListener {
private final String TAG = PeripheralActivity;
private static final long SCAN_PERIOD = 3000;
private Button mScanButton;
private ListView mDeviceListView;
private boolean isScan = false;
private Handler mHandler = new Handler();
private BluetoothAdapter mBluetoothAdapter;
//蓝牙设备集合
private ArrayListBluetoothDevice mLeDevices = new ArrayListBluetoothDevice();
private ArrayListHashMapString, Object listItem; // ListView 的数据源用于显示在界面,
这里是一个HashMap 的列表
private SimpleAdapter listItemAdapter; // ListView 的适配器
private static final int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
super.onCreate(savedInstanceState);
setContentView(R.layout.peripheral);
BaseApp.getInstance().addActivity(this);
initView();
initBlue();
setupView();
}
/**
* 检查手机蓝牙
*/
private void initBlue() {
// 检查当前手机是否支持ble 蓝牙,如果不支持退出程序
if
(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
// 初始化 Bluetooth adapter, 通过蓝牙管理器得到一个参考蓝牙适
显示全部