最近在作一个蓝牙的项目,是Blu4.0,Android系统支持是4.3版本以上(为此特意卖了个手机,)。html
固然也参考了很多大牛的的微博,说的都不错,再次特意感谢java
http://blog.csdn.net/hellogv/article/details/6036849android
http://blog.csdn.net/hellogv/article/details/6042091git
http://www.cnblogs.com/zdz8207/archive/2012/10/17/bluetooth_ble_android.html
github
http://blog.csdn.net/changemyself/article/details/8454633
app
http://blog.csdn.net/hellogv/article/details/24267685
测试
调试Ble硬件的读写的Appui
http://www.wandoujia.com/apps/com.siflink.ble.mgrthis
这个App,能够在本身app为开发好的状况下,简单的测试下读写特征值,不知道为何,好像只能写入一个值。也许是我不知道怎么写入多个值。spa
下面这个是从github上搜索的一个ble4.0的代码
https://github.com/StevenRudenko/BleSensorTag
网上有不少关于Ble4.0的开发,再次也不作介绍,反正都是差很少的。
一、声明蓝牙的权限
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
二、监听搜索的回调
BleBluetoothGattCallback gattCallback = new BleBluetoothGattCallback(); gattCallback.setOnBleReadCallback(new OnBleReadCallback() { public void onReadData(byte[] data) { } public void onReadComple() { McLog.mByStackTrace(); } public void onConnect() { showToast("链接设备成功"); isConnect = true; } public void onDisConnect() { showToast("断开设备"); isConnect = false; } }); BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { McLog.mByStackTrace(); String address = device.getAddress(); String name = device.getName(); String t = name + "[" + address + "]"; McLog.i("find a device %s", t); if (isRightDevice(device)) { // 判断是不是本身的想要的设备 btAdapter.stopLeScan(this); // 进行请求的连接 device.connectGatt(context, false, gattCallback); McLog.i("device %s is this account's.", t); } else { McLog.i("device %s is not this account's.", t); } } }; BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); btAdapter = bluetoothManager.getAdapter(); btAdapter.startLeScan(leScanCallback);
三、设置请求连接的回调
public class BleBluetoothGattCallback extends BluetoothGattCallback { public static final UUID SERVER_UUID = UUID.fromString("0000FFF0-0000-1000-8000-00805F9B34FB"); OnBleReadCallback onBleReadCallback; public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { // 连接状态的改变的回调 McLog.mByStackTrace(); McLog.i("status = " + status); McLog.i("newState = " + newState); switch (newState) { case BluetoothProfile.STATE_CONNECTED: McLog.e("STATE_CONNECTED"); // 连接成功 gatt.discoverServices(); if (onBleReadCallback != null) { onBleReadCallback.onConnect(); } break; case BluetoothProfile.STATE_DISCONNECTED: gatt.close(); McLog.e("STATE_DISCONNECTED");// 断开连接 if (onBleReadCallback != null) { onBleReadCallback.onDisConnect(); } break; case BluetoothProfile.STATE_CONNECTING: McLog.e("STATE_CONNECTING"); break; case BluetoothProfile.STATE_DISCONNECTING: McLog.e("STATE_DISCONNECTING"); break; } } public void onServicesDiscovered(final BluetoothGatt gatt, int status) { // <span style="font-family: Arial, Helvetica, sans-serif;">发现服务的回调</span> McLog.mByStackTrace(); McLog.i("status = " + status); if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(SERVER_UUID); McLog.i("service = " + service); if (service != null) { // 找到服务后,能够进行读取数据和写入数据 } } else { // McToastUtil.show("discover services fail."); } } public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {// 读取特征值的回调 McLog.i("onCharacteristicRead"); UUID currentUUId = characteristic.getUuid(); McLog.i("currentUUId = " + currentUUId); if (currentUUId == null) { return; } // 数据的内容 byte[] data = characteristic.getValue(); McLog.i("data = " + Arrays.toString(data)); } public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {// 写入特征值回调 McLog.i("onCharacteristicWrite status = " + status); } public void setOnBleReadCallback(OnBleReadCallback onBleReadCallback) { this.onBleReadCallback = onBleReadCallback; } public interface OnBleReadCallback { // 本身定义的回调 void onConnect(); void onReadData(byte[] data); void onReadComple(); void onDisConnect(); } }
最后特别重要的是,必定好配合硬件工程师调试蓝牙。