新手刚接触蓝牙4.0,好不容易实现了读取蓝牙上的步数,体重上的数据和血压计的数据。网上资料真的很难找。
扫描蓝牙设备的Activity
public class DeviceScanActivity extends ListActivity { private final static int REQUEST_ENABLE_BT = 1; private boolean mScanning; private LeDeviceListAdapter mLeDeviceListAdapter; private Handler mHandler; private BluetoothAdapter mBluetoothAdapter; private static final long SCAN_PERIOD = 10000; public static int MY_ACCESS_COARSE_LOCATION = 7895; boolean mHasPermission = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //getActionBar().setTitle(R.string.title_device); if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_support, Toast.LENGTH_SHORT).show(); finish(); } BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, R.string.error_bluetooth_not_support, Toast.LENGTH_SHORT).show(); finish(); return; } if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){ mHasPermission = false; }else { mHasPermission = true; } if(!mHasPermission) { //申请ACCESS_COARSE_LOCATION权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_ACCESS_COARSE_LOCATION); Toast.makeText(this, "do not has ACCESS_COARSE_LOCATION Permission!!!", Toast.LENGTH_LONG).show(); } mHandler = new Handler(); scanLeDevice(true); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); doNext(requestCode, grantResults); } private void doNext(int requestCode, int[] grantResults) { if (requestCode == MY_ACCESS_COARSE_LOCATION) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission Granted 允许授予的权限 // 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; } //mHasPermission = true; } else { // Permission Denied Toast.makeText(this, "No Permission!!!", Toast.LENGTH_LONG).show(); //mHasPermission = false; finish(); } } } @Override protected void onResume() { super.onResume(); if (mHasPermission) { if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(mBluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } mLeDeviceListAdapter = new LeDeviceListAdapter(); setListAdapter(mLeDeviceListAdapter); scanLeDevice(true); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED){ finish(); return; } super.onActivityResult(requestCode, resultCode, data); } @Override protected void onPause() { super.onPause(); mLeDeviceListAdapter.clear(); scanLeDevice(false); } public void scanLeDevice(final boolean enable){ if(enable){ mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); invalidateOptionsMenu(); } },SCAN_PERIOD); mScanning = true; mBluetoothAdapter.startLeScan(mLeScanCallback); }else { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } invalidateOptionsMenu(); } private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { runOnUiThread(new Runnable() { @Override public void run() { mLeDeviceListAdapter.addDevice(device); mLeDeviceListAdapter.notifyDataSetChanged(); } }); } }; public class LeDeviceListAdapter extends BaseAdapter{ private ArrayList<BluetoothDevice> mLeDevices; private LayoutInflater mInflater; public LeDeviceListAdapter(){ super(); mLeDevices = new ArrayList<BluetoothDevice>(); mInflater = DeviceScanActivity.this.getLayoutInflater(); } public void addDevice(BluetoothDevice device){ if(!mLeDevices.contains(device)){ mLeDevices.add(device); } } public void clear(){ mLeDevices.clear(); } public BluetoothDevice getDevice(int position){ return mLeDevices.get(position); } @Override public int getCount() { return mLeDevices.size(); } @Override public Object getItem(int position) { return mLeDevices.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder viewHolder; if(view == null){ viewHolder = new ViewHolder();