最近在研究这个,等我有时间来写吧!java
终于在端午节给本身放个假,如今就来讲说关于android蓝牙ble的android
最近的学习成果吧!!shell
须要材料(写个简单教程吧~~关于小米手环的哦!嘿嘿)数组
Android 手机一部 要求android 4.3 系统以上框架
小米手环一个 一代最好 (我手里只有一代的 , 二代有没有修改uuid 我不清楚)ide
首先说明想要使用android作蓝牙ble的开发首先须要Android 4.3以上的系统哦!这个应该不难,目前大部分Android手机都比这个版本高吧布局
下面就让咱们开始吧~学习
首先先了解下基础知识!看看系统先帮咱们作好了那些事情吧!~ui
上图!!this
这些是Android 系统为咱们作的事
首先看BluetoothAdapter
这是系统提供的蓝牙适配器。在使用手机的蓝牙功能前必定是要先获取的。
该适配器提供了一个扫描方法( bluetoothAdapter.startLeScan(callback) ),能够用它来获取蓝牙设备。是否是很贴心啊~~
扫描完设备以后 回调方法中会获得扫描到的设备信息放在bluetoothdevice中
选择咱们须要的设备后能够经过 bluetoothdevice的connectGatt方法链接设备。
哎呀~说的太复杂。仍是在项目里说明吧!
先布局:
就简单的作成这样的。
小米手环有震动 和计步的功能,咱们就在下面展现下计步和电池电量。
主要玩它的震动功能~嘿嘿~~~
简单的写个线性布局。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.wbnq.shouhuan.MainActivity"> <Button android:id="@+id/saomiao" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="扫描设备"/> <Button android:id="@+id/zhendong" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="短震 / 嘿嘿嘿"/> <Button android:id="@+id/changzhen" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="长震 / 呵呵呵"/> <Button android:id="@+id/buting" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="不要停 / 哈哈哈"/> <Button android:id="@+id/tingxia" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停下来 / ~~"/> <TextView android:id="@+id/jibu" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="走了多少步:" android:gravity="center" android:textSize="20dp"/> <TextView android:id="@+id/dianliang" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="电池电量" android:gravity="center" android:textSize="20dp"/> <TextView android:id="@+id/lianjiezhuangtai" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="链接状态:未链接" android:gravity="center" android:textSize="20dp"/> </LinearLayout>
正戏来啦:
MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button saomiao , duanzhen , changzhen , buting , tingxia; private TextView jibu , dianliang , lianjiezhuangtai; BluetoothAdapter bluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //蓝牙管理,这是系统服务能够经过getSystemService(BLUETOOTH_SERVICE)的方法获取实例 BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); //经过蓝牙管理实例获取适配器,而后经过扫描方法(scan)获取设备(device) bluetoothAdapter = bluetoothManager.getAdapter(); } private void initView() { saomiao = (Button) findViewById(R.id.saomiao); duanzhen = (Button) findViewById(R.id.zhendong); changzhen = (Button) findViewById(R.id.changzhen); buting = (Button) findViewById(R.id.buting); tingxia = (Button) findViewById(R.id.tingxia); jibu = (TextView) findViewById(R.id.jibu); dianliang = (TextView) findViewById(R.id.dianliang); lianjiezhuangtai = (TextView) findViewById(R.id.lianjiezhuangtai); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.saomiao: break; case R.id.zhendong: break; case R.id.changzhen: break; case R.id.buting: break; case R.id.tingxia: break; } } }
这个是基本框架。
咱们看到在onCreate方法中 咱们获取了
BluetoothManager实例,并经过该实例获取了系统的蓝牙适配器
bluetoothAdapter
//蓝牙管理,这是系统服务能够经过getSystemService(BLUETOOTH_SERVICE)的方法获取实例 BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); //经过蓝牙管理实例获取适配器,而后经过扫描方法(scan)获取设备(device) bluetoothAdapter = bluetoothManager.getAdapter();
另外,想要正常执行的话还须要添加必须的权限:(目前须要的,后面还会加入新权限)
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
下面咱们来实现扫描方法吧!
首先,想要实现扫描功能天然要先打开蓝牙啦!!
请求开启蓝牙的代码:
//开始扫描前开启蓝牙 Intent turn_on = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turn_on, 0); Toast.makeText(MainActivity.this, "蓝牙已经开启", Toast.LENGTH_SHORT).show();
由于扫描是个耗时操做,因此最好把它写在线程当中:
Thread scanThread = new Thread(new Runnable() { @Override public void run() { Log.i("TAG", "run: saomiao ..."); saomiao(); } });
扫描方法:
public void saomiao(){ deviceList.clear(); bluetoothAdapter.startLeScan(callback); }
代码很简单是吧!的确~
deviceList是个bluetoothdevice的数组(List<BluetoothDevice> deviceList = new ArrayList<>();)
用来存放扫描来的全部device。用以前固然要先清理(clear)一下啦~
而后使用 bluetoothAdapter提供的扫描方法扫描蓝牙ble设备(这里固然就是小米手环啦)
扫描方法有个回调:
//扫描回调 public BluetoothAdapter.LeScanCallback callback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice bluetoothDevice, int i, byte[] bytes) { Log.i("TAG", "onLeScan: " + bluetoothDevice.getName() + "/t" + bluetoothDevice.getAddress() + "/t" + bluetoothDevice.getBondState()); //重复过滤方法,列表中包含不应设备才加入列表中,并刷新列表 if (!deviceList.contains(bluetoothDevice)) { //将设备加入列表数据中 deviceList.add(bluetoothDevice); } } };
将前面扫描到的数据放进deviceList列表中。
这里若是直接运行的话确定会弹出个要获取位置的错误。很少说
为项目添加位置权限:
<!-- Android6.0 蓝牙扫描才须要--> <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
为啥要位置权限我也不清楚。还须要研究呢。
如今已经能够扫描咯!
看看都扫描到那些设备把:
千万别觉得我有那么多蓝牙设备哦!其实仔细看原来只有两个~~
没错~一个是手环另外一个是其余蓝牙ble设备,为啥会出来这么多呢?
由于每一个开着的蓝牙ble设备一直都在以固定的频率发出服务(例如1s/1次)以便手机搜索到该设备
因此这个扫描咱们才要写进线程里啊~~
那如何中止扫描呢?很简单哦!
开启扫描的方法记得么?
bluetoothAdapter.startLeScan(callback);
关闭的方法基本同样,系统也提供了对应的中止方法:
bluetoothAdapter.stopLeScan(callback);
很方便吧?
下一节咱们让设备在手机上以列表形式显示出来吧~~
你们加油啦~~~
android 系统调用shell脚本